3 ms·
There are no immutable arrays. If you have a slice of capacity ten with four elements, you can write a fifth element to it. If you reduce the capacity to four,
by tedunangst 4y ago
There are no immutable arrays. If you have a slice of capacity ten with four elements, you can write a fifth element to it. If you reduce the capacity to four, then a new slice must be allocated to store five elements.
The backing store for a slice is just a pointer. The rule is you don't give people pointers that you don't want them to write to.
- andrewstuart2 4y agoThat's just not true. There are immutable arrays in length. [4]int cannot be appended to. The backing of all slices are array types and array doubling is used for appends that go beyond the capacity. https://go.dev/ref/spec#Array_types https://go.dev/ref/spec#Array_types
- tedunangst 4y agoYeah, that was a mistake.
- yencabulator 4y agoNo array can change its length, the length is part of the type. There really are no "immutable array"s.