Timers and Tickers
Schedule and repeat work.
Timers and Tickers 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.
Pausing with Sleep
The simplest scheduling tool is time.Sleep(d), which pauses the current goroutine for a duration.
package main
import (
"fmt"
"time"
)
func main() {
fmt.Println("start")
time.Sleep(10 * time.Millisecond)
fmt.Println("done")
}What Is a Timer?
A time.Timer fires once after a delay. It sends the current time on its C channel when it expires.
package main
import (
"fmt"
"time"
)
func main() {
t := time.NewTimer(10 * time.Millisecond)
<-t.C
fmt.Println("timer fired")
}time.After Shortcut
time.After(d) returns a channel that delivers one value after the delay. It is a convenient one-shot timer without a variable.
package main
import (
"fmt"
"time"
)
func main() {
<-time.After(10 * time.Millisecond)
fmt.Println("elapsed")
}Stopping a Timer
timer.Stop() cancels a timer before it fires. It returns true if it stopped the timer in time.
package main
import (
"fmt"
"time"
)
func main() {
t := time.NewTimer(time.Hour)
stopped := t.Stop()
fmt.Println("stopped:", stopped)
}What Is a Ticker?
A time.Ticker fires repeatedly at a fixed interval, sending on its C channel each time.
Use it for periodic work like polling or animation frames.
package main
import (
"fmt"
"time"
)
func main() {
ticker := time.NewTicker(5 * time.Millisecond)
<-ticker.C
fmt.Println("tick")
ticker.Stop()
}Counting Ticks
Loop over the ticker channel to act on each interval, then stop after enough ticks.
package main
import (
"fmt"
"time"
)
func main() {
ticker := time.NewTicker(5 * time.Millisecond)
for i := 1; i <= 3; i++ {
<-ticker.C
fmt.Println("tick", i)
}
ticker.Stop()
}Always Stop a Ticker
Unlike timers, a ticker keeps running until you call Stop(). Forgetting to stop it leaks resources.
package main
import (
"fmt"
"time"
)
func main() {
ticker := time.NewTicker(5 * time.Millisecond)
defer ticker.Stop()
<-ticker.C
fmt.Println("one tick, will stop on return")
}time.Tick Convenience
time.Tick(d) returns a ticker channel directly. It is simple but cannot be stopped, so use it only for the program's whole lifetime.
package main
import (
"fmt"
"time"
)
func main() {
c := time.Tick(5 * time.Millisecond)
<-c
fmt.Println("ticked")
}Timeout with select
Combine a work channel with time.After in a select to implement a timeout.
package main
import (
"fmt"
"time"
)
func main() {
done := make(chan bool)
go func() { done <- true }()
select {
case <-done:
fmt.Println("work finished")
case <-time.After(time.Second):
fmt.Println("timed out")
}
}AfterFunc Runs a Callback
time.AfterFunc(d, f) runs the function f in its own goroutine after the delay, without you reading a channel.
package main
import (
"fmt"
"time"
)
func main() {
done := make(chan bool)
time.AfterFunc(10*time.Millisecond, func() {
fmt.Println("callback ran")
done <- true
})
<-done
}Timer vs Ticker
Choose based on repetition:
- Timer: fires once after a delay.
- Ticker: fires again and again at an interval.
- Stop both when finished to free resources.
package main
import (
"fmt"
"time"
)
func main() {
<-time.After(5 * time.Millisecond)
fmt.Println("one-shot complete")
}Quick Check
Which type fires repeatedly at a fixed interval?
Recap: Timers and Tickers
You learned to schedule work:
Sleeppauses;Timerandtime.Afterfire once.Tickerandtime.Tickfire repeatedly.- Use
Stop()to clean up; combine withselectfor timeouts.
package main
import (
"fmt"
"time"
)
func main() {
ticker := time.NewTicker(5 * time.Millisecond)
defer ticker.Stop()
for i := 0; i < 2; i++ {
<-ticker.C
fmt.Println("beat")
}
}Frequently asked questions
Is the “Timers and Tickers” lesson free?
Yes — the full text of “Timers 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 “Timers and Tickers”?
Schedule and repeat work. 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 “Timers 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
- Time and Duration
- Formatting and Parsing
- Timers and Tickers
- Time Zones