a slice is not an array; this is a view of an existing array. This slice is backed up by an array larger than himself. When you define a fragment of an existing slice, you are actually slicing the base array, but the indicated indices refer to the original slice.
This is a sip. We prove this as follows: we will create a slice of zero length, but we will make the main array be larger. When creating a slice with make third parameter sets the size of the base array. The expression make([]int, 0, 2) will select an array of size 2, but it is evaluated as a slice with a zero size.
package main import ("fmt") func main() {
see here . You can use the cap keyword to refer to the size of the array that this slice supports.
The specific code you requested about loops over cap(pq) in the calling context (container / heap / example_test.go, line 90). If you change the code on the call site and try to click another item in the queue, it will panic as you expect. I ... probably wouldn't suggest writing such code. Although the code in the standard library executes, I would be very sour if I found this in my code base. It is generally safer to use the append keyword.
source share