My solution for the slice exercise on A Tour of Go.

Ankit Wadhwana
1 min readJul 21, 2020

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)/2, x*y, and x^y.

package mainimport (
"golang.org/x/tour/pic"
)
func Pic(dx, dy int) [][]uint8 {
//using make to create a slice of length dy
pic := make([][]uint8, dy)
for i := 0; i < dy; i++ {
//using make to create a slice of length dx
pic[i] = make([]uint8, dx)
}
//creating image
for y, _ := range pic {
for x, _ := range pic[y] {
//choice for image

//pic[y][x] = uint8(x ^ y)
pic[y][x] = uint8(x+y)/2
//pic[y][x] = uint8(x-y)
//pic[y][x] = uint8((x <<uint8(y))/2)
}
}
return pic
}
func main() {
pic.Show(Pic)
}

Output:

Simple and straight forward let me know if there is a better solution (:

--

--