Go Academy · Lesson

Tickers for Intervals

Repeat at fixed rates.

Lesson 3 of 413 steps

Tickers for Intervals is a free Go Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Go Academy learning path, and your progress syncs across the web and the CoddyKit app. The Go Academy course includes 4 lessons in total.

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 every d on its C channel
  • Always defer ticker.Stop() to avoid leaks
  • Tickers drop beats under slow work rather than bursting
  • Use Reset to change rate and a context/done channel to exit
Free to start

Learn Go with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Tickers for Intervals” lesson free?

Yes — any 3 lessons of the Go Academy learning path, including “Tickers for Intervals”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Go Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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

  1. Time-Based Scheduling
  2. The robfig/cron Library
  3. Tickers for Intervals
  4. Graceful Job Shutdown
← Back to Go Academy