0PricingLogin
Go Academy · Lesson

Timers and Tickers

Schedule and repeat work.

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")
}

All lessons in this course

  1. Time and Duration
  2. Formatting and Parsing
  3. Timers and Tickers
  4. Time Zones
← Back to Go Academy