FAQ
Okay, I read everyone's replies, and thank you all.

I then went and read the blog article about
slices<http://blog.golang.org/slices>which answered a lot more
questions than the book and other materials I've
read.

I then spent half an hour juggling slices on the playground, getting more
of a feel for it.

It makes more sense to me now. You're right, "deleting" from a slice
doesn't even make sense - deleting from a slice isn't even a thing, because
that's just not how this data structure works.

Given how this data structure does work, I think I figured out a more
meaningful way to do what I need to do while using this data structure the
way it was intended - and let me know if this sounds right or wrong?

So I need to loop over the list of listeners, and I'm expecting some are
going to be removed.

Given that "deleting" a slice would require you to copy and reconstruct the
entire thing every time, I'm thinking you might as well initialize a new
slice *before *you start the iteration - assuming most items are going to
stay (which they are) you can then simply allocate a size equal to the
current number of listeners, then copy every recurring listener to the new
slice as you go.

This should be a lot more efficient than a full copy for every delete?

As a further optimization, I could also keep a count of the number of
once-listeners - if there are no once-listeners, we can skip copying and
reconstruction, and just loop over the listeners, not expecting to make any
changes.

So this will work, I think :-)

So once again, thank you for your patience and your help!

I still have to wonder though... it's great that Go has slices, and it
seems like you can make some clever memory and computational optimizations
that would not be possible with conventional data structures. But sometimes
(and probably most times IRL) programmer hours are a lot more expensive
than clockcycles - it really wouldn't suck to *also *have more
traditional/convenient data structures, for those times when you have to be
task-oriented and raw performance is secondary to, well, gettin' shit
done... ;-)


On Wed, Mar 19, 2014 at 3:39 PM, Dan Kortschak wrote:

Then write a helper:

func delete(l []ListenerList, i int) []ListenerList {
return append(l[:i], l[i+1:]...)
}
On 20/03/2014, at 12:25 AM, "Rasmus Schultz" wrote:

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.
--
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

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 28 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