Timeouts and Tickers
Using time.After, time.Tick with select
Timeouts and Tickers is a free Go Academy lesson on CoddyKit — lesson 2 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.
Why timeouts matter
Without timeouts, a goroutine waiting on a slow channel can block forever. The time package provides primitives to add time-based control to channel operations.
time.After for one-shot timeout
time.After(d) returns a channel that receives a value after duration d. Use it in a select case to abort a wait.
select {
case res := <-ch:
fmt.Println(res)
case <-time.After(2 * time.Second):
fmt.Println("timed out")
}Repeated timeouts with time.NewTimer
For reusable timers, use time.NewTimer and call Reset after each use. Call Stop to prevent leaking the timer goroutine.
timer := time.NewTimer(5 * time.Second)
defer timer.Stop()
select {
case <-timer.C:
fmt.Println("timer fired")
}time.Tick vs time.NewTicker
time.Tick is convenient but leaks the underlying ticker — prefer time.NewTicker so you can call Stop() to release resources.
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for range ticker.C {
fmt.Println("tick")
}Ticker in a loop
Combine a ticker with a done channel to drive periodic work that stops cleanly.
ticker := time.NewTicker(time.Second)
done := make(chan struct{})
go func() {
for {
select {
case <-ticker.C:
doWork()
case <-done:
ticker.Stop()
return
}
}
}()time.After leak
time.After creates a new timer every call and the timer cannot be garbage-collected until it fires. In a tight loop, use time.NewTimer + Reset to avoid a timer leak.
Deadline vs timeout
A timeout is a relative duration ("wait 5s"). A deadline is an absolute time ("wait until 14:00"). Use time.Until(deadline) to convert a deadline to a duration for time.After.
deadline := time.Now().Add(5 * time.Second)
select {
case v := <-ch:
use(v)
case <-time.After(time.Until(deadline)):
fmt.Println("deadline exceeded")
}context.WithTimeout is preferred
For HTTP handlers and structured work, prefer context.WithTimeout over raw channel timeouts — it propagates cancellation through the call stack automatically.
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()Rate limiting with Ticker
A ticker is the simplest rate limiter: send one request per tick. For burst support, pre-fill a buffered channel and refill with a ticker.
requests := make(chan int, 5)
for i := 0; i < 5; i++ { requests <- i }
close(requests)
limiter := time.NewTicker(200 * time.Millisecond)
for req := range requests {
<-limiter.C
fmt.Println("request", req)
}Monotonic clock
Go timer durations use the monotonic clock, not the wall clock, so they are immune to system clock adjustments. time.Since and time.Until also use it.
Stopping a ticker correctly
After ticker.Stop(), the ticker channel is NOT closed — it just stops receiving. Drain any pending value in the channel if needed before returning.
Quick Check
Why is time.NewTicker preferred over time.Tick?
Recap: Timeouts and Tickers
Key points:
- time.After for one-shot timeout in select
- time.NewTicker for repeating work; always call Stop()
- Avoid time.After in loops — it leaks timers
- Prefer context.WithTimeout for cancellable work
Frequently asked questions
Is the “Timeouts and Tickers” lesson free?
Yes — the full text of “Timeouts and Tickers” 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 “Timeouts and Tickers”?
Using time.After, time.Tick with select 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Timeouts and Tickers” 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
- The select Statement
- Timeouts and Tickers
- Fan-in and Fan-out Patterns
- Worker Pool Pattern