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