I store some Individual
objects in a slice. Before appending it to the slice I print the name of the Individual
object.
After I have stored
See https://play.golang.org/p/eg8oYDV6Xx p1 and p2 are already addresses, you don't need to their address (address of address), just print p1 and p2 - they'll be the same as you can see.
Two things to fix this:
Person
variables, since they need to implement an interfacetype Individual interface {
GetName() *string
SetName(name string)
}
type Person struct {
name string
}
// Implement functions of the Individual interface
func (p Person) GetName() *string {
return &p.name
}
func (p *Person) SetName(newName string) {
p.name = newName
}
var individuals []Individual
func main() {
person := &Person{name: "Steve"}
fmt.Println(person)
individuals = append(individuals, person) // append Person to slice
p1 := individuals[0] // Retrieve the only Person from slice
p1.SetName("Peter") // Change name
fmt.Println(p1) // Prints "Steve"
fmt.Println(person) // Prints "Steve"
}
Example on Go Playground.
Whenever a method wants to modify the receiver, it must be a pointer to the value; the method must have a pointer receiver.
If a method has a non-pointer receiver, a copy will be made and passed when that method is called. From that point, no matter what you do with the receiver, you can only modify the copy, and not the original.
So in your example:
func (p Person) SetName(newName string) {
name := p.GetName();
*name = newName
}
When SetName()
is called, a copy is made of the Person
. Inside SetName()
you obtain the address of the name
field of the copy, which you modify. (Actually, a copy of the copy, more on this later...)
Solution: use a pointer receiver:
func (p *Person) SetName(newName string) {
name := p.GetName();
*name = newName
}
From this point on, only *Person
implements Individual
, so use a pointer when appending:
individuals = append(individuals, &person)
It's tricky, because after this it sill won't work. Why is that?
It is because the Person.GetName()
still has a non-pointer receiver:
func (p Person) GetName() *string {
return &p.name
}
So when GetName()
is called from SetName()
, a copy will be made again, and GetName()
will return the address of the name
field of the copy, and SetName()
will only modify the copy created for calling GetName()
.
So to make all work, you also have to use pointer receiver for GetName()
:
func (p *Person) GetName() *string {
return &p.name
}
And now it's working, output (try it on the Go Playground):
{Steve}
&{Peter}
{Peter}
But know that the easiest and recommended way is simply:
func (p *Person) SetName(newName string) {
p.name = newName
}
That's all it takes.