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

开始-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 MYSQL 多表拼接link
  • ¥15 关于某款2.13寸墨水屏的问题
  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵
  • ¥15 券商软件上市公司信息获取问题
  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了