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
+26.6k Golang : Change a file last modified date and time
+10.5k Golang : Get remaining text such as id or filename after last segment in URL path
+7.1k Golang : Auto-generate reply email with text/template package
+22.8k Golang : Daemonizing a simple web server process example
+13.4k Golang : package is not in GOROOT during compilation
+16.1k Golang : Qt image viewer example
+29.2k Golang : How to redirect to new page with net/http?
+10.8k Golang : Convert a rune to unicode style string \u
+7.2k Golang : How To Use Panic and Recover
+5.7k Golang : Muxing with Martini example
+16.5k Golang : Simple client server example
+6.7k Golang : Ways to recover memory during run time.