如何乘以整数的持续时间? [英] How to multiply duration by integer?
问题描述
为了测试并发的goroutines,我在函数中添加了一行,以使它随机返回一个时间(最多一秒)
To test concurrent goroutines, I added a line to a function to make it take a random time to return (up to one second)
time.Sleep(rand.Int31n(1000) * time.Millisecond)
然而,当我编译了,我得到了这个错误:
However when I compiled, I got this error
.\crawler.go:49:无效操作:rand.Int31n(1000)* time。毫秒(不匹配类型int32和time.Duration)
.\crawler.go:49: invalid operation: rand.Int31n(1000) * time.Millisecond (mismatched types int32 and time.Duration)
任何想法?我怎么能乘以一个持续时间?
Any ideas? How can I multiply a duration?
推荐答案
int32
and time.Duration
是不同的类型。您需要将 int32
转换为 time.Duration
,例如 time.Sleep( time.Duration(rand.Int31n(1000))* time.Millisecond)
。
int32
and time.Duration
are different types. You need to convert the int32
to a time.Duration
, such as time.Sleep(time.Duration(rand.Int31n(1000)) * time.Millisecond)
.
这篇关于如何乘以整数的持续时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!