Golang time.Timer.Reset() function example
package time
Golang time.Timer.Reset() function usage example. Kinda like resetting a time bomb example.
package main
import (
"fmt"
"time"
)
func main() {
durationToBlowUp := time.Duration(5) * time.Second
timer := time.NewTimer(durationToBlowUp)
go func() {
<-timer.C
fmt.Println("Timer blows!")
}()
// instead of 5 seconds, make it faster
// reset bomb to 2 seconds!!!!
reset := timer.Reset(time.Duration(2) * time.Second)
// ok go to sleep and wait for the pop sound
time.Sleep(time.Duration(6) * time.Second)
fmt.Println("Manage to reset timer from 5 to 2 seconds? : ", reset)
}
Output :
Timer blows!
Manage to reset timer from 5 to 2 seconds? : true
SEE ALSO on how to use NewTimer and Stop functions at https://www.socketloop.com/references/golang-time-time-newtimer-function-and-time-timer-type-example
Reference :
See also : Golang time.Time.NewTimer(), Stop() functions and time.Timer type example
Advertisement
Something interesting
Tutorials
+27.9k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+8.2k Golang : Qt splash screen with delay example
+11.2k Golang : Calculate Relative Strength Index(RSI) example
+7.8k Swift : Convert (cast) String to Double
+7.7k Golang : Command line ticker to show work in progress
+30.8k error: trying to remove "yum", which is protected
+15.6k Golang : ROT47 (Caesar cipher by 47 characters) example
+11.5k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+8.3k Swift : Convert (cast) Character to Integer?
+11k Golang : Create S3 bucket with official aws-sdk-go package
+10.2k Golang : How to profile or log time spend on execution?
+37.5k Upload multiple files with Go