为什么在golang中打印出max int会导致编译错误?

这是去游乐场的链接


package main


import "fmt"

import "math"

func main() {

    fmt.Println("Hello, playground")

    fmt.Println(math.MaxUint32)

}

上面的代码似乎导致


constant 4294967295 overflows int

是否会fmt.Println自动将每个数字转换为 int?


SMILET
浏览 287回答 1
1回答

慕桂英4014372

无类型常量具有默认类型,该类型是在需要类型值的上下文中将常量隐式转换为的类型。无类型常量的默认类型分别是 bool、rune、int、float64、complex128 或 string,具体取决于它是 boolean、rune、integer、浮点、complex 或 string 常量。func Println(a ...interface{}) (n int, err error)fmt.Println(math.MaxUint32)math.MaxUint32是int在此上下文中默认为类型的无类型整数常量,是类型interface{}参数的无类型整数常量参数。int 是有符号的 32 位或 64 位整数,具体取决于实现。const (&nbsp; &nbsp; MaxInt32&nbsp; = 1<<31 - 1&nbsp; &nbsp; MaxUint32 = 1<<32 - 1)MaxUint32大于MaxInt32。如果您运行,go env您应该看到您使用的是 32 位架构,例如,GOARCH="386".不要接受默认的 32 位int类型。使用兼容的类型转换。例如,写fmt.Println(uint32(math.MaxUint32))
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go