3 ms·
> `a = append(a[:i], a[i+1:]...)` Not only is this ugly and unintuitive (“what function do I call to delete an element?”; “append”), according to the linked pa
by pansa2 6y ago
> `a = append(a[:i], a[i+1:]...)`
Not only is this ugly and unintuitive (“what function do I call to delete an element?”; “append”), according to the linked page it can also cause a memory leak?!
- pasabagi 6y agoOn the other hand, it's very clear about what's actually happening when you delete an element of an array. I'm not a C guy, but I figure doing this in C would involve several lines of mmemove, with loads of error checking[1]. So the Go version is about as ergonomic as you can get without obscuring what's going on. [1] - I guess you'd do something like: memmove(a+i+1, a+i, sizeof(element) * (a->len - i)) with a whole load of checks to make sure everything's in order. I think C has the advantage of being utterly explicit about what is actually happening, but I think a higher level thing like append() is kind of nice when you don't want to think hard about, say, inserting an element in the middle of an array.