spec: unclear definition of the capacity of a slice #48036
Labels
NeedsDecision
Feedback is required from experts, contributors, and/or the community before a change can be made.
Milestone
What version of Go are you using (
go version
)?https://golang.org/ref/spec says: Version of Jul 26, 2021
What did you do?
After a few years of Go experience, I decided to read the spec again.
When I got to the section about slices, I stumbled upon these sentences:
The second sentence seemed wrong to me, so I tried it using go1.16:
https://play.golang.org/p/dkHHwZ0vqmc
In the above code, the slice starts at
&arr[1]
, it has length 1 and capacity 2.The "length of the array beyond the slice" probably refers to "beyond len(slice)". If so, I assume it to be 3.
The sum of these is 1 + 3 == 4, but the capacity of the underlying array is 5.
The spec says the capacity should be 4, but in reality it is 2.
Is the spec wrong in this regard, or did I just interpret it wrong?
In the first sentence, I interpreted that the word "beyond" means "past the end", ignoring the portion of the underlying array that is before the beginning of the slice.
The text was updated successfully, but these errors were encountered: