理解Go类型推断常量使用中的溢出

理解Go类型推断常量使用中的溢出,go,overflow,type-inference,Go,Overflow,Type Inference,在TourOfGo const的例子中,他们写道 非类型化常量采用其上下文所需的类型 但以下程序会引发溢出: package main import "fmt" const Big = 1 << 100 // no overflow here // var Big = 1 << 100 // overflow here func main() { fmt.Printf("big = %T",Big) // causes overflow err

在TourOfGo const的例子中,他们写道

非类型化常量采用其上下文所需的类型

但以下程序会引发溢出:

package main

import "fmt"

const  Big  = 1 << 100 // no overflow here 
// var  Big  = 1 << 100  // overflow here 

func main() {
    fmt.Printf("big = %T",Big) // causes overflow error here
}
主程序包
输入“fmt”

const Big=1只要文本只是一个常量(不在任何地方赋值),它就不必被物化,因此没有错误。编译器等待您在某个地方实际使用它。考虑这一点:

package main

import "fmt"

const  Big  = 1 << 100 // no overflow here 
    var f float64
    f = Big
    fmt.Println(f)
}
主程序包
输入“fmt”

const Big=1需要将const Big转换为实(非const)类型。哪一个Big是整数,但没有足够大的整数类型。最大的整数类型是uint64和大溢出uint64。这就是错误所在。