The robfig/cron Library
Schedule with cron syntax.
The robfig/cron Library 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.
Cron Expressions in Go
The robfig/cron library lets you schedule jobs with familiar cron syntax instead of hand-written time math. Install with go get github.com/robfig/cron/v3.
import "github.com/robfig/cron/v3"Cron Syntax Refresher
A standard cron expression has five fields:
- minute (0-59)
- hour (0-23)
- day of month (1-31)
- month (1-12)
- day of week (0-6)
Example: 0 9 * * 1-5 = 9:00 AM on weekdays.
Creating a Scheduler
cron.New() returns a scheduler. You add jobs, then start it.
c := cron.New()
c.Start()
defer c.Stop()Adding a Job
AddFunc registers a function to run on a schedule, returning an entry ID and an error if the spec is invalid.
id, err := c.AddFunc("30 * * * *", func() {
fmt.Println("runs at minute 30")
})
if err != nil {
log.Fatal(err)
}
_ = idThe Seconds Field
robfig/cron v3 uses a 5-field spec by default (no seconds). To enable a 6-field spec with seconds, build the cron with the seconds option.
c := cron.New(cron.WithSeconds())
c.AddFunc("*/5 * * * * *", func() { fmt.Println("every 5s") })Predefined Schedules
Convenient macros exist:
@hourly@daily@weekly@every 1h30m
c.AddFunc("@every 1h30m", func() { fmt.Println("every 90 min") })
c.AddFunc("@daily", func() { fmt.Println("midnight") })Removing Jobs
Use the returned EntryID to remove a job dynamically.
id, _ := c.AddFunc("@hourly", task)
// later:
c.Remove(id)Jobs Run in Goroutines
Each scheduled job runs in its own goroutine. If a job can overlap with its next trigger, guard against concurrency or use a wrapper that skips overlapping runs.
Timezones
By default schedules use the machine's local time. Pass a location to make them explicit and avoid DST surprises.
loc, _ := time.LoadLocation("America/New_York")
c := cron.New(cron.WithLocation(loc))Parsing Without Running
You can validate a spec or compute the next run with a parser, which is great for tests and showing users the next execution.
sched, _ := cron.ParseStandard("0 9 * * 1-5")
next := sched.Next(time.Now())
fmt.Println("next run:", next)Runnable: Compute Next Cron Run (Analogy)
robfig/cron is external; this standard-library snippet models the simplest cron rule "every minute at minute boundary" by computing the next matching time.
package main
import (
"fmt"
"time"
)
func nextRun(from time.Time) time.Time {
return from.Truncate(time.Minute).Add(time.Minute)
}
func main() {
now := time.Date(2026, 1, 1, 8, 15, 30, 0, time.UTC)
fmt.Println("now:", now.Format("15:04:05"))
fmt.Println("next:", nextRun(now).Format("15:04:05"))
}Quick Check
Test your understanding of the robfig/cron library.
Recap
You learned the robfig/cron library:
cron.New()+AddFunc(spec, fn)+Start()- 5-field spec by default;
WithSecondsadds a seconds field - Macros like
@dailyand@everysimplify common schedules - Use
Remove, locations, andParseStandardfor control and testing
Frequently asked questions
Is the “The robfig/cron Library” lesson free?
Yes — the full text of “The robfig/cron Library” 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 “The robfig/cron Library”?
Schedule with cron syntax. 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 “The robfig/cron Library” 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