Time-Based Scheduling
Run periodic work.
Time-Based Scheduling is a free Go Academy lesson on CoddyKit. This is lesson 1 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.
Running Work on a Schedule
Many systems need periodic work: cleaning temp files nightly, sending a digest email each morning, polling an API every minute. This lesson introduces time-based scheduling in Go using the standard library.
The time Package
Go's time package is the foundation. Key tools:
time.Sleeppauses a goroutinetime.Afterreturns a channel that fires oncetime.Tickerfires repeatedlytime.Timerfires once after a delay
A Naive Loop
The simplest scheduler: do work, sleep, repeat.
package main
import (
"fmt"
"time"
)
func main() {
for i := 0; i < 3; i++ {
fmt.Println("tick", i)
time.Sleep(10 * time.Millisecond)
}
}Why Sleep Drifts
A sleep loop drifts: if the work takes 200ms and you sleep 1s, your real interval is 1.2s, and it accumulates. For accurate intervals, use a Ticker, covered in a later lesson.
time.After for One-Shot Delays
time.After(d) returns a channel that receives once after duration d. Useful in select for timeouts.
select {
case <-time.After(2 * time.Second):
fmt.Println("2s elapsed")
}time.Timer
A Timer fires once and can be stopped or reset, unlike time.After which you cannot cancel.
t := time.NewTimer(5 * time.Second)
defer t.Stop()
<-t.C
fmt.Println("fired")Scheduling at a Specific Time
To run at, say, 3 AM, compute the duration until that moment and sleep/timer for it, then reschedule for the next day.
now := time.Now()
next := time.Date(now.Year(), now.Month(), now.Day(), 3, 0, 0, 0, now.Location())
if next.Before(now) {
next = next.Add(24 * time.Hour)
}
fmt.Println("sleep for", time.Until(next))Durations Are Typed
time.Duration is an int64 of nanoseconds with handy constants: time.Second, time.Minute, time.Hour. Multiply them for readable intervals.
every := 30 * time.Minute
fmt.Println(every)Monotonic vs Wall Clock
Go's time.Now includes a monotonic reading used for measuring elapsed time, immune to clock adjustments. Use time.Since for durations, not wall-clock subtraction.
start := time.Now()
// ... work ...
fmt.Println("took", time.Since(start))When to Reach for a Library
For complex schedules ("every weekday at 9:15") hand-rolling time math is error-prone. The robfig/cron library, covered next, parses cron expressions for you.
Runnable: Compute Next Run
This self-contained program computes how long until the next minute boundary using only the time package.
package main
import (
"fmt"
"time"
)
func main() {
now := time.Date(2026, 1, 1, 10, 30, 45, 0, time.UTC)
nextMinute := now.Truncate(time.Minute).Add(time.Minute)
fmt.Println("now:", now.Format("15:04:05"))
fmt.Println("next run:", nextMinute.Format("15:04:05"))
fmt.Println("wait:", nextMinute.Sub(now))
}Quick Check
Test your understanding of time-based scheduling.
Recap
You learned time-based scheduling basics:
- The
timepackage provides Sleep, After, Timer, Ticker - Sleep loops drift; tickers stay on schedule
- Compute durations to a target time with
time.Until - Use
time.Sinceand the monotonic clock for elapsed measurement
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 “Time-Based Scheduling” lesson free?
Yes — any 3 lessons of the Go Academy learning path, including “Time-Based Scheduling”, 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 “Time-Based Scheduling”?
Run periodic 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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “Time-Based Scheduling” 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