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?
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 int
s.