Go Language Object Oriented Programming: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 6: Line 6:
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 "[[Object-Oriented_Programming#Class|class]]". There is no <code>class</code> keyword in Go. However, Go allows associating data with methods, which what a class in other programming languages really is.  
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 "[[Object-Oriented_Programming#Class|class]]". There is no <code>class</code> keyword in Go. However, Go allows associating data with methods, which what a class in other programming languages really is.  


Go uses <code>struct</code> instead, which is the encapsulation of data and methods, so the <code>struct</code> ends being equivalent to a class. To associate data with methods, we use Go functions and we give the a '''receiver type'''.
Go uses <code>struct</code> instead, which is the encapsulation of data and methods, so the <code>struct</code> 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.


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 [[Object-Oriented_Programming#Polymorphism|polymorphism]].
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 [[Object-Oriented_Programming#Polymorphism|polymorphism]].

Revision as of 01:12, 29 August 2023

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.

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.