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 :

http://golang.org/pkg/time/#Timer.Reset

  See also : Golang time.Time.NewTimer(), Stop() functions and time.Timer type example

Advertisement