FAQ
I create a slice of structs like this:


dirResult := make([]DirCheckType, 0, 0)

for _, dirName := range dirsToCheck {
  r := DirCheckType{}
dirResult = append(dirResult, r)
r.DirName = dirname

// Set more fields in r
// continue statements so the loop doesn't always get to the end

}


Apparently the append() function does a copy because the modifications I
make to the struct don't make it into the slice of structs.

Is it idiomatic in this case to use a slice of pointers to structs or
should I get a reference to the last struct appended to the slice?


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

  • Caleb Spare at Mar 17, 2014 at 9:26 pm
    Yes, it's idiomatic to use a pointer in this case. Function parameters
    are always passed by value in Go. By the way, no need to specify
    capacity to make in this case, or even use make at all.

    var dirResult []*DirCheckType
    ...
    r := new(DirCheckType) // or &DirCheckType{} if you prefer
    dirResult = append(dirResult, r)
    ...

    Another common approach is to declare your slice with the length you
    know it's going to require. Additionally you can set the DirName field
    in the struct literal.

    dirResult := make([]*DirCheckType, len(dirsToCheck))
    for i, name := range dirsToCheck {
       dirResult[i] = &DirCheckType{DirName: name}
    }

    -Caleb

    On Mon, Mar 17, 2014 at 2:15 PM, Dean Schulze wrote:
    I create a slice of structs like this:


    dirResult := make([]DirCheckType, 0, 0)

    for _, dirName := range dirsToCheck {
    r := DirCheckType{}
    dirResult = append(dirResult, r)
    r.DirName = dirname

    // Set more fields in r
    // continue statements so the loop doesn't always get to the end

    }


    Apparently the append() function does a copy because the modifications I
    make to the struct don't make it into the slice of structs.

    Is it idiomatic in this case to use a slice of pointers to structs or should
    I get a reference to the last struct appended to the slice?


    --
    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.
    --
    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.
  • Matthew Kane at Mar 17, 2014 at 9:28 pm
    If you've just appended the item, you know that it's accessible as
    slice[len(slice)-1], so why not mutate that copy?
    On Mon, Mar 17, 2014 at 5:15 PM, Dean Schulze wrote:
    I create a slice of structs like this:


    dirResult := make([]DirCheckType, 0, 0)

    for _, dirName := range dirsToCheck {
    r := DirCheckType{}
    dirResult = append(dirResult, r)
    r.DirName = dirname

    // Set more fields in r
    // continue statements so the loop doesn't always get to the end

    }


    Apparently the append() function does a copy because the modifications I
    make to the struct don't make it into the slice of structs.

    Is it idiomatic in this case to use a slice of pointers to structs or should
    I get a reference to the last struct appended to the slice?


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


    --
    matt kane
    twitter: the_real_mkb / nynexrepublic
    http://hydrogenproject.com

    --
    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.
  • Mohamedh Fazal at Mar 17, 2014 at 9:28 pm
    Have look at this thread
    https://groups.google.com/forum/#!topic/golang-nuts/INedfATw74A

    and you can get rid of that 0, 0 in make
    On Tuesday, March 18, 2014 2:15:21 AM UTC+5, Dean Schulze wrote:

    I create a slice of structs like this:


    dirResult := make([]DirCheckType, 0, 0)

    for _, dirName := range dirsToCheck {
    r := DirCheckType{}
    dirResult = append(dirResult, r)
    r.DirName = dirname

    // Set more fields in r
    // continue statements so the loop doesn't always get to the end

    }


    Apparently the append() function does a copy because the modifications I
    make to the struct don't make it into the slice of structs.

    Is it idiomatic in this case to use a slice of pointers to structs or
    should I get a reference to the last struct appended to the slice?

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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 17, '14 at 9:15p
activeMar 17, '14 at 9:28p
posts4
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase