Go 语言之旅练习 #18:切片,索引超出范围

7
我正在完成Go之旅中的练习,但遇到了一个问题,我无法解决。
我正在做练习:切片,但是出现了以下错误:
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)
}

相关 https://dev59.com/j18e5IYBdhLWcg3wssLG - user4466350
2个回答

7

Slices

For a string, array, pointer to array, or slice a, the primary expression

a[low : high]

constructs a substring or slice. The index expressions low and high select which elements appear in the result. The result has indexes starting at 0 and length equal to high - low.

For arrays or strings, the indexes low and high must satisfy 0 <= low <= high <= length; for slices, the upper bound is the capacity rather than the length.

Indexes

A primary expression of the form

a[x]

denotes the element of the array, slice, string or map a indexed by x. The value x is called the index or map key, respectively. The following rules apply:

For a of type A or *A where A is an array type, or for a of type S where S is a slice type:

x must be an integer value and 0 <= x < len(a)

a[x] is the array element at index x and the type of a[x] is
the element type of A

if a is nil or if the index x is out of range, a run-time panic occurs

Making slices, maps and channels

make(T, n)       slice      slice of type T with length n and capacity n
make(T, n, m)    slice      slice of type T with length n and capacity m

y必须是整数值,且0 <= y < len(pixel[]uint8)。x必须是整数值,且0 <= x < len(pixel[][]uint8)。例如:

package main

import "tour/pic"

func Pic(dx, dy int) [][]uint8 {
    pixels := make([][]uint8, dy)
    for y := 0; y < dy; y++ {
        pixels[y] = make([]uint8, dx)
        for x := 0; x < dx; x++ {
            pixels[y][x] = uint8(x * y)
        }
    }
    return pixels
}

func main() {
    pic.Show(Pic)
}

那么make([]uint8, dx)会创建一个包含dx个零的切片?如果是这样,那和容量有什么区别呢?我的意思是,如果我执行make([]uint8, 0, dx),那不是会创建一个可以容纳多达dx个uint8的切片,但目前为空吗? - CaseyB
1
看我的回答。make([]uint8, dx) 等同于 make([]uint8, dx, dx),即 len()=dx 和 cap()=dx,这与 make([]uint8, 0, dx) 不同,即 len()=0 和 cap()=dx。在两种情况下,底层数组都是 cap()=dx 的零值元素。索引必须满足 0 <= i < len(),否则会发生运行时 panic。 - peterSO

-2
package main

import "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++ {
 // append can skip make statement   
 pixels[y] = append(pixels[y],uint8(x*y)) 

     }
}

 return pixels
}

 func main() {
    pic.Show(Pic)
 }

仅提供代码而没有任何描述或解释所做的更改或原因是不太有帮助的。 - Dave C

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接