FAQ
This doesn't work for me, I'm going bonkers here...

for i, item := range list {
if item.id == listener.id {
// TODO this doesn't remove the item, instead it seems to add one!?!
list = append(list[:i], list[i+1:]...)
break
}
}

For some reason my list here comes out with removed item now *duplicated *instead
of removed!

Should it be this hard to remove an item from a slice? how come we don't
simply have more keywords/functions like append() for other common slice
operations?

I read the SliceTricks page and I am none the wiser...
On Friday, December 2, 2011 4:11:53 PM UTC-6, peterGo wrote:

Paul,

Delete

a = append(a[:i], a[i+1:]...)

Slice Tricks
http://code.google.com/p/go-wiki/wiki/SliceTricks

Peter
On Dec 2, 1:56 pm, Paul Lalonde wrote:
I have a slice that I want to remove an object from in an arbitrary
position. The remove is made hideous by the possibility of removing
the last element:
// Remove the ith element
if i < len(s)-1 {
s = append(s[:i], s[i+1:]...)
} else {
s = s[:i]
}

This seems a tad inelegant. What's the idiom I'm missing?

Thanks,
Paul
--
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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

Discussion Posts

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 1 of 30 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 17, '14 at 3:34a
activeMar 20, '14 at 3:25a
posts30
users14
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase