Tickers for Intervals
Repeat at fixed rates.
Tickers for Intervals is a free Go Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Repeating at a Fixed Rate
When you simply need to do something every N seconds, a Ticker is the right tool. Unlike a sleep loop, it fires on a steady cadence and does not drift with work time.
Creating a Ticker
time.NewTicker(d) returns a Ticker whose channel C receives the current time every d.
ticker := time.NewTicker(time.Second)
defer ticker.Stop()Always Stop a Ticker
A ticker keeps an internal timer alive. If you never call Stop(), it leaks. Use defer ticker.Stop() right after creating it.
Ranging Over the Channel
You can range over the ticker channel to react each tick.
for t := range ticker.C {
fmt.Println("tick at", t)
}Stopping With select
Combine the ticker with a done channel so the loop can exit cleanly.
for {
select {
case <-ticker.C:
doWork()
case <-done:
return
}
}Ticker vs Timer
Key difference:
- Timer fires once after a delay
- Ticker fires repeatedly at an interval
Use a Timer for one-shot timeouts, a Ticker for periodic work.
time.Tick Convenience
time.Tick(d) returns just the channel. It is convenient but cannot be stopped, so it leaks if used in long-lived programs. Prefer NewTicker outside of quick scripts.
for range time.Tick(time.Second) {
// simple, but cannot Stop -> avoid in servers
}Handling Slow Work
If your work takes longer than the interval, ticks are not queued; the ticker drops ticks rather than bursting. So you never get a backlog, but you may skip beats under load.
Adjusting the Rate
To change the interval, create a new ticker. Go 1.15+ also offers ticker.Reset(d) to change the period without reallocating.
ticker.Reset(500 * time.Millisecond)Context Cancellation
In real services, drive ticker loops with a context.Context so shutdown signals stop them gracefully.
case <-ctx.Done():
return ctx.Err()Runnable: Fixed-Rate Ticker
This self-contained program ticks three times at a fixed interval, then stops cleanly using a counter.
package main
import (
"fmt"
"time"
)
func main() {
ticker := time.NewTicker(10 * time.Millisecond)
defer ticker.Stop()
count := 0
for range ticker.C {
count++
fmt.Println("tick", count)
if count == 3 {
break
}
}
fmt.Println("done")
}Quick Check
Test your understanding of tickers.
Recap
You learned tickers for intervals:
time.NewTicker(d)fires everydon itsCchannel- Always
defer ticker.Stop()to avoid leaks - Tickers drop beats under slow work rather than bursting
- Use
Resetto change rate and a context/done channel to exit
Frequently asked questions
Is the “Tickers for Intervals” lesson free?
Yes — the full text of “Tickers for Intervals” is free to read here on the web, and the Go Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Go Academy course, upgrade to CoddyKit PRO.
What will I learn in “Tickers for Intervals”?
Repeat at fixed rates. You practise Go Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Go Academy?
No prior experience is required. Go Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Tickers for Intervals” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Go Academy lesson?
Yes. Every Go Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Time-Based Scheduling
- The robfig/cron Library
- Tickers for Intervals
- Graceful Job Shutdown