Go Language Object Oriented Programming: Difference between revisions
Line 15: | Line 15: | ||
<span id='Structs_as_Receiver_Types'></span><span id='Structs_as_Receiver_Type'></span>The most common pattern for implementing object-orientation is to use a <code>struct</code> as data encapsulation mechanism, optionally declaring fields as [[Go_Packages#Unexported_Members|unexported]], thus preventing direct access to them from outside the package, and then associating the struct with functions, by declaring the struct type as the [[#Receiver_Type|receiver type]] for those functions. The functions become [[#Method|methods]] of the type. This construct is the closest equivalent to a class in other programming languages. The standard dot notation is then used to call a method on the instance of the receiver type. | <span id='Structs_as_Receiver_Types'></span><span id='Structs_as_Receiver_Type'></span>The most common pattern for implementing object-orientation is to use a <code>struct</code> as data encapsulation mechanism, optionally declaring fields as [[Go_Packages#Unexported_Members|unexported]], thus preventing direct access to them from outside the package, and then associating the struct with functions, by declaring the struct type as the [[#Receiver_Type|receiver type]] for those functions. The functions become [[#Method|methods]] of the type. This construct is the closest equivalent to a class in other programming languages. The standard dot notation is then used to call a method on the instance of the receiver type. | ||
Go does offer inheritance, overriding and polymorphism in the language syntax the same way other OOP languages do, but these features are available and can be implemented with a combination of [[Go_Structs#Embedded_Fields|struct field embedding]] and [[Go_Interfaces#Overview|interfaces]]. See: {{Internal|Go_Inheritance_and_Polymorphism#Overview|Go Inheritance and Polymorphism}} | Go does NOT offer inheritance, overriding and polymorphism in the language syntax the same way other OOP languages do, but these features are available and can be implemented with a combination of [[Go_Structs#Embedded_Fields|struct field embedding]] and [[Go_Interfaces#Overview|interfaces]]. See: {{Internal|Go_Inheritance_and_Polymorphism#Overview|Go Inheritance and Polymorphism}} | ||
=<span id='Method'></span>Methods= | =<span id='Method'></span>Methods= |
Revision as of 02:21, 4 November 2024
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 the language. However, Go allows associating data with methods, which what a class in other programming languages really is.
Go provides syntax to associate an arbitrary local type with arbitrary functions, turning them into methods of that type. The local type can be a type alias, a struct
or a function. In this context, "local" means a type that is defined in the same package as the function. That is why a programmer cannot add new methods to built-in types.
The most common pattern for implementing object-orientation is to use a struct
as data encapsulation mechanism, optionally declaring fields as unexported, thus preventing direct access to them from outside the package, and then associating the struct with functions, by declaring the struct type as the receiver type for those functions. The functions become methods of the type. This construct is the closest equivalent to a class in other programming languages. The standard dot notation is then used to call a method on the instance of the receiver type.
Go does NOT offer inheritance, overriding and polymorphism in the language syntax the same way other OOP languages do, but these features are available and can be implemented with a combination of struct field embedding and interfaces. See:
Methods
Encapsulation
Encapsulation in this context is giving access to private data, such as package-internal data or private struct fields, via public methods. This way, access to data is controlled.
Encapsulation can be implemented with package data and with structs, where the "private" fields are declared using identifiers that start with lower cap letters, so they are not visible outside the package. Controlled access to them is given via accessor and mutator methods, or getters and setters, which are functions that have been declared to use the struct as a receiver type:
type Color struct {
color string
}
func (c *Color) Init(color string) {
c.SetColor(color)
}
func (c *Color) Color() string {
return (*c).color
}
func (c *Color) SetColor(color string) {
(*c).color = color
}
...
var c Color
c.Init("blue")
fmt.Println(c.Color()) // prints blue
c.SetColor("red")
fmt.Println(c.Color()) // prints red
Interfaces
Inheritance
Go does not have type inheritance in the same sense a fully featured object-oriented programming language has.
In Java or Python, a type can be at the same time other type. For example, a Dog instance is at the same type an Animal instance, where "Dog" and "Animal" are two distinct types in the type system, and "Dog" inherits "Animal". The language syntax supports this model.
In Go, an instance can have one type and one type only.
Go supports inheritance of behavior, by allowing interface inheritance. Explain this better.