timer and ticker
Go time values that deliver on a channel later: a timer fires once after a duration, a ticker fires repeatedly at an interval.
In more detail
time.After(d) is a one-shot timer channel, handy in a select for a timeout. A ticker suits periodic checks. Stop tickers and timers when you are done so they release resources; a context is often clearer for cancelling a whole request.
Example
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()Related terms
- select statement — A Go statement that waits on several channel operations at once and runs the first one that is ready.
- time.Duration — The Go type for a length of time, such as 2*time.Second, stored as a count of nanoseconds.
- context deadline — A time limit attached to a context, after which it is cancelled automatically and ctx.Err() reports context.DeadlineExceeded.
- polling — Having the client ask repeatedly, on its own schedule, whether something has changed or finished.
Taught in
- The select Statement — Concurrent Systems Programming with Go
- The context Package — Concurrent Systems Programming with Go
- Context Cancellation and Timeouts — Concurrent Systems Programming with Go
- The time Package — Concurrent Systems Programming with Go
- Graceful Shutdown on SIGTERM — Concurrent Systems Programming with Go