英文:
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)
这是错误信息:
invalid operation: time.Hour / d (mismatched types time.Duration and float64)
英文:
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)
答案1
得分: 6
这行代码:
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
转换为 time.Duration
:
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)
详细了解该主题:
https://stackoverflow.com/questions/42153747/why-does-0-1-0-2-get-0-3-in-google-go/42154298#42154298
英文:
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 the time
package, its type is time.Duration
; and 73.0
is an untyped numeric constant which when used in an expression, will take the appropriate type if possible. Since time.Duration
has int64
as its underlying type, the constant 73.0
can be converted to time.Duration
without loss of precision, so that will happen, and the constant expression time.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 is float64
. So d
will be a variable of type float64
.
The short variable declaration in the next line: time.Hour / d
, here time.Hour
is again a value of type time.Duration
, but you attempt to divide it by a value of type float64
, that is not allowed in Go.
To make it work, you have to explicitly convert d
to time.Duration
:
s := time.Hour / time.Duration(d)
Or d
must be of type time.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 example 73.5
(because time.Duration
has int64
underlying type). In this case you have to convert time.Hour
to float64
, perform the division and convert the result to time.Duration
, e.g.:
d := 73.5
s := time.Duration(float64(time.Hour) / d)
Read more details on the topic:
https://stackoverflow.com/questions/42153747/why-does-0-1-0-2-get-0-3-in-google-go/42154298#42154298
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论