A pointer is an object reference. If we refer to an object by reference and change the data, then the data changes from the original object. If we address by value, then, as it were, we copy the object, if we change its data, the original object will remain the same.
For example, we create Mike. Obviously, the person does not need to copy, so it is better to save the link. Suppose Mike enters the Family and Work structures:
type Person struct { Name string Age int } func (p *Person) GrowUp() { p.Age += 1 } type Family struct { Husband *Person Wife *Person } type Work struct { Director *Person Employees []*Person } func main() { p := &Person{Name: "Mike", Age: 30} f := Family{Husband: p} w := Work{Director: p} }
Those. if you take the age from the husband in the family f and from the director in the company w - it will be 30 years. Then Mike matured p.GrowUp() . And if we again check the age of the husband and the director, we get 31.
func main() { p := &Person{Name: "Mike", Age: 30} f := Family{Husband: p} w := Work{Director: p} p.GrowUp() fmt.Println(f.Husband.Age, w.Director.Age) }
https://play.golang.org/p/Vn_uLZd6zuR
If you remove the pointers, then after changing the age of Mike, the age of the director and the age of the husband will not change - https://play.golang.org/p/lJNW2I9rcC5