我正在完成 Go 巡回赛中的练习,但遇到了一个我无法弄清楚的障碍。
我正在做Exercise: Slices
,我收到了这个错误:
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
这是我的代码:
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)
}