FAQ
For example I have a struct *Item* in my db, and two function *GetAllItem*which return more than one Item and
*GetLastItem* return one lasted Item.
GetAllItem will return a slice of Item and and error if any
func GetAllItem() ([]Item, error)

So I can return a nil slice if any error occur.
Now is my wonder about GetLastItem:
func GetLastItem() (*Item, error)

So I will able to return a nil Item, but it not analog with GetAllItem, so
should I change the GetAllItem to return a slice of *Item?
func GetAllItem() ([]*Item, error)

The Go GC is great, so we can go around and throw garbage everywhere?
Thanks!

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

  • Adnaan badr at May 26, 2013 at 4:17 pm
    Yes. You need *T if you want to modify it's fields, just T if you only to
    read them.
    "Every thing in Go is referred by value"
    On Sunday, 26 May 2013 21:35:57 UTC+5:30, Nguyên Nguyễn Văn Cao wrote:

    For example I have a struct *Item* in my db, and two function *GetAllItem*which return more than one Item and
    *GetLastItem* return one lasted Item.
    GetAllItem will return a slice of Item and and error if any
    func GetAllItem() ([]Item, error)

    So I can return a nil slice if any error occur.
    Now is my wonder about GetLastItem:
    func GetLastItem() (*Item, error)

    So I will able to return a nil Item, but it not analog with GetAllItem, so
    should I change the GetAllItem to return a slice of *Item?
    func GetAllItem() ([]*Item, error)

    The Go GC is great, so we can go around and throw garbage everywhere?
    Thanks!
    --
    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.
  • Minux at May 26, 2013 at 4:20 pm
    The general problem depends on whether you want the caller to modify the
    Item?
    and in some cases, you have to return a pointer, for example, you can't
    pass sync.Mutex
    by value, so if the struct embeds a Mutex, you have to pass it by reference
    (pointer).

    On Mon, May 27, 2013 at 12:05 AM, Nguyên Nguyễn Văn Cao
    wrote:
    For example I have a struct *Item* in my db, and two function *GetAllItem*which return more than one Item and
    *GetLastItem* return one lasted Item.
    GetAllItem will return a slice of Item and and error if any
    func GetAllItem() ([]Item, error)

    So I can return a nil slice if any error occur.
    Now is my wonder about GetLastItem:
    func GetLastItem() (*Item, error)

    So I will able to return a nil Item, but it not analog with GetAllItem, so
    should I change the GetAllItem to return a slice of *Item?
    func GetAllItem() ([]*Item, error)
    you don't need to modify GetAllItem() to return []*Item for this as you
    could do something like this:
       itms, err := GetAllItem()
       if err != nil {}
       return &itms[len(itms)-1]

    --
    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.
  • Gustavo Niemeyer at May 26, 2013 at 4:32 pm

    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.
  • Adnaan badr at May 26, 2013 at 4:34 pm
    you have to pass it by reference (pointer).
    Yes, and referred by value on the other end.
    I am not saying anything to the contrary.
    On Sunday, 26 May 2013 21:50:23 UTC+5:30, minux wrote:

    The general problem depends on whether you want the caller to modify the
    Item?
    and in some cases, you have to return a pointer, for example, you can't
    pass sync.Mutex
    by value, so if the struct embeds a Mutex, you have to pass it by
    reference (pointer).

    On Mon, May 27, 2013 at 12:05 AM, Nguyên Nguyễn Văn Cao <[email protected]<javascript:>
    wrote:
    For example I have a struct *Item* in my db, and two function *GetAllItem
    * which return more than one Item and *GetLastItem* return one lasted
    Item.
    GetAllItem will return a slice of Item and and error if any
    func GetAllItem() ([]Item, error)

    So I can return a nil slice if any error occur.
    Now is my wonder about GetLastItem:
    func GetLastItem() (*Item, error)

    So I will able to return a nil Item, but it not analog with GetAllItem,
    so should I change the GetAllItem to return a slice of *Item?
    func GetAllItem() ([]*Item, error)
    you don't need to modify GetAllItem() to return []*Item for this as you
    could do something like this:
    itms, err := GetAllItem()
    if err != nil {}
    return &itms[len(itms)-1]
    --
    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.
  • Jon Renner at May 26, 2013 at 4:38 pm
    http://golang.org/doc/faq#pass_by_value

    should help

    --
    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.
  • Chris dollin at May 26, 2013 at 4:41 pm

    On 26 May 2013 17:05, Nguyên Nguyễn Văn Cao wrote:
    The Go GC is great, so we can go around and throw garbage everywhere?
    Thanks!
    You /can/, but you shouldn't.

    Just because the GC will (usually [1]) find unreferenced items and
    make the space available again doesn't mean that you should
    make lots and lots of items unreferenced for it to look for: GC
    isn't free.

    Chris

    [1] Conservative collector is conservative.

    --
    Chris "allusive" Dollin

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

Related Discussions

Discussion Navigation
viewthread | post
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