github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/blog/content/race-detector/timer-fixed.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"math/rand"
     6  	"time"
     7  )
     8  
     9  func main() {
    10  	start := time.Now()
    11  	reset := make(chan bool)
    12  	var t *time.Timer
    13  	t = time.AfterFunc(randomDuration(), func() {
    14  		fmt.Println(time.Now().Sub(start))
    15  		reset <- true
    16  	})
    17  	for time.Since(start) < 5*time.Second {
    18  		<-reset
    19  		t.Reset(randomDuration())
    20  	}
    21  }
    22  
    23  func randomDuration() time.Duration {
    24  	return time.Duration(rand.Int63n(1e9))
    25  }