Timers in Go
Timers are important part of any programming languages. They provides async functionality to your applications. We have created a timer that can run 1 million child timers in single timer.
You can create your own timer using tiniyo timer. I am going to add one example of tiniyo timer here:
Get Surendra’s stories in your inbox
Join Medium for free to get updates from this writer.
Example:
package mainimport (
"math/rand"
"sync" timer "github.com/tiniyo/timer"
)var testTimer timer.TiniyoTimertype testTimerData struct {
message string
}var wg sync.WaitGroupfunc testTimerCallbackHandler(data interface{}) error {
//fmt.Println("testTimerCallbackHandler : ", data.(testTimerData).message)
wg.Done()
return nil
}func main() {
testTimer.InitializeTiniyoTimer(testTimerCallbackHandler)
testTimer.Run()
min := 1
max := 5
wg.Add(1)
for i := 0; i < 1000000; i++ {
testTimer.StartTimer(uint64(rand.Intn(max-min)+min), string(i), testTimerData{message: "This is test"})
wg.Add(1)
}
wg.Wait()
}
Above code sample creates 1million timers in a range of 1–5 seconds. It waits for it to expire. Once all the timers expire, main exits.
Visit for more at Tiniyo







