In golang, my understanding was that array slice types are references. I've got a problem where golang is acting like it is making copies of the data, rather than passing references around.
https://play.golang.org/p/EfEOMV_wcS
type Temp struct {
Id string `json:"id"`
Lost string `json:"lost"`
}
func makeFoo1() []Temp {
foos := make([]Temp, 0)
foos = append(foos, Temp{Id: "foo"})
return foos
}
func makeFoo2() []Temp {
foos := makeFoo1()
for _, t := range foos {
t.Lost = "lost"
}
return foos
}
func main() {
j, _ := json.Marshal(makeFoo2())
fmt.Printf("%s
", j)
}
The output of this is [{"id":"foo","lost":""}]
How can the assignment t.Lost = "..."
be made in makeFoo2 so it doesn't get lost? Or I guess the real question is how do I ensure the array is a reference and not a value?