0Pricing
Go Academy · Lesson

Time-Based Scheduling

Run periodic work.

Time-Based Scheduling is a free Go Academy lesson on CoddyKit — lesson 1 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.

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.Sleep pauses a goroutine
  • time.After returns a channel that fires once
  • time.Ticker fires repeatedly
  • time.Timer fires 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 time package provides Sleep, After, Timer, Ticker
  • Sleep loops drift; tickers stay on schedule
  • Compute durations to a target time with time.Until
  • Use time.Since and the monotonic clock for elapsed measurement

Frequently asked questions

Is the “Time-Based Scheduling” lesson free?

Yes — the full text of “Time-Based Scheduling” 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 “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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. Time-Based Scheduling
  2. The robfig/cron Library
  3. Tickers for Intervals
  4. Graceful Job Shutdown
← Back to Go Academy