golang - 关于 Go 语言中 const 的问题
本文介绍了golang - 关于 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
声明的 int
与 var
声明的 int
会有差别?
解决方案
首先感谢 @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
这篇关于golang - 关于 Go 语言中 const 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文