3 ms·
Well, conceptually it means you limit how much you can extend the length of the slice inside the array, not that the array has a different length. For example,
by yiyus 13y ago
Well, conceptually it means you limit how much you can extend the length of the slice inside the array, not that the array has a different length.
For example, we have:
full := []int{0, 1, 2, 3, 4, 5}
half1 := full[:3] // [0, 1, 2]
half2 := full[3:] // [3, 4, 5]
Now, the capacity of half1 is 6, so it can "grow inside" half2, such that:
append(half1, 666) // Now, half2 is [666, 4, 5]
This may happen even when you are not using append, for example you could do: half1 = half1[:5] and get into half2 again.
The new syntax solves this problem such that, after defining half1 as full[:3:3], if you try half1[:5] you will get an error (the length cannot be larger than the capacity) and if you use append, a new array will be allocated, leaving the contents of half2 intact in any case.