Timers are for when you want to do something once in the future - tickers are for when you want to do something repeatedly at regular intervals. Here'an example of a ticker that ticks periodically until we stop it
package main import ( "fmt" "time" ) func main() { ticker := time.NewTicker(time.Millisecond * 500) go func() { for t := range ticker.C { fmt.Println("Tick at", t) } }() time.Sleep(time.Millisecond * 1500) ticker.Stop() fmt.Println("Ticker stopped") }
Tick at 2015-03-18 13:20:55.55095115 +0800 CST Tick at 2015-03-18 13:20:56.050762096 +0800 CST Tick at 2015-03-18 13:20:56.550295916 +0800 CST Ticker stopped
总结 :
1 : ....
有疑问加站长微信联系(非本文作者)