dta43039 2016-03-26 02:42
浏览 340
已采纳

为什么打印出最大整数会导致golang编译错误?

Here is a link to go playground

package main

import "fmt"
import "math"
func main() {
    fmt.Println("Hello, playground")
    fmt.Println(math.MaxUint32)
}

The above code seems to cause

constant 4294967295 overflows int

does fmt.Println convert every number to int automatically?

  • 写回答

1条回答 默认 最新

  • dongpu5600 2016-03-26 02:56
    关注

    The Go Programming Language Specification

    Constants

    An untyped constant has a default type which is the type to which the constant is implicitly converted in contexts where a typed value is required. The default type of an untyped constant is bool, rune, int, float64, complex128 or string respectively, depending on whether it is a boolean, rune, integer, floating-point, complex, or string constant.

    func Println(a ...interface{}) (n int, err error)
    
    fmt.Println(math.MaxUint32)
    

    math.MaxUint32 is an untyped integer constant that defaults to type int in this context, an untyped integer constant argument for a type interface{} parameter.

    int is a signed 32- or 64-bit integer depending on the implementation.

    const (
        MaxInt32  = 1<<31 - 1
        MaxUint32 = 1<<32 - 1
    )
    

    MaxUint32 is greater than MaxInt32.

    if you run go env you should see that you are using a 32-bit architecture, for example, GOARCH="386".

    Don't accept the default 32-bit int type. Use a compatible type conversion. For example, write

    fmt.Println(uint32(math.MaxUint32))
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么