Last active
April 19, 2021 16:32
-
-
Save tommylees112/7d53c6a884024946cdba56f116f5fe52 to your computer and use it in GitHub Desktop.
Slice exercises from the Go Tutorial
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Logic from the SO Question here
a[x]
with a[]uint8
slice ofdx
sizea[x][y]