Is that really the _best_ way to delete from a slice in Go?
Is that really the _best_ way to delete from a slice in Go?
Yep[0], and it needs a special case to remove the last element of the slice (if the index is user-provided). If you don't care about the slice order you can also swap the element you want to delete with the last element of the slice and shrink the slice by one element. Also fun, it can leak memory.
If the final argument is assignable to a slice type []T, it may be passed unchanged as the value for a ...T parameter if the argument is followed by .... In this case no new slice is created.
https://golang.org/ref/spec#Passing_arguments_to_..._paramet...
(Otherwise, I completely agree that having to use append to remove an element is stupid.)
And why would one even remotely want that?
Unless they seriously think that if the syntax is painful, people will avoid doing it and get better performance...
If you need to delete from the middle of an ordered collection of items then it's better to use a linked or doubly-linked list (unless it's small then it doesn't matter).
So?
> If you need to delete from the middle of an ordered collection of items then it's better to use a linked or doubly-linked list (unless it's small then it doesn't matter).
Meh. Deleting from the middle of an array is one memmove, you don't get 1~2 pointers memory overhead, caches blown and having to deref' n pointers getting to the item to remove in the first place. If your language provides arrays, there are almost no cases where you should reach for linked lists without having seriously benched both cases.
And that's before the consideration that a Go linked list means loss of type safety or a hand-rolled hand-specialized implementation.