The code below requires that I use personPtr.age = personPtr.age + 1
. I was thinking I would have to dereference the pointer like so *personPtr.age = *personPtr.age + 1
but the compiler complains about this latter option. Why do I not need to dereference the pointer?
package main
import (
"fmt"
)
type person struct {
name string
age int
}
func happyBirthday(personPtr *person) {
personPtr.age = personPtr.age + 1
}
func main() {
youngMonkey := person{name: "YoungMonkey", age: 0}
happyBirthday(&youngMonkey)
fmt.Println(youngMonkey.age)
}