Add timers
This commit is contained in:
parent
e55d715892
commit
f15865a95f
6
main.go
6
main.go
@ -9,7 +9,8 @@ package main
|
||||
// import "git.sangeeth.dev/gobyexample/cselect"
|
||||
// import "git.sangeeth.dev/gobyexample/timeouts"
|
||||
// import "git.sangeeth.dev/gobyexample/selectdefault"
|
||||
import "git.sangeeth.dev/gobyexample/closingchannels"
|
||||
// import "git.sangeeth.dev/gobyexample/closingchannels"
|
||||
import "git.sangeeth.dev/gobyexample/timers"
|
||||
|
||||
func main() {
|
||||
// runes.Runes()
|
||||
@ -21,5 +22,6 @@ func main() {
|
||||
// cselect.Select()
|
||||
// timeouts.Timeouts()
|
||||
// selectdefault.SelectDefault()
|
||||
closingchannels.ClosingChannels()
|
||||
// closingchannels.ClosingChannels()
|
||||
timers.Timers()
|
||||
}
|
||||
|
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")
|
||||
}
|
Loading…
Reference in New Issue
Block a user