Skip to content

Instantly share code, notes, and snippets.

@dnorton
Created January 6, 2014 23:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnorton/8291832 to your computer and use it in GitHub Desktop.
Save dnorton/8291832 to your computer and use it in GitHub Desktop.
simple solution to Go Tour Slice Exercise
package main
/**
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.)
http://tour.golang.org/#38
**/
import "code.google.com/p/go-tour/pic"
func Pic(dx, dy int) [][]uint8 {
pic_array := make([][]uint8, dy)
for i := 0; i < len(pic_array); i++ {
pic_array[i] = make([]uint8, dx)
for j := range pic_array[i] {
pic_array[i][j] = uint8(i ^ j)
}
}
return pic_array
}
func main() {
pic.Show(Pic)
}
@dnorton
Copy link
Author

dnorton commented Jan 6, 2014

TIL: because of proxy issues, I couldn't download go-tour/pic, so I downloaded the zip and unpacked into $GOPATH/src/code.google.com/p/go-tour/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment