Add timers

This commit is contained in:
2024-04-29 06:15:06 +05:30
parent e55d715892
commit f15865a95f
2 changed files with 30 additions and 2 deletions

26
timers/timers.go Normal file
View File

@@ -0,0 +1,26 @@
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")
}