Skip to content

Instantly share code, notes, and snippets.

@tommylees112
Last active April 19, 2021 16:32
Show Gist options
  • Save tommylees112/7d53c6a884024946cdba56f116f5fe52 to your computer and use it in GitHub Desktop.
Save tommylees112/7d53c6a884024946cdba56f116f5fe52 to your computer and use it in GitHub Desktop.
Slice exercises from the Go Tutorial
package main
import "golang.org/x/tour/pic"
func Formula(x int, y int) uint8 {
// return uint8((x + y) / 2)
return uint8(x * y)
// return uint8(x ^ y)
}
func Pic(dx, dy int) [][]uint8 {
// initialise the picture frame of length dy
mypic := make([][]uint8, dy)
for x := 0; x < dx; x++ {
// make a new slice of length dy
mypic[x] = make([]uint8, dy)
for y := 0; y < dy; y++ {
value := Formula(x, y)
// assign values to that location in mypic
mypic[x][y] = value
}
}
return mypic
}
func main() {
pic.Show(Pic)
}
@tommylees112
Copy link
Author

Logic from the SO Question here

  • outer loop to assign a[x] with a []uint8 slice of dx size
  • an inner loop to assign each value a[x][y]

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