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
- Time and Duration
- Formatting and Parsing
- Timers and Tickers
- Time Zones