Timers
Context
Section titled “Context”A time.Timer represents a single event in the future. You can wait for it on its channel (<-timer.C) or stop it before it fires.
Example
Section titled “Example”Create a timer that fires after 2 seconds.
Code example
Section titled “Code example”package main
import ( "fmt" "time")
func main() { timer := time.NewTimer(2 * time.Second) fmt.Println("Waiting...") <-timer.C fmt.Println("Timer fired")
// Stopping a timer timer2 := time.NewTimer(1 * time.Second) stop := timer2.Stop() if stop { fmt.Println("Timer2 stopped before firing") }}Output
Section titled “Output”Waiting...Timer firedTimer2 stopped before firing