Golang type conversion

package main

import (
    "fmt"
)

const (
    c uint64 = 112
    d uint64 = 2147483650111
)

func main() {

    var e uint64 = 2147483650111
    fmt.Println(byte(c), byte(d), byte(e))

}

ask everyone why the constant d returns an error constant 2147483650111 overflows byte when converting to byte . While the variable e is intercepted

Dec.17,2021

go has different conversion rules for constants and nonconstants, see https://golang.org/ref/spec-sharpC..

x-> T, simply translate:
A constant value x can be converted to type T if x is representable by a value of T.
constant conversion must require x to be represented by a value of type T. Here T=byte (0-255) obviously cannot express 2147483650111

.

A non-constant value x can be converted to type T in any of these cases:
x is assignable to T.
non-constant conversion only needs to be able to assign x to T type. Here, it can be assigned after truncation, which conforms to the conversion rules

.
Menu