Go 101: Methods on Pointers vs. Values
Methods can be declared on both pointers and values. The difference is subtle but important.
type Person struct {
age int
}
// Method's receiver is the value, `Person`.
func (p Person) Age() int {
return p.age
}
// Method's receiver is a pointer, `*Person`.
func (p *Person) SetAge(age int) {
p.age = age
}
In reality, you’d only define getter and setter functions like this if you needed to implement additional logic. In an example like this you’d just make the
Agefield public.