Receivers and Methods as Pointers and Values

In this lesson, you'll study the difference between the value and the pointer type in Go to receive a result or call a method.

Pointer or value as a receiver

The recv is most often a pointer to the receiver-type for performance reasons (because we don’t make a copy of the value, as would be the case with call by value); this is especially true when the receiver type is a struct. Define the method on a pointer type if you need the method to modify the data the receiver points to. Otherwise, it is often cleaner to define the method on a normal value type.

This is illustrated in the following example :

Get hands-on with 1400+ tech skills courses.