为什么切片的容量会发生变化?不应该一样吗?

why is the capacity of the slice changing? shouldn't it be the same?

我是新手,这个例子让我感到困惑:

import "fmt"

func main() {
    s := []int{2, 3, 5, 7, 11, 13}
    printSlice(s)

    // Slice the slice to give it zero length.
    s = s[:0]
    printSlice(s)

    // Extend its length.
    s = s[:4]
    printSlice(s)

    // Drop its first two values.
    s = s[2:]
    printSlice(s)
}

func printSlice(s []int) {
    fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s)
}

输出:

len=6 cap=6 [2 3 5 7 11 13]
len=0 cap=6 []
len=4 cap=6 [2 3 5 7]
len=2 cap=4 [5 7]

为什么最后一个容量变成了4个?根据定义,容量是底层数组的长度。

切片的容量是底层数组中元素的数量,从切片中的第一个元素开始计算。

在您的示例中,在最后一种情况下,切片的起始元素为 5,而在基础 arrray({2, 3, 5, 7, 11, 13}) 中,5 的位置为 2。计算容量你应该从索引 2 开始计数。如果你从那个位置开始计数,你将得到正确的容量 4

Slice建立在数组之上,它由指向数组的指针、段的长度和它的容量(段的最大长度)组成。

重点是segment,而不是array

    s = s[2:]

当你去掉前两个值时,s变成了一个切片,指向数组的另一半段,从第三个元素开始,所以它的上限是4

就像你不能做一个s[-1]来指向数组的前一部分一样,那部分也不能算进cap

参考:https://blog.golang.org/slices-intro