Go Methods: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 45: Line 45:
==<span id='Value_or_Pointer_Receiver'></span>Deciding between Value or Pointer Receiver==
==<span id='Value_or_Pointer_Receiver'></span>Deciding between Value or Pointer Receiver==


The deciding factor should be whether the method is intended to change the state of the receiver.  
The deciding factor should be whether the method intends to change the state of the receiver.  


Use a pointer receiver if you intend to use the method to modify the state of the receiver instance.
If the method is intended to modify the state of the receiver instance, use a pointer receiver. Use a value receiver otherwise. Since in this case the receiver argument is passed by value, the method will not be able to modify the original receiver, intendedly or erroneously.
 
Use a value receiver if the method must not be able to change the state of the receiver instance. Since the receiver argument is passed by value, the original receiver is protected from changes.


Go documentation (<font color=darkkhaki>Link?</font>) recommends to not mix value and pointer receivers when defining methods for a type.
Go documentation (<font color=darkkhaki>Link?</font>) recommends to not mix value and pointer receivers when defining methods for a type.

Revision as of 01:53, 31 August 2024

External

Internal

Overview

Go allows associating arbitrary behavior with built-in or custom types, which contributes to the object-oriented character of the language. Note that Go is not a fully object-oriented language, it misses type inheritance, for example.

Syntactically, the association of the behavior with the type is done by declaring a function, encapsulating the behavior we want to add to the type, and adding a receiver type to its signature:

func (t ReceiverType) FunctionName(parameters, ...) (return_declaration) {
 ...
}

As result of this association, the function becomes a method of the type.

The declaration is identical to that of a regular function, with the exception of the receiver parameter, which precedes the function name. The receiver parameter gives the method's body access to the instance of the associated type. Aside from its special syntactical position, all other aspects the receiver parameter is identical with the regular parameters of the function.

An aspect that has profound implications on the relationship between the method and the type instance is whether the receiver parameter is a value or a pointer. The deciding factor should be whether the method is intended to change the state of the receiver. Use pointer receivers if you intend to let the method modify the state of the receiver instance. See Deciding between Value or Pointer Receiver.

Receiver

The receiver is the type instance we invoke the method on. It is passed to the method as a parameter, which does not differ in any way from the "regular" parameters. As with the regular parameters, the receiver parameter can be a value or a pointer, and it is called a value receiver or a pointer receiver, respectively.

Value Receiver

func (t ReceiverType) MethodName(parameters, ...) (return_declaration) {
   ...
}

A value receiver gives access to the copy of the variable the method is invoked on, inside the method. As such, the original variable cannot be modified by the method.

Pointer Receiver

func (t *ReceiverType) MethodName(parameters, ...) (return_declaration) {
   ...
}

A pointer receiver gives access to the variable the method is invoked on, inside the method. As such, the method may modify the state of the original variable.

Deciding between Value or Pointer Receiver

The deciding factor should be whether the method intends to change the state of the receiver.

If the method is intended to modify the state of the receiver instance, use a pointer receiver. Use a value receiver otherwise. Since in this case the receiver argument is passed by value, the method will not be able to modify the original receiver, intendedly or erroneously.

Go documentation (Link?) recommends to not mix value and pointer receivers when defining methods for a type.

TODO

Continue and deplete Go_Language_Object_Oriented_Programming#Methods.