github.com/graybobo/golang.org-package-offline-cache@v0.0.0-20200626051047-6608995c132f/x/blog/content/race-detector/timer-fixed.go (about)

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