I've found that you can cast a variable int32 to uint32 but you cannot cast a const int32 to uint32.
See here: https://play.golang.org/p/tDm6B6g5P6u
If line 14 is commented out it works.
Does anyone have an explanation for this?
Thanks!
I've found that you can cast a variable int32 to uint32 but you cannot cast a const int32 to uint32.
See here: https://play.golang.org/p/tDm6B6g5P6u
If line 14 is commented out it works.
Does anyone have an explanation for this?
Thanks!
The expression uint32(ci)
is a constant expression. The spec says this about constant expressions:
The values of typed constants must always be accurately representable as values of the constant type.
A uint32
cannot accurately represent the negative value ci
, therefore this expression results in a compilation error.
Positive values in range are supported. For example, uint32(-ci)
compiles with no error.
The expressions uint32(vi)
and uint32(vc)
are conversions. Conversions betweeen numeric types are allowed, even when there's loss of accuracy.