我正在尝试实现随机时间 sleep (在 Golang 中)
r := rand.Intn(10)
time.Sleep(100 * time.Millisecond) //working
time.Sleep(r * time.Microsecond) // Not working (mismatched types int and time.Duration)
最佳答案
将参数类型匹配到time.Sleep
:
r := rand.Intn(10)
time.Sleep(time.Duration(r) * time.Microsecond)
这是因为 time.Duration
有 int64
作为其基础类型:
type Duration int64
关于go - 如何在golang中实现随机 sleep ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44536045/