切片:Go 中的越界错误


package main
import "fmt"
func main() {
    a := make([]int, 5)
    printSlice("a", a)
    b := make([]int, 0, 5)
    printSlice("b", b)
    c := b[1:]
    printSlice("c", c)
}

func printSlice(s string, x []int) {
    fmt.Printf("%s len=%d cap=%d %vn",
        s, len(x), cap(x), x)
}

以上给了我一个越界错误:

a len=5 cap=5 [0 0 0 0 0]
b len=0 cap=5 []
panic: runtime error: slice bounds out of range
goroutine 1 [running]:
main.main()
   /private/var/folders/q_/53gv6r4s0y5f50v9p26qhs3h00911v/T/compile117.go:10 +0x150

为什么用于创建切片c切片的切片表达式会导致错误?

简而言之:问题不在于可以等于或大于 len() 的下限(在切片的情况下,上限由cap()决定(。问题在于上限:它必须大于或等于下限。由于您没有指定上限,因此它默认为 len()(而不是cap()!(,这是0 。并且1不小于或等于0.

规格: 切片表达式:

对于数组或字符串,如果0 <= low <= high <= len(a)索引,则在范围内,否则它们超出范围。对于切片,索引上限是切片容量cap(a)而不是长度。

由于您正在切片,因此在以下情况下,索引在范围内:

0 <= low <= high <= cap(a)

所以这行:

c := b[1:]

无效,因为:

缺少

低索引默认为零;缺少高索引默认为切片操作数的长度

因此,在您的情况下,low = 1high = 0(隐式(,这不满足:

0 <= low <= high <= cap(a)

例如,以下表达式是有效的:

c := b[1:1]        // c len=0 cap=4 []
c := b[1:2]        // c len=1 cap=4 [0]
c := b[1:cap(b)]   // c len=4 cap=4 [0 0 0 0]

最新更新