I want to convert uint8
to int
, so I write a const 0xfc
, and try to use int8(0xfc)
to convert it. However the code raises an error:
package main
import (
"fmt"
)
func main() {
a := int8(0xfc) // compile error: constant 252 overflows int8
b := a
fmt.Println(b)
}
But if I defer the type conversion after assignment, the code can work around.
package main
import (
"fmt"
)
func main() {
a := 0xfc
b := int8(a) // ok
fmt.Println(b)
}
My question:
- Is there any difference between these two codes?
- Why does the first one raise a compile error?