-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Go Recipes for Developers
By :
In this recipe, we’ll explore how to choose between a pointer receiver and a value receiver for methods.
In general, use one kind, not both. There are two reasons for this:
If a method modifies the receiver object, use a pointer receiver. If a method does not modify the receiver object, or if the method relies on getting a copy of the receiver object, you can use a value receiver.
If you are implementing an immutable type, in most cases, you should use a value receiver.
If your structures are large, using a pointer receiver will reduce copy overhead. You can find different guidelines on whether or not a structure can be considered large. When in doubt, write a benchmark and measure.
For a type T, if you declare a method using a value receiver...