Go Language Object Oriented Programming: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 41: Line 41:
</syntaxhighlight>
</syntaxhighlight>


The receiver instance is passed by value. If you want to modify the receiver instances, use a pointer.
In the example above, the receiver instance is passed by value. The instance the method is invoked on cannot be modified in the method, because the method gets a copy of the receiver type instance.
 
To modify the receiver instance, use a '''pointer receiver type'''.
 
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
func (<receiver_type_parameter_name> *<receiver-type>) <function-name>(...) ... {
func (<receiver_type_parameter_name> *<receiver-type>) <function-name>(...) ... {
Line 50: Line 53:
Example:  
Example:  
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
type Color struct {
  color string
}
func (c *Color) Darken() {
func (c *Color) Darken() {
   (*c).color = "dark " + (*c).color
   (*c).color = "dark " + (*c).color

Revision as of 15:56, 29 August 2023

External

Internal

Overview

Go is an object oriented language, but the object orientation programming model is relatively simple, compared with other object oriented languages. It has been said about Go that is "weakly" object-oriented. Go does not use the term "class". There is no class keyword in Go. However, Go allows associating data with methods, which what a class in other programming languages really is.

Go uses struct instead, which is the encapsulation of data and methods, so the struct ends being equivalent to a class. To associate data with methods, we use Go functions and we give the a receiver type, which is the type that function - now becoming a method - is associated with. When calling a method on an instance of the receiver type, the standard dot notation is used.

Go does not have inheritance, constructors or generics. Inheritance can be replaced to a certain extent by composition, embedding and interface, which support code reuse and polymorphism.

Methods

Functions can be associated with a type, turning them into methods of that type, by declaring a receiver type on the function.

The syntax to declare a value receiver type for a method is:

func (<receiver_type_parameter_name> <receiver-type>) <function-name>(...) ... {
  ...
}

Example:

type Color struct {
  color string
}

func (c Color) GetADarkerShade() string {
  return "dark " + c.color 
}

In the example above, Color is the receiver type, and c is the variable that refers to the particular receiver type instance the method was invoked on.

The invocation of the method on the receiver type instance is done with the usual dot notation:

c := Color{"blue"}
result := c.GetADarkerShade() // result will be assigned with "dark blue"

In the example above, the receiver instance is passed by value. The instance the method is invoked on cannot be modified in the method, because the method gets a copy of the receiver type instance.

To modify the receiver instance, use a pointer receiver type.

func (<receiver_type_parameter_name> *<receiver-type>) <function-name>(...) ... {
  ...
}

Example:

func (c *Color) Darken() {
  (*c).color = "dark " + (*c).color
}

...
c := Color{"blue"}
c.Darken()
fmt.Println(c) // will print "{dark blue}"


Methods can only be associated with receiver types that are defined in the same package as the method. That is why a programmer cannot add new methods to built-in types.