FAQ

On Sun, May 26, 2013 at 1:05 PM, Nguyên Nguyễn Văn Cao wrote:
func GetAllItem() ([]Item, error)
(...)
func GetAllItem() ([]*Item, error)
As you might expect, there really isn't a single answer; it depends on
what an Item is, and how the data is expected to be moved/copied in
the algorithms that use it. With []Item, all the data is contiguous in
a single block, which is a significant advantage. But then, it means
that when you append an item to the slice, you're really appending a
copy of it, which isn't always okay. Also, if you do a lot of item
shuffling within that slice or other operations that force copying the
whole item rather than a pointer, it might compromise that initial
advantage.

In the end, as most things doing the best requires context. So, as a
rule of thumb, until you know best I suggest using []*Item whenever
you do need to preserve the original value instead of having a copy of
it, and []Item otherwise. Then, observe and profile to understand what
is going on.
The Go GC is great, so we can go around and throw garbage everywhere?
That's an easy one: no! :-)


gustavo @ http://niemeyer.net

--
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 4 of 7 | next ›
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 26, '13 at 4:06p
activeMay 26, '13 at 4:41p
posts7
users6
websitegolang.org

People

Translate

site design / logo © 2023 Grokbase