《Go程序设计语言》中第一章生成lissajous图形中代码有一处有问题,可能对新手造成困扰。
package main
import (
"bytes"
"image"
"image/color"
"image/gif"
"io"
"io/ioutil"
"math"
"math/rand"
"os"
)
func main() {
rand.Seed(time.Now().UTC().UnixNano())
if len(os.Args) > 1 && os.Args[1] == "web" {
handler := func(w http.ResponseWriter, r *http.Request) {
lissajous(w)
}
http.HandleFunc("/", handler)
log.Fatal(http.ListenAndServe("localhost:8000", nil))
return
}
buf := &bytes.Buffer{}
lissajous(buf)
if err := ioutil.WriteFile("1.gif", buf.Bytes(), 0666); err != nil {
panic(err)
}
}
注意main结尾处所作修改,笔者按照书中代码运行编码生成的GIF无法正常打开,改为直接输出字节文件就可以了,目测是因为windows环境转了一些控制字符。网上没有看到相关问题,特此说明望后来者能少浪费一些时间。