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))
}
求助一下大家, 为什么常量的d
在转换为byte
的时候会报错constant 2147483650111 overflows byte
. 而变量e
却会被截取??
go对常量和非常量转换规则不一样,参见https://golang.org/ref/spec#C...。
x->T,简单翻译一下:
A constant value x can be converted to type T if x is representable by a value of T...
常量转换必须要求x能用T类型的一个值来表示,这里T=byte(0-255),显然没法表示2147483650111
A non-constant value x can be converted to type T in any of these cases:
x is assignable to T...
非常量转换只要能够将x赋值给T类型即可,这里截断后可以赋值,符合转换规则