Go Methods: Difference between revisions
Jump to navigation
Jump to search
Line 5: | Line 5: | ||
Go allows associating arbitrary [[Object-Oriented_Programming#Associating_Behavior_with_Types|behavior]] with built-in or custom [[Go_Language#Type|types]], which contributes to the object-oriented character of the language. Note that Go is not a fully object-oriented language, it misses [[Go_Inheritance_and_Polymorphism#Go_Does_Not_Have_Type_Inheritance|type inheritance]], for example. | Go allows associating arbitrary [[Object-Oriented_Programming#Associating_Behavior_with_Types|behavior]] with built-in or custom [[Go_Language#Type|types]], which contributes to the object-oriented character of the language. Note that Go is not a fully object-oriented language, it misses [[Go_Inheritance_and_Polymorphism#Go_Does_Not_Have_Type_Inheritance|type inheritance]], for example. | ||
Syntactically, the association of the behavior with the type is done by declaring a [[Go_Functions#Declaration|function]] | Syntactically, the association of the behavior with the type is done by declaring a [[Go_Functions#Declaration|function]], encapsulating the behavior we want to add to the type, and adding a '''receiver type''' to its signature: | ||
<font size=-1.5> | <font size=-1.5> |
Revision as of 00:35, 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, 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.