import ( "time" "fmt" ) func main() { t := time.NewTimer(time.Second * 5) go func() { <-t.C fmt.Println("Timer has fired!") }() // Stop the timer before it can fire if t.Stop() { fmt.Println("Timer has been stopped") } else { fmt.Println("Timer has already expired and cannot be stopped") } }
import ( "time" "fmt" ) func main() { t := time.NewTimer(time.Millisecond * 500) defer t.Stop() time.Sleep(time.Millisecond * 250) if !t.Stop() { // Timer has already completed fmt.Println("Timer has already expired and cannot be stopped") return } // Timer was stopped before it could fire fmt.Println("Timer was stopped before it could fire") }Package library: `time`.