Tri*_*ice 2 types inference go
我一直对元编程涉及包和我已经运行到错误constant 9223372036854775807 overflows int时math.MaxInt64和math.MaxUint64显示.
我已将其分为两种情况:
有效
var a int64 = math.MaxInt64
b := interface{}(int64(math.MaxInt64))
Run Code Online (Sandbox Code Playgroud)
无效
a := math.MaxInt64
b := interface{}(math.MaxInt64)
Run Code Online (Sandbox Code Playgroud)
https://play.golang.org/p/U1QDmFbV29
似乎Go没有做正确的类型推断.
这是一个错误还是预期的行为?如果期待,有谁知道为什么?
math.MaxInt64是一个Untyped Constant.数字常量表示任意精度的值,不会溢出.将此值分配给变量时,需要将其转换为数字类型,如果未指定,int则默认使用.
由于intGo中的类型表示体系结构的本机大小,因此在具有32位ints的系统上会溢出.