尝试为 GOARCH 386 进行交叉编译:package mainimport ( "fmt" "math")func main() { i := math.MaxInt64 fmt.Println(i)}建造:env GOARCH=386 go run main.go输出:# command-line-arguments./sample.go:9:4: constant 9223372036854775807 overflows int为什么 vari被推断为 int32 而不是 int64?这是一个众所周知的错误或 go lang 规范吗?
1 回答

小唯快跑啊
TA贡献1863条经验 获得超2个赞
math.MaxInt64定义如下:
const (
MaxInt64 = 1<<63 - 1
)
来自https://blog.golang.org/constants:
Go 中无类型常量的概念意味着所有数字常量,无论是整数、浮点、复数,甚至是字符值,都存在于一种统一的空间中。当我们将它们带入变量、赋值和操作的计算世界时,实际类型很重要。
通过强制编译为 32 位架构,您将int数字的 go 默认类型强制为显式int32- 而在大多数现代 CPUint上,隐式为int64.
顺便说一句,如果您明确使用类型int64(而不是推断的int类型):
var i int64
i = math.MaxInt64
虽然这可能会为 32 位架构“编译” ,但它不会运行:
GOARCH=386 go build -o too_big && ./too_big
./too_big: Bad CPU type in executable
- 1 回答
- 0 关注
- 151 浏览
添加回答
举报
0/150
提交
取消