I can't understand why after defining a struct (sp
) with a struct pointer (&s
), the initial struct (s
) keeps being modified while mutating the latter (sp
).
http://play.golang.org/p/TdcL_QJqfB
type person struct {
name string
age int
}
func main() {
s := person{name: "Sean", age: 50}
fmt.Printf("%p : %g
", &s, s.age)
sp := &s
fmt.Printf("%p : %g
", &sp, sp.age)
sp.age = 51
fmt.Printf("%p : %g
", &sp, sp.age) // yield 51
fmt.Printf("%p : %g
", &s, s.age) // yields 51, but why not 50 ???
}
Output:
0xc0100360a0 : %!g(int=50)
0xc010000000 : %!g(int=50)
0xc010000000 : %!g(int=51)
0xc0100360a0 : %!g(int=51) // why not 50 ???
I'm new to C family language, Go and pointers, so any pointer ( :) ) to the right concept or error would be very kind of you. thanks in advance !