Skip to content

Instantly share code, notes, and snippets.

@patio11

patio11/foo.go Secret

Created August 22, 2016 01:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patio11/bc883d566778c323742432c203e69541 to your computer and use it in GitHub Desktop.
Save patio11/bc883d566778c323742432c203e69541 to your computer and use it in GitHub Desktop.
GoLang's understanding of what timer.Reset() should do does not match mine
package main
import "time"
import "fmt"
func main() {
longTimer := time.NewTimer(1 * time.Second)
time.Sleep(5 * time.Second)
fmt.Println("We agree that longTimer has fired, right? That will become important in the future.")
longTimer.Reset(1 * time.Second)
shortTimer := time.NewTimer(5 * time.Millisecond)
select {
case <-shortTimer.C:
fmt.Println("This is the behavior we expect: 1 second is longer than 5ms, 'by a lot.'")
case <-longTimer.C:
fmt.Println("This is the behavior GoLang delivers: Reset() did not actually reset the timer.")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment