Methods
You can define methods on types. A method is a function with a spetial receiver argument. The receiver appears in its own argument list between the func
keyword and the method name.
1 | import ( |
You can declare a method on non-struct types, too. You can only declare a method with a receiver whose type is defined in the same package as the method. You cannot declare a method with a receiver whose type is defined in another package (which includes the built-in types such as int
).
Methods with pointer receivers can modify the value to which the receiver points (as Scale
does here). Since methods often need to modify their receiver, pointer receivers are more common than value receivers.
1 | type MyFloat float64 |
With a value receiver, the Scale
method operates on a copy of the original Vertex
value. (This is the same behavior as for any other function argument.) The Scale
method must have a pointer receiver to change the Vertex value declared in the main
function.
Function and Method with Pointer
Functions with a pointer argument must take a pointer:
1 | var v Vertex |
Methods with pointer receivers take either a value or a pointer as the receiver when they are called:
1 | var v Vertex |
As a convenience, Go interprets the statement v.Scale(5)
as (&v).Scale(5)
since the Scale method has a pointer receiver.
Functions that take a value argument must take a value of that specific type:
1 | var v Vertex |
Methods with value receivers take either a value or a pointer as the receiver when they are called:
1 | var v Vertex |
In this case, the method call p.Abs()
is interpreted as (*p).Abs()
.
Choose a pointer receiver
There are two reasons to use a pointer receiver.
The first is so that the method can modify the value that its receiver points to.
The second is to avoid copying the value on each method call. This can be more efficient if the receiver is a large struct, for example.
In this example, both Scale
and Abs
are with receiver type *Vertex
, even though the Abs
method needn’t modify its receiver.