A Tour of Go chapter 44
Exercise: Slices
Implement Pic. It should return a slice of length dy, each element of which is a slice of dx 8-bit unsigned integers. When you run the program, it will display your picture, interpreting the integers as grayscale (well, bluescale) values.
The choice of image is up to you. Interesting functions include x^y, (x+y)/2, and x*y.
(You need to use a loop to allocate each uint8 inside the []uint8.)
(Use uint8(intValue) to convert between types.)
Example
package main import "code.google.com/p/go-tour/pic" func Pic(dx, dy int) [][]uint8 { pic := make([][]uint8, dx, dy) for iy := 0; iy < dy; iy++ { row := make([]uint8, dx) for ix := 0; ix < dx; ix++ { row[ix] = uint8((ix + iy) / 2) } pic[iy] = row } return pic } func main() { pic.Show(Pic) }
付言
code.google.com/p/go-tour/picのコードは読まなくとも充分書ける。hintにあるように、行毎に[]uint8
を初期化する必要がある。