~/Go Tickers Behind the Scenes
Feb 14, 2022
A ticker in Go delivers repeated notifications on a channel after a specified interval. Use it to perform actions at regular intervals without sleeping in a loop.
Quick Example:
This code prints “Tick” every second for three seconds.
Stopping a ticker is important to release underlying resources:
|
|
Unlike time.After, which waits once, time.Ticker repeatedly sends time values. Using tickers is more efficient than manual sleep in periodic jobs.
Common uses include scheduled tasks, polling, and timeouts in concurrent systems. Always remember to stop the ticker to avoid goroutine leaks.