Add timers
This commit is contained in:
26
timers/timers.go
Normal file
26
timers/timers.go
Normal 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")
|
||||
}
|
||||
Reference in New Issue
Block a user