I am trying to create a bunch of struct instance and append to a list after setting some values. This was reusing the variable. This was not working as it turns out golang was returning the same object. This is against what I expect. Is there any rationale for the behavior? What is the solution. Below is the code snippet from goplayground.
package main
import (
"fmt"
)
type a struct {
I int
}
func main() {
b := new(a)
b.I = 10
fmt.Printf("Hello, playground %v p: %p", b, &b)
b = new(a)
b.I = 12
fmt.Printf(" Hello, playground %v p: %p", b, &b)
}
here is the output :
Hello, playground &{10} **p: 0x40c138** Hello, playground &{12} **p: 0x40c138**