Skip to content

Instantly share code, notes, and snippets.

@krry
Last active April 17, 2020 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krry/34d84a617f0711722b7b601617d077fa to your computer and use it in GitHub Desktop.
Save krry/34d84a617f0711722b7b601617d077fa to your computer and use it in GitHub Desktop.
An answer to the Slices exercise in A Tour of Go
package main
import "golang.org/x/tour/pic"
func Pic(dx, dy int) [][]uint8 {
grid := make([][]uint8, dy)
for y := range grid {
line := make([]uint8, dx)
for x := range line {
line[x] = uint8(y-x)
}
grid[y] = line
// why doesn't this work instead?
// grid = append(grid, line)
}
return grid
}
func main() {
pic.Show(Pic)
}
@ibdimitrov
Copy link

You should move line 6 (line := make([]uint8, dx)) between lines 8 and 9.

@krry
Copy link
Author

krry commented Apr 17, 2020

Righto, @ibdimitrov. Thanks for the heads up.

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