October 2015
Beginner to intermediate
400 pages
14h 44m
English
Because calling a function makes a copy of each argument value, if a
function needs to update a variable, or if an argument is so large
that we wish to avoid copying it, we must pass the address of the
variable using a pointer.
The same goes for methods that need to update the
receiver variable: we attach them to the pointer type, such as *Point.
func (p *Point) ScaleBy(factor float64) {
p.X *= factor
p.Y *= factor
}
The name of this method is (*Point).ScaleBy. The parentheses are
necessary; without them, the expression would be parsed as
*(Point.ScaleBy).
In a realistic program, convention dictates that if any method of
Point has a pointer receiver, then all methods of ...