doulu6314 2015-03-27 20:47
浏览 454
已采纳

开始-math.MaxInt64和类型推断错误

I've been working on metaprogramming involving packages and I've been running into the error constant 9223372036854775807 overflows int whenever math.MaxInt64 and math.MaxUint64 show up.

I've isolated it into two cases:

Valid

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

Not Valid

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

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

It seems like that Go doesn't do correct type inference.

Is this a bug or expected behavior? And if expect, does anyone know why?

  • 写回答

1条回答 默认 最新

  • doupin1073 2015-03-27 21:26
    关注

    math.MaxInt64 is an Untyped Constant. Numeric constants represent values of arbitrary precision and do not overflow. When you assign this to a variable it needs to be converted to a numeric type, and if none is specified, int is used by default.

    Since the int type in Go represents the native size for your architecture, this will overflow on systems with 32 bit ints.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图