Go - math.MaxInt64 和类型推断错误

Go - math.MaxInt64 and Type Inference Error

我一直致力于涉及包的元编程,每当 math.MaxInt64math.MaxUint64 出现时,我就 运行 进入错误 constant 9223372036854775807 overflows int

我把它分为两种情况:

有效

var a int64 = math.MaxInt64
b := interface{}(int64(math.MaxInt64))

无效

a := math.MaxInt64
b := interface{}(math.MaxInt64)

https://play.golang.org/p/U1QDmFbV29

Go 似乎没有进行正确的类型推断。

这是错误还是预期行为?如果期待,有人知道为什么吗?

math.MaxInt64 是一个 Untyped Constant。数字常量表示任意精度的值并且不会溢出。将其赋值给变量时,需要将其转换为数值类型,如果指定none,则默认使用int

由于 Go 中的 int 类型表示您的体系结构的本机大小,这将在具有 32 位 ints 的系统上溢出。