Go Methods: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 8: Line 8:


<font size=-1.5>
<font size=-1.5>
  <font color='Green'><b>func</b></font> (t T) <font color=LightSteelBlue>FunctionName</font>(<font color=gray>parameters, ...</font>) (<font color=gray>return_types, ...</font>) {
  <font color='Green'><b>func</b></font> (t T) <font color=SteelBlue>FunctionName</font>(<font color=gray>parameters, ...</font>) (<font color=gray>return_types, ...</font>) {
   ...
   ...
  }
  }

Revision as of 00:33, 31 August 2024

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 (the behavior) and adding to its signature a receiver type (the type).

func (t T) FunctionName(parameters, ...) (return_types, ...) {
 ...
}

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