Tour of Go exercise #18: Slices, index out of range

Viewed 11330

I am working through the exercises in the Go tour and I have hit a snag that I can't figure out.

I'm doing Exercise: Slices and I am getting this error:

256 x 256

panic: runtime error: index out of range [0] with length 0

goroutine 1 [running]:
main.Pic(0x100, 0x100)
    /tmp/sandbox1628012103/prog.go:14 +0xcf
golang.org/x/tour/pic.Show(0xc0000001a0)
    /tmp/gopath962180923/pkg/mod/golang.org/x/tour@v0.0.0-20201207214521-004403599411/pic/pic.go:32 +0x28
main.main()
    /tmp/sandbox1628012103/prog.go:25 +0x25

Here is my code:

package main

import (
    "fmt"
    "golang.org/x/tour/pic"
)

func Pic(dx, dy int) [][]uint8 {
    fmt.Printf("%d x %d\n\n", dx, dy)

    pixels := make([][]uint8, 0, dy)

    for y := 0; y < dy; y++ {
        pixels[y] = make([]uint8, 0, dx)

        for x := 0; x < dx; x++ {
            pixels[y][x] = uint8(x * y)
        }
    }

    return pixels
}

func main() {
    pic.Show(Pic)
}
2 Answers
Related