Go Reference ยท Time, Math and Randomness
Go time.Ticker type
Reference for Go time.Ticker, NewTicker, Reset and Stop: run code at a fixed interval, avoid leaks, handle slow receivers and use time.Tick safely.
A time.Ticker delivers the current time on a channel at regular intervals. You create one with time.NewTicker(d), read from its C channel, and call Stop when you are done. It is the standard tool for polling, heartbeats, progress reports and any "do this every N seconds" loop.
Unlike a timer, a ticker keeps firing until it is stopped. If the receiver is slow, ticks are dropped rather than queued, so a ticker never builds up a backlog.
ticker := time.NewTicker(time.Second) defer ticker.Stop() for t := range ticker.C { fmt.Println("tick at", t) }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
type Ticker struct { C <-chan Time // contains filtered or unexported fields } func NewTicker(d Duration) *Ticker func (t *Ticker) Stop() func (t *Ticker) Reset(d Duration) func Tick(d Duration) <-chan Time
| Name | Description |
|---|---|
d |
Interval between ticks. Must be greater than zero. |
C |
Receive-only channel that carries the time of each tick. |
Returns: NewTicker returns a running ticker. Tick returns just the channel, with no way to stop it.
Panics: NewTicker and Reset panic if d <= 0.
Usage notes
- Always call
Stop(usually withdefer). Since Go 1.23 an unreferenced, unstopped ticker can be garbage collected, but stopping is still the clear way to release resources and to say when ticking ends. Stopdoes not closeC. Afor range ticker.Cloop will not end by itself afterStop; leave the loop withbreak,returnor aselectcase on a done channel or context.- Slow receivers lose ticks. If your loop body takes longer than the interval, missed ticks are discarded and the next one arrives as soon as you are ready to receive.
- Ticks are aligned to the start time, not to the end of your work, so the average rate stays steady even if one iteration is slow.
time.Tickis the convenience form withoutStop. Before Go 1.23 it leaked the ticker; now it is collectable, but preferNewTickerin anything long-lived.- Use
Resetto change the period without allocating a new ticker. Since Go 1.23 the channel is unbuffered in effect, so afterStoporResetyou will not receive a stale tick that was computed before the call (when the program's language version is 1.23 or later). - Use a
Timerfor one shot and a ticker for repetition; seetime.After(). - Tests: real tickers make tests slow and flaky. Accept an interval as a parameter and use a few milliseconds in tests.