睡眠时间的分数 [英] Sleeping by Fractions of a Time Duration
问题描述
有人可以告诉我为什么这样做:
s:= time.Hour / 73.0
fmt.Println (sleeping:,s)
time.Sleep(s)
:
d:= 73.0
s:= time.Hour / d
fmt.Println(sleeping :,s)
time.Sleep(s)
这是错误:
无效操作:time.Hour / d(不匹配类型time.Duration和float64)
s:= time.Hour / 73.0
是短变量声明,其中右侧的表达式为:
time.Hour / 73.0
。
time.Hour
是来自time
包,其类型为time.Duration
;和73.0
是一个无类型数字常量,在表达式中使用时,将尽可能使用适当的类型。由于time.Duration
具有int64
作为其基础类型,常量73.0
可以被转换为time.Duration
而不会丢失精度,所以会发生,并且常量表达式time.Hour / time.Duration (73.0)
将被执行。
另一行:
d:= 73.0
s:= time.Hour / d
第一行是一个短变量声明,其中
d
的类型将从右侧表达式中推断出来,该表达式是一个无类型数字常量(用浮点文字是准确的)。由于需要使用类型,因此将使用其默认类型,即float64
。所以d
将会是float64
类型的变量。
下一行中的短变量声明:
time.Hour / d
,这里time.Hour
也是一个值time.Duration
类型,但您尝试将它除以类型float64
的值,这是不允许的Go。
为了使它工作,你必须明确地将
d
转换为时间。持续时间
:s:= time.Hour / time.Duration(d)
或者
d
必须是time.Duration
:d:= time.Duration(73.0)
s:= time.Hour / d
或者:
var d time.Duration = 73.0
s:= time.Hour / d
如果值不能用
time.Duration
表示,例如73.5
(因为time.Duration
有int64
底层类型)。在这种情况下,您必须将time.Hour
转换为float64
,执行除法并将结果转换为time.Duration
,例如:d:= 73.5
s:= time.Duration(float64(time.Hour)/ d)
阅读关于主题的更多细节: / p>
< a href =https://stackoverflow.com/questions/36052922/golang-converting-float64-to-i nt-error / 36055172#36055172> Golang将float64转换为int错误
Can someone tell me why this works:
s := time.Hour/73.0 fmt.Println("sleeping: ", s) time.Sleep(s)
But this fails:
d := 73.0 s := time.Hour/d fmt.Println("sleeping: ", s) time.Sleep(s)
This is the error:
invalid operation: time.Hour / d (mismatched types time.Duration and float64)
解决方案This line:
s := time.Hour/73.0
Is a short variable declaration, where the right hand side expression is:
time.Hour / 73.0
.time.Hour
is a typed constant from thetime
package, its type istime.Duration
; and73.0
is an untyped numeric constant which when used in an expression, will take the appropriate type if possible. Sincetime.Duration
hasint64
as its underlying type, the constant73.0
can be converted totime.Duration
without loss of precision, so that will happen, and the constant expressiontime.Hour / time.Duration(73.0)
will be executed.The other line:
d := 73.0 s := time.Hour/d
The first line is a short variable declaration, where the type of
d
will be inferred from the right hand side expression, which is an untyped numeric constant (given with a floating point literal to be exact). Since a type is needed, its default type will be used, which isfloat64
. Sod
will be a variable of typefloat64
.The short variable declaration in the next line:
time.Hour / d
, heretime.Hour
is again a value of typetime.Duration
, but you attempt to divide it by a value of typefloat64
, that is not allowed in Go.To make it work, you have to explicitly convert
d
totime.Duration
:s := time.Hour / time.Duration(d)
Or
d
must be of typetime.Duration
:d := time.Duration(73.0) s := time.Hour / d
Or:
var d time.Duration = 73.0 s := time.Hour / d
These wouldn't work if the value cannot be represented with
time.Duration
, for example73.5
(becausetime.Duration
hasint64
underlying type). In this case you have to converttime.Hour
tofloat64
, perform the division and convert the result totime.Duration
, e.g.:d := 73.5 s := time.Duration(float64(time.Hour) / d)
Read more details on the topic:
Why does 0.1 + 0.2 get 0.3 in Google Go?
How does Go perform arithmetic on constants?
Golang converting float64 to int error
这篇关于睡眠时间的分数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!