This seems a tad inelegant. What's the idiom I'm missing?
Thanks,
Paul
This works:
s = append(s[:i], s[i+1:]...)
Recall that s[len(s):] is a zero length slice.
On 12月3日, 上午3时29分, Steven Blenkinsop <steven...@gmail.com> wrote:
Delete
a = append(a[:i], a[i+1:]...)
Slice Tricks
http://code.google.com/p/go-wiki/wiki/SliceTricks
Peter
Works here http://play.golang.org/p/WNd9wmkq9c
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
I don't want to debate this at length, but this really isn't semantic:s2 := append(s1[:i], s1[i+1:]...)Semantic would be a keyword or something else that actually expresses deletion, e.g.:delete s1[i];
delete s1[i];
My point is, when I'm reading through a function that happens to delete something from a slice, I don't want to get distracted by misleading words and lengthy expressions for something is trivial as removing an item from a list.
In general, I'm not too happy with how deleting from a slice needs to happen, but there are a few reasons for it:1. A slice's backing store is just an array. Deleting from an array is actually not trivial in *any* language that has the C definition of arrays - it involves memory allocation (sometimes), copying (most of the time)2. Even if there was a delete convenience operation on slices (ala map's delete), it would be just a wrapper on append anyway - I can't think of any other way to do it for storage that is essentially a C-style array.A slice is a nice abstraction on top of an array - nothing else. It's *not* a linked list (in which deletion would be trivial, yes).Hence, with the less is more approach, Go authors would probably not be keen on adding mode and more runtime functions ala len, cap, delete unless there was no other way. And "append" is kind of nice because it solves more than 1 use case. Your slice "delete" would solve *one* case and again that case can be done with append already.
BTW, before Go 1, the way to delete from a map, if I recall correctly, wasm["key"] = Z, false (Z being the zero value of the map's value types)Now, aren't you glad we at least got delete for maps? :-)