我目前在做朗朗教程,“数字常量”是精确的。示例代码开始用下面的语句:打印大号
const (
// Create a huge number by shifting a 1 bit left 100 places.
// In other words, the binary number that is 1 followed by 100 zeroes.
Big = 1 << 100
// Shift it right again 99 places, so we end up with 1<<1, or 2.
Small = Big >> 99
)
恒Big
显然是巨大的,我想打印出来,其类型,像这样:
fmt.Printf("%T", Big)
fmt.Println(Big)
不过,我得到以下误差为两条线:
# command-line-arguments ./compile26.go:19: constant 1267650600228229401496703205376 overflows int
我会尝试铸造大一些其它类型,如uint64
,它与同样的错误溢出,或只是转换我吨至一个字符串,而是试图Big.String()
我收到以下错误时:
Big.String undefined (type int has no field or method String)
看来,它的类型是int
,但我不能打印或将其转换到任何东西,它溢出的所有方法。我如何处理这个数字/对象,以及如何打印?
常量在Go是这恰好是是const _not_正常的变量,它们是不同的。 const的概念只在编译期间存在,const是任意的(几乎)精度和大小,并且算术是可能的。当使用const时,它会转换为“正确”类型。这种转换可能会失败,因为您的程序格式不正确。没有(直接,简单)的方式来打印'Big'。 – Volker
可能的重复[如何执行算术常量?](http://stackoverflow.com/questions/38982278/how-does-go-perform-arithmetic-on-constants) – icza