When you write
obj := *p
You copy the value of the structure pointed to by p ( * dereferences p ). It looks like:
var obj me = *p
So, obj is a new variable of type me , which is initialized to the value *p . This causes obj have a different memory address.
Note that obj is of type me , and p is of type *me . But these are separate values. Changing the value of the obj field will not affect the value of this field in p (unless the me structure has a reference type as a field, that is, a slice, map, or channel. See here and here .). If you want to achieve this effect, use:
obj := p // equivalent to: var obj *me = p
Now obj points to the same object as p . They still have different addresses, but they contain the same address of the actual object me .
abhink
source share