时间.毫秒 * int 混淆

huangapple go评论120阅读模式
英文:

time.Millisecond * int confusion

问题

在下面的例子中,如果1000是int类型(我认为是的),为什么下面的代码会编译失败?

//成功
time.Sleep(1000 * time.Millisecond)

//失败
var i = 1000
time.Sleep(i * time.Millisecond)
英文:

In the below example, if the 1000's are both int's (which I think they are) why would the bottom fail to compile?

//works
time.Sleep(1000 * time.Millisecond)

//fails
var i = 1000
time.Sleep(i * time.Millisecond)

答案1

得分: 30

【Operators】
运算符将操作数组合成表达式。

比较运算符在其他地方讨论。对于其他二元运算符,除非操作涉及移位或无类型常量,否则操作数类型必须相同。对于仅涉及常量的操作,请参阅常量表达式部分。

除移位操作外,如果一个操作数是无类型常量而另一个操作数不是,则将常量转换为另一个操作数的类型。

例如,使用“*”(乘法)运算符,

package main

import (
	"time"
)

func main() {

	// 正常工作 - 1000 是无类型常量
	// 会被转换为 time.Duration 类型
	time.Sleep(1000 * time.Millisecond)

	// 失败 - v 是 int 类型的变量
	// 与 time.Duration 类型不相同
	var v = 1000
	// 无效操作:i * time.Millisecond(类型不匹配:int 和 time.Duration)
	time.Sleep(v * time.Millisecond)
}

可能的解决方案:
i 转换为 time.Duration(在底层是 int64)。(由 @lcapra 提出)

var i = 1000
time.Sleep(time.Duration(i) * time.Millisecond)
英文:

> Operators
>
> Operators combine operands into expressions.
>
> Comparisons are discussed elsewhere. For other binary operators, the
> operand types must be identical unless the operation involves shifts
> or untyped constants. For operations involving constants only, see the
> section on constant expressions.
>
> Except for shift operations, if one operand is an untyped constant and
> the other operand is not, the constant is converted to the type of the
> other operand.

For example, using the "*" (multiplication) operator,

package main

import (
	"time"
)

func main() {

	// works - 1000 is an untyped constant
	// which is converted to type time.Duration
	time.Sleep(1000 * time.Millisecond)

	// fails - v is a variable of type int
	// which is not identical to type time.Duration
	var v = 1000
	// invalid operation: i * time.Millisecond (mismatched types int and time.Duration)
	time.Sleep(v * time.Millisecond)
}

Possible solution: <br/>
Convert i to time.Duration (which underneath is an int64). (as suggested by @lcapra)

var i = 1000
time.Sleep(time.Duration(i) * time.Millisecond)

答案2

得分: 23

你应该转换为 time.Duration(其底层是 int64 类型)

var i = 1000
time.Sleep(time.Duration(i) * time.Millisecond)

你可以将上述代码翻译为以下内容:

var i = 1000
time.Sleep(time.Duration(i) * time.Millisecond)
英文:

You should convert to time.Duration (which underneath is an int64)

var i = 1000
time.Sleep(time.Duration(i) * time.Millisecond)

答案3

得分: 2

你可以将i定义为常量而不是变量,如果你主要关心给值命名的话:

package main

import (
    "time"
)

func main() {
    // 可行:
    time.Sleep(1000 * time.Millisecond)

    // 也可行:
    const i = 1000
    time.Sleep(i * time.Millisecond)
}

回复链接:https://stackoverflow.com/a/19338130/11630268

英文:

You can make i a constant instead of a variable, if you are primarily interested in naming the value:

package main

import (
    &quot;time&quot;
)

func main() {
    // works:
    time.Sleep(1000 * time.Millisecond)

    // works also:
    const i = 1000
    time.Sleep(i * time.Millisecond)
}

In-reply-to: https://stackoverflow.com/a/19338130/11630268

答案4

得分: 1

Go不会自动为您转换数字类型。据我了解,除非将其定义为数字类型,否则1000不是数字类型。

语言规范中提到:

当在表达式或赋值中混合不同的数字类型时,需要进行转换。例如,int32和int虽然在特定架构上可能具有相同的大小,但它们不是相同的类型。

英文:

Go won't convert numeric types automatically for you. As far as I understand, 1000 isn't a numeric type until defined as one.

The language specification says:

> Conversions are required when different numeric types are mixed in an expression or assignment. For instance, int32 and int are not the same type even though they may have the same size on a particular architecture.

答案5

得分: -2

两个操作数都需要是相同类型的 time.Duration。你可以使用 time.Sleep(v * time.Millisecond)。

英文:

Both operants need to be of the same type time.Duration. You can use time.Sleep(v * time.Millisecond).

huangapple
  • 本文由 发表于 2013年10月13日 02:55:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/19337955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定