Range Over Ticker In Go With Immediate First Tick
The Go standard library has a really cool type – Ticker . Tickers are used when you want to do something at a regular interval, similar to JavaScript’s setInterval . Here’s an example: package main import ( "fmt" "time" ) func main() { ticker := time.NewTicker(time.Second) go func() { for range ticker.C { fmt.Println("Tick") } }() time.Sleep(time.Second * 4) ticker.Stop() fmt.Println("Ticker stopped") } As per the docs , a ticker is a struct that holds a receive-only channel of time.Time objects. type Ticker struct { C <-chan Time // The channel on which the ticks are delivered. } In the example at the beginning of the article, you will notice by running the program that the first tick sent over the channel happens after the first interval of time has elapsed. As such, if you are trying to bu...