index
[Timers](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's an example of a ticker that ticks periodically until we stop it.
package main
import (
	"fmt"
	"time"
)
func main() {
Tickers use a similar mechanism to timers: a channel that is sent values. Here we'll use the `select` builtin on the channel to await the values as they arrive every 500ms.
	ticker := time.NewTicker(500 * time.Millisecond)
	done := make(chan bool)
	go func() {
		for {
			select {
			case <-done:
				return
			case t := <-ticker.C:
				fmt.Println("Tick at", t)
			}
		}
	}()
Tickers can be stopped like timers. Once a ticker is stopped it won't receive any more values on its channel. We'll stop ours after 1600ms.
	time.Sleep(1600 * time.Millisecond)
Tick at 2020-10-21 16:49:16.221050216 +0000 UTC m=+0.500221756
Tick at 2020-10-21 16:49:16.721060817 +0000 UTC m=+1.000232379
Tick at 2020-10-21 16:49:17.22112193 +0000 UTC m=+1.500293464
	ticker.Stop()
Tick at 2020-10-21 16:49:18.057000663 +0000 UTC m=+0.500209920
Tick at 2020-10-21 16:49:18.557044531 +0000 UTC m=+1.000253788
Tick at 2020-10-21 16:49:19.057070021 +0000 UTC m=+1.500279270
	done <- true
Tick at 2020-10-21 16:49:19.910618706 +0000 UTC m=+0.500249033
Tick at 2020-10-21 16:49:20.410621395 +0000 UTC m=+1.000251735
Tick at 2020-10-21 16:49:20.910741264 +0000 UTC m=+1.500371590
	fmt.Println("Ticker stopped")
Tick at 2020-10-21 16:49:21.736548374 +0000 UTC m=+0.500250576
Tick at 2020-10-21 16:49:22.236535084 +0000 UTC m=+1.000237307
Tick at 2020-10-21 16:49:22.736562531 +0000 UTC m=+1.500264739
Ticker stopped
}
Tick at 2020-10-21 16:49:23.554917688 +0000 UTC m=+0.500245535
Tick at 2020-10-21 16:49:24.054957087 +0000 UTC m=+1.000284919
Tick at 2020-10-21 16:49:24.554927622 +0000 UTC m=+1.500255482
index