On Wed, Jan 27, 2021 at 5:13 PM Barry Li <barrya...@gmail.com> wrote:
>
> Some doubts about type conversion in go.
> In the following code, why can it be executed normally after passing the 
> variable?
> The code is an error during the compilation phase.
>
> // The following two lines of code can be executed normally
>     x := uint64(256)
>     fmt.Println(byte(uint64(x))) // normal execution
>
> // The following code cannot be executed normally
>     fmt.Println(byte(uint64(256))) // constant 256 overflows byte
>
> Can anyone help me answer it?

In Go, arithmetic on constant values follows different rules than
arithmetic on variable values.  For example, as you've seen, an
out-of-range conversion is a compilation error for constants but a run
time error for variables.  The same is true of division by zero.  See
https://golang.org/ref/spec#Constant_expressions.

Ian

-- 
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 golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcXCx4E%2B0O4a-ZM7Rc2x%3D-fJmK_UQknePE%2BgkSoDpoAuVA%40mail.gmail.com.

Reply via email to