* peterGo <[email protected]> [170225 22:40]: > On Saturday, February 25, 2017 at 10:21:49 PM UTC-5, Felix Sun wrote: > > https://play.golang.org/p/TmxMmltHGH > > > > package main > > > > import ( > > "fmt" > > ) > > > > func main() { > > var f int = -1 > > fmt.Println("become huge number:", uint(f)) > > fmt.Println("this panic", uint(-1)) > > > > } > > The Go Programming Language Specification https://golang.org/ref/spec > > Conversions https://golang.org/ref/spec#Conversions > > When converting between integer types, if the value is a signed integer, it > is sign extended to implicit infinite precision; otherwise it is zero > extended. It is then truncated to fit in the result type's size. For > example, if v := uint16(0x10F0), then uint32(int8(v)) == 0xFFFFFFF0. The > conversion always yields a valid value; there is no indication of overflow.
And the other side (compile error for uint(-1)): https://golang.org/ref/spec#Constants (in the sixth paragraph) A constant may be given a type explicitly by a constant declaration or conversion, or implicitly when used in a variable declaration or an assignment or as an operand in an expression. It is an error if the constant value cannot be represented as a value of the respective type. In this particular case, you can get around the problem like this: fmt.Println("this works: ", ^uint(^-1)) ...Marvin -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
