go slice capacity
go has a similar way to slice up arrays like python:
s := []int{1,2,3,4,5}
fmt.Printf(s[:4])
// 1, 2, 3, 4go has another concept for array: capacity
my understanding of it is: from the start of the array to the end of the array, the memory slot that has been taken up by the array
The capacity of a slice is the number of elements in the underlying array, counting from the first element in the slice.
when we move the end of the array, it doesn’t affect its capacity, because it’s memory has already been assigned, all the assigned memory is still accessible;
but when we move the start of the array, the previous start position is not accessible anymore, hence it’s capacity decreases.
s := []int{1,2,3,4,5} // length is 5
a := s[:len(s)-1]
b := s[1:]
fmt.Printf("%v, %v\n", cap(a), cap(b))
// result is: 5, 4