I’m learning fundamentals of Go today but I stumble in this example:
My concern is the ChangeLocation
method still changed the Location
without the pointing into the memory address.
<code> package main
import (
"fmt"
)
type Person struct {
FirstName, LastName, Location string
}
func (p *Person) ChangeLocation(newLocation string) { // Limits the access of this method to a pointer
p.Location = newLocation
}
func main() {
p := Person{
"John",
"Doe",
"Route A",
}
p.ChangeLocation("Route B")
fmt.Println(p) // Outputs {John Doe Route B}
}
</code>
<code> package main
import (
"fmt"
)
type Person struct {
FirstName, LastName, Location string
}
func (p *Person) ChangeLocation(newLocation string) { // Limits the access of this method to a pointer
p.Location = newLocation
}
func main() {
p := Person{
"John",
"Doe",
"Route A",
}
p.ChangeLocation("Route B")
fmt.Println(p) // Outputs {John Doe Route B}
}
</code>
package main
import (
"fmt"
)
type Person struct {
FirstName, LastName, Location string
}
func (p *Person) ChangeLocation(newLocation string) { // Limits the access of this method to a pointer
p.Location = newLocation
}
func main() {
p := Person{
"John",
"Doe",
"Route A",
}
p.ChangeLocation("Route B")
fmt.Println(p) // Outputs {John Doe Route B}
}
Here is the playground
2