github.com/spotify/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/blog/content/race-detector/timer.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  	var t *time.Timer
    12  	t = time.AfterFunc(randomDuration(), func() {
    13  		fmt.Println(time.Now().Sub(start))
    14  		t.Reset(randomDuration())
    15  	})
    16  	time.Sleep(5 * time.Second)
    17  }
    18  
    19  func randomDuration() time.Duration {
    20  	return time.Duration(rand.Int63n(1e9))
    21  }