How to realize a random dream in a golang

I'm trying to realize a random sleep dream (in the Golang)

r := rand.Intn(10)
time.Sleep(100 * time.Millisecond)  //working 
time.Sleep(r * time.Microsecond)    // Not working (mismatched types int and time.Duration)
+4
source share
1 answer

Match argument types to time.Sleep:

time.Sleep(time.Duration(r) * time.Microsecond)

This works because it time.Durationhas uint64as its base type:

type Duration int64

Docs: https://golang.org/pkg/time/#Duration

+15
source

Source: https://habr.com/ru/post/1679202/


All Articles