Skip to content

Instantly share code, notes, and snippets.

@kdastan
Last active December 26, 2019 13:30
Show Gist options
  • Save kdastan/fd753c267ea7dd71f2057971a3007bca to your computer and use it in GitHub Desktop.
Save kdastan/fd753c267ea7dd71f2057971a3007bca to your computer and use it in GitHub Desktop.
Go exercise solution on Golang tour (https://tour.golang.org/moretypes/18)

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.

(You need to use a loop to allocate each []uint8 inside the [][]uint8.)

(Use uint8(intValue) to convert between types.)

package main

import "golang.org/x/tour/pic"

func Pic(dx, dy int) [][]uint8 {

	pic := make([][]uint8, dy)

	for i := range pic {
		pic[i] = make([]uint8, dx)
	}
	
	for i, v := range pic {
		for idx := range v {
			v[idx] = uint8(((idx^i)+2))
		}
	}

	return pic
}

func main() {
	pic.Show(Pic)
}

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