练习1.4
描述:修改 dup2 ,出现重复的行时打印文件名称
代码:
// Dup2 prints the count and text of lines that appear more than once
// in the input. It reads from stdin or from a list of named files.
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
counts := make(map[string]int)
files := os.Args[1:] //命令行go run xx.go [文件名1] [文件名2]....
//判断是否存在文件,如果不存在文件直接从标准命令行输入
if len(files) == 0 {
countLines(os.Stdin, counts)
} else {
for _, arg := range files {
f, err := os.Open(arg)
if err != nil {
fmt.Fprintf(os.Stderr, "dup2: %v\n", err)
continue
}
countLines2(f, counts, arg)
f.Close()
}
}
//打印相同行
for line, n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\n", n, line)
}
}
}
func countLines(f *os.File, counts map[string]int) {
input := bufio.NewScanner(f)
for input.Scan() {
counts[input.Text()]++
}
// NOTE: ignoring potential errors from input.Err()
}
func countLines2(f *os.File, counts map[string]int, arg string) {
input := bufio.NewScanner(f)
for input.Scan() {
counts[input.Text()]++
if counts[input.Text()] > 1 {
fmt.Println("出现重复行,文件名为:", arg)
}
}
// NOTE: ignoring potential errors from input.Err()
}
运行:
练习1.5
描述: 修改前面的Lissajous程序里的调色板,由黑色改为绿色。我们可以用 color.RGBA{0xRR, 0xGG, 0xBB, 0xff} 来得到 #RRGGBB 这个色值,三个十六进制的字符串分别代表红、绿、蓝像素。
代码:
package main
import (
"image"
"image/color"
"image/gif"
"io"
"math"
"math/rand"
"os"
"time"
)
var green = color.RGBA{0x00, 0xFF, 0x00, 0xFF} //生成绿色
var palette = []color.Color{color.White, green} //生成一个slice切片
const (
whiteIndex = 0 // first color in palette
blackIndex = 1 // next color in palette
)
func main() {
// The sequence of images is deterministic unless we seed
// the pseudo-random number generator using the current time.
// Thanks to Randall McPherson for pointing out the omission.
rand.Seed(time.Now().UTC().UnixNano())
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} //struct结构体
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),
blackIndex)
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim) // NOTE: ignoring encoding errors
}
运行:
- 打开cmd(一定得是cmd,powershell会出错)
- 构建exe
go build main.go
main.exe >outGreen.gif
- 得到动图
练习1.6
描述:修改Lissajous程序,修改其调色板来生成更丰富的颜色,然后修改SetColorIndex
的第三个参数,看看显示结果吧。
代码:
package main
import (
"image"
"image/color"
"image/gif"
"io"
"math"
"math/rand"
"os"
"time"
)
var green = color.RGBA{0x00, 0xFF, 0x00, 0xFF} //生成绿色
var red = color.RGBA{0xFF, 0x00, 0x00, 0xFF}
var blue = color.RGBA{0x00, 0x00, 0xFF, 0xFF}
var palette = []color.Color{color.White, green, red, blue} //生成一个slice切片
const (
whiteIndex = 0 // first color in palette
greenIndex = 1 // next color in palette
redIndex = 2
blueIndex = 3
)
func main() {
// The sequence of images is deterministic unless we seed
// the pseudo-random number generator using the current time.
// Thanks to Randall McPherson for pointing out the omission.
rand.Seed(time.Now().UTC().UnixNano())
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} //struct结构体
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)
// 设置随机数种子
rand.Seed(time.Now().UnixNano())
// 生成范围为 {0, 1, 2} 的随机数
colorIndex := rand.Intn(4)
img.SetColorIndex(size+int(x*size+0.5), size+int(y*size+0.5),
uint8(colorIndex))
}
phase += 0.1
anim.Delay = append(anim.Delay, delay)
anim.Image = append(anim.Image, img)
}
gif.EncodeAll(out, &anim) // NOTE: ignoring encoding errors
}
运行:
为了生成更加丰富的颜色,我添加了红绿蓝到调色板,并设置了随机数来选取每一次用哪种颜色。
按照练习1.5运行(时间相对久一点点),得到下图: