Add tickers

This commit is contained in:
Sangeeth Sudheer 2024-04-29 06:25:37 +05:30
parent f15865a95f
commit 5112447dfb
Signed by: x
GPG Key ID: F6D06ECE734C57D1
2 changed files with 33 additions and 2 deletions

View File

@ -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
View 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")
}