原文:  How to multiply duration by integer?

 看到golang项目中的一段代码,

--------------------------------------------------------------------------------------------------

You have to cast it to a correct format Playground.

yourTime := rand.Int31n(1000)
time.Sleep(time.Duration(yourTime) * time.Millisecond)

If you will check documentation for sleep, you see that it requires func Sleep(d Duration) duration as a parameter. Your rand.Int31n returns int32.

The line from the example works (time.Sleep(100 * time.Millisecond)) because the compiler is smart enough to understand that here your constant 100 means a duration. But if you pass a variable, you should cast it.

相关文章:

  • 2021-07-20
  • 2022-12-23
  • 2021-06-09
  • 2022-03-05
  • 2021-12-31
  • 2021-10-09
  • 2021-10-11
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-02-11
  • 2022-12-23
  • 2022-12-23
  • 2021-12-29
  • 2022-12-23
相关资源
相似解决方案