这段代码是对的,
package main
import "fmt"
const (
Big = 1 << 100
Small = Big >> 99
)
func needInt(x int) int { return x*10 + 1 }
func needFloat(x float64) float64 {
return x * 0.1
}
func main() {
fmt.Println(needInt(Small))
fmt.Println(needFloat(Small))
fmt.Println(needFloat(Big))
}
但是当我添加
fmt.Println(Big)
我遇到了一个错误:
tmp/sandbox042871394/main.go:16: constant 1267650600228229401496703205376 overflows int
我很困惑
const (
Big = 1 << 100
Small = Big >> 99
)
为什么这两行代码没有错误。
答案 0 :(得分:2)
简短回答:Big是无类型整数常量
始终精确计算常量表达式;中间值和常量本身可能要求精度明显大于语言中任何预先声明的类型所支持的精度。以下是法律声明:
const Huge = 1 << 100 // Huge == 1267650600228229401496703205376 (untyped integer constant)
const Four int8 = Huge >> 98 // Four == 4 (type int8)
参考https://golang.org/ref/spec#Constant_expressions
在常量中你没有明确表示它是一个整数,如果说它会失败
const (
Big int = 1 << 100
Small = Big >> 99
)
将显示错误
tmp/sandbox351128854/main.go:9: constant 1267650600228229401496703205376 overflows int
答案 1 :(得分:1)
大小都是无类型的常量。
interface{}
接受android:descendantFocusability="blocksDescendants"
。常量将转换为其默认类型。
默认类型为int。