time.Ticker in Go
The time.Ticker
type in Go is incredibly useful for situations in which polling is needed.
The easiest way to use the Ticker is via the time.Tick
function,
which just provides access to the ticking channel,
which makes it easy to range
over:
package main
import (
"fmt"
"time"
)
func main() {
for currentTime := range time.Tick(time.Second) {
fmt.Println("current time: ", currentTime)
}
}
For more control, time.NewTicker
works wonders —
there’s no better example than the one from the docs!