睡眠时间的分数 [英] Sleeping by Fractions of a Time Duration

查看:196
本文介绍了睡眠时间的分数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我为什么这样做:

  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>

规范:常量



Go博客:常量



为什么0.1 + 0.2在Google Go中得到0.3?

Go如何在常量上执行算术?



< 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 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:

Spec: Constants

The Go Blog: Constants

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆