关于 Go 语言中 const 的问题

本问题来自《Go语言圣经》中 GIF 动画 中的代码,作用是生成一个利萨如图形 GIF。

代码如下:

// Lissajous generates GIF animations of random Lissajous figures.
package main

import (
    "fmt"
    "image"
    "image/color"
    "image/gif"
    "io"
    "math"
    "math/rand"
    "os"
    "reflect"
)

var palette = []color.Color{color.White, color.RGBA{0x6C, 0x9F, 0xB4, 0xff}}

const (
    whiteIndex = 0 // first color in palette
    greenIndex = 1 // second color in palette
)

func main() {
    lissajous(os.Stdout)
}

func lissajous(out io.Writer) {
    const (
        cycles  = 5     // number of complete x oscillator revolutions
        res     = 0.001 // angular resolution
        size    = 100   // image canvas covers [-size..+size]
        nframes = 64    // number of animation frames
        delay   = 8     // delay between frames in 10ms units
    )

    freq := rand.Float64() * 3.0 // relative frequency of y oscillator
    anim := gif.GIF{LoopCount: nframes}
    phase := 0.0 // phase difference
    for i := 0; i < nframes; i++ {
        rect := image.Rect(0, 0, 2*size+1, 2*size+1)
        img := image.NewPaletted(rect, palette)
        for t := 0.0; t < cycles*2*math.Pi; t += res {
            x := math.Sin(t)
            y := math.Sin(t*freq + phase)
            img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5), greenIndex)
        }
        phase += 0.1
        anim.Delay = append(anim.Delay, delay)
        anim.Image = append(anim.Image, img)
    }

    // gif.EncodeAll(out, &anim) // NOTE: ignoring encoding errors

    fmt.Println(reflect.TypeOf(cycles))
}

不输出 gif,执行代码查看 cycles 的类型为 int.

% go run lissajous.go
int

将 cycles 的声明语句改为 var cycles = 5 后出错,这里 cycles 也是 int 型。

% go run lissajous.go
# command-line-arguments
./lissajous.go:44: constant 3.14159 truncated to integer
./lissajous.go:44: invalid operation: t < cycles * 2 * math.Pi (mismatched types float64 and int)

尝试改成 var cycles = 5.0 后,cycles 的类型为 float64,代码能正常运行。

问题

为什么用 const 声明的 intvar 声明的 int 会有差别?

阅读 10.4k
3 个回答

首先感谢 @mugbya 的回答,这里自己完善下回答。

常量| Go 语言圣经 下有这么一段

Go语言的常量有个不同寻常之处。虽然一个常量可以有任意有一个确定的基础类型,例如int或float64,或者是类似time.Duration这样命名的基础类型,但是许多常量并没有一个明确的基础类型。编译器为这些没有明确的基础类型的数字常量提供比基础类型更高精度的算术运算;你可以认为至少有256bit的运算精度。

通过延迟明确常量的具体类型,无类型的常量不仅可以提供更高的运算精度,而且可以直接用于更多的表达式而不需要显式的类型转换。

例子: math.Pi 为无类型的浮点型常量,可以直接用于需要浮点型或复数的地方

var x float32 = math.Pi     // 3.1415927
var y float64 = math.Pi     // 3.141592653589793
var z complex128 = math.Pi  // (3.141592653589793+0i)

const Pi64 float64 = math.Pi // 明确指明类型
var i float32 = Pi64        // error: cannot use Pi64 (type float64) as type float32 in assignment
var j float64 = Pi64        // 3.141592653589793

const 中有这么一句

Go语言 的字面常量更接近我们自然语言中的常量概念,它是无类型的。只要这个常量在相应类型的值域范内,就可以作为该类型的常量,比如上面的常量-12,它可以赋值给int、uint、int32、 int64、float32、float64、complex64、complex128等类型的变量。

const 应该做了更多的事情,难道可以自动转?

我怎么没有出来图形呢?

我把 cycles 的声明语句改为 var cycles = 5 后没错啊
能输出int

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题