Overview
In Go, slicing with three indexes is done using s[i:j:k]
- Length:
j - i
- Capacity:
k - i
Usage
This slicing technique is useful for preventing access to elements beyond a specified range in the backing array
When using a slice without the third index (s[i:j]
), the append
operation may reuse the backing array, modifying its content. By setting the capacity to 1 (e.g., s[2:3:3]
), append
allocates a new backing array, ensuring that the initial array remains unmodified
Consider this example:
Here, the third element of the slice is modified, even though we pass only two elements. To protect the third element we can use full slice expression like so:
Memory Usage
The inaccessible space (elements in the backing array with indices larger than capacity) is not reclaimed by GC