Add tickers
This commit is contained in:
parent
f15865a95f
commit
5112447dfb
6
main.go
6
main.go
@ -10,7 +10,8 @@ package main
|
|||||||
// import "git.sangeeth.dev/gobyexample/timeouts"
|
// import "git.sangeeth.dev/gobyexample/timeouts"
|
||||||
// import "git.sangeeth.dev/gobyexample/selectdefault"
|
// import "git.sangeeth.dev/gobyexample/selectdefault"
|
||||||
// import "git.sangeeth.dev/gobyexample/closingchannels"
|
// import "git.sangeeth.dev/gobyexample/closingchannels"
|
||||||
import "git.sangeeth.dev/gobyexample/timers"
|
// import "git.sangeeth.dev/gobyexample/timers"
|
||||||
|
import "git.sangeeth.dev/gobyexample/tickers"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// runes.Runes()
|
// runes.Runes()
|
||||||
@ -23,5 +24,6 @@ func main() {
|
|||||||
// timeouts.Timeouts()
|
// timeouts.Timeouts()
|
||||||
// selectdefault.SelectDefault()
|
// selectdefault.SelectDefault()
|
||||||
// closingchannels.ClosingChannels()
|
// closingchannels.ClosingChannels()
|
||||||
timers.Timers()
|
// timers.Timers()
|
||||||
|
tickers.Tickers()
|
||||||
}
|
}
|
||||||
|
29
tickers/tickers.go
Normal file
29
tickers/tickers.go
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
package tickers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Tickers() {
|
||||||
|
t := time.NewTicker(500 * time.Millisecond)
|
||||||
|
done := make(chan bool)
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-done:
|
||||||
|
return
|
||||||
|
case val := <-t.C:
|
||||||
|
fmt.Printf("tick at %v\n", val)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
|
||||||
|
fmt.Println("Stopping ticking")
|
||||||
|
t.Stop()
|
||||||
|
done <- true
|
||||||
|
fmt.Println("Program exiting")
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user