A very useful utility that uses time.Timer is the time.AfterFunc function, which returns a timer that will execute the passed function in its own goroutine when the timer fires:
func main() {
time.AfterFunc(time.Millisecond, func() {
fmt.Println("Hello 1!")
})
t := time.AfterFunc(time.Millisecond*5, func() {
fmt.Println("Hello 2!")
})
if !t.Stop() {
panic("should not fire")
}
time.Sleep(time.Millisecond * 10)
}
The full example is available at https://play.golang.org/p/77HIIdlRlZ1.
In the previous example, we define two timers for two different closures, and we stop one of them and let the other trigger.