gobyexample/timers/timers.go

27 lines
383 B
Go
Raw Normal View History

2024-04-29 00:45:06 +00:00
package timers
import (
"fmt"
"time"
)
func Timers() {
t := time.NewTimer(time.Second)
go func() {
<-t.C
fmt.Println("Goroutine called after 1 second!")
}()
cancelled := t.Stop()
if cancelled {
fmt.Println("Timer cancelled before it got to yield")
} else {
fmt.Println("Failed to cancel timer")
}
time.Sleep(2 * time.Second)
fmt.Println("Program exiting")
}