Skip to content

Instantly share code, notes, and snippets.

@dz1984
Created March 23, 2014 06:47
Show Gist options
  • Save dz1984/9719631 to your computer and use it in GitHub Desktop.
Save dz1984/9719631 to your computer and use it in GitHub Desktop.
"A Tour of Go"
/*
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.)
*/
package main
import (
"code.google.com/p/go-tour/pic"
"math"
)
func Pic(dx, dy int) [][]uint8 {
result := make([][]uint8,dx)
for x := 0; x < dx; x++ {
result[x] = make([]uint8,dy)
for y := 0; y < dy; y++ {
result[x][y] = uint8(math.Pow(float64(x),2)+math.Pow(float64(y),2))
}
}
return result
}
func main() {
pic.Show(Pic)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment