3 ms·
> how would append even know that somebody else appended to the backing array? As far is it can see that might as well be leftover garbage. Well, the backing a
by majewsky 6y ago
> how would append even know that somebody else appended to the backing array? As far is it can see that might as well be leftover garbage.
Well, the backing array knows its length and capacity, and the slice knows its length, so it's quite trivial to see that there are elements in the array beyond the length of the slice.
EDIT: I'm shocked and appalled by the fact that you're completely right. https://play.golang.org/p/UZSlBMUlc_E https://play.golang.org/p/UZSlBMUlc_E
- masklinn 6y ago> Well, the backing array knows its length and capacity arrays don’t have capacity. The capacity of a slice is the length of its backing array (minus the offset of the slice in the array, which is why slices do need to store a cap separately). > it's quite trivial to see that there are elements in the array beyond the length of the slice. There are always elements in the array beyond the length of the slice (unless the slice is « at capacity »). > EDIT: I'm shocked and appalled by the fact that you're completely right. https://play.golang.org/p/UZSlBMUlc_E https://play.golang.org/p/UZSlBMUlc_E Yup. Go slices are fun. And by fun I mean error-prone. Well most of the trouble really comes from them having double-duty as actual slices and add-hoc pseudo vectors.