A slice is an abstraction that uses an array under covers.
cap
indicates the capacity of the underlying array. len
reports how many elements are in the array.
Go , , Go , .
:
s := make([]int, 0, 3)
for i := 0; i < 5; i++ {
s = append(s, i)
fmt.Printf("cap %v, len %v, %p\n", cap(s), len(s), s)
}
- :
cap 3, len 1, 0x1040e130
cap 3, len 2, 0x1040e130
cap 3, len 3, 0x1040e130
cap 8, len 4, 0x10432220
cap 8, len 5, 0x10432220
, , append
. 4- .
, , .