Go (Golang) Task Scheduler: From a Simple Ticker to Industrial Cron
A task scheduler is a component that allows you to execute specific functions at a given time or with a given periodicity. In the Go (Golang) ecosystem, there are several approaches to implementing a scheduler: from built-in language primitives to powerful third-party libraries. In this article, we will look at how to create a task scheduler in Go, starting with simple examples and ending with the use of the popular robfig/cron library.
Why do you need a task scheduler in Go?
Go is ideal for creating high-performance server applications, microservices, and command-line utilities. A task scheduler allows you to automate routine operations:
- Cleaning cache or temporary files
- Sending periodic notifications (email, SMS)
- Collecting metrics and monitoring
- Synchronizing data with external APIs
- Generating reports on a schedule
Basic scheduler using time.Ticker
The simplest way to execute a task every N seconds is to use time.Ticker. This built-in Go tool sends a value to a channel at specified time intervals.
package main
import ( "fmt" "time")
func main() { ticker := time.NewTicker(2 * time.Second) done := make(chan bool)
go func() { for { select { case <-ticker.C: fmt.Println("Task executed:", time.Now().Format(time.RFC3339)) case <-done: ticker.Stop() return } } }()
// Stop the scheduler after 10 seconds time.Sleep(10 * time.Second) done <- true fmt.Println("Scheduler stopped")}This code creates a goroutine that prints the current time every 2 seconds. Note the use of select — this is a key Go pattern for non-blocking handling of multiple channels.
Scheduler with flexible scheduling: the robfig/cron library
For real projects that require cron-like scheduling (for example, "every Monday at 9:00" or "every 5 minutes from 9 to 18"), the github.com/robfig/cron/v3 library is best suited. It fully supports the standard cron syntax and adds convenient features such as timezone support and panic handling.
Installation
go get github.com/robfig/cron/v3Usage example
package main
import ( "fmt" "log" "time"
"github.com/robfig/cron/v3")
func main() { c := cron.New(cron.WithSeconds()) // Enable seconds support
// Add a task: every 10 seconds _, err := c.AddFunc("*/10 * * * * *", func() { fmt.Println("Task 1: every 10 seconds", time.Now().Format(time.RFC3339)) }) if err != nil { log.Fatal(err) }
// Task: every day at 14:30 UTC _, err = c.AddFunc("0 30 14 * * *", func() { fmt.Println("Task 2: daily at 14:30 UTC") }) if err != nil { log.Fatal(err) }
// Task with a name for subsequent management entryID, _ := c.AddFunc("@every 1m", func() { fmt.Println("Task 3: every minute") })
c.Start()
// Remove the task after 5 minutes time.Sleep(5 * time.Minute) c.Remove(entryID) fmt.Println("Task 3 removed")
// Block main select {}}The library supports several schedule formats:
- Standard cron:
"30 * * * *"— every 30 minutes - With seconds:
"0 30 * * * *"— at 30 seconds of every minute - @every:
"@every 5m"— every 5 minutes Also in library
Recommendations