What is a Class and an Object?
Before we can answer whether Go has classes and objects, we first need to define what they are in the context of programming. A class is a model or template that outlines the characteristics and actions of a collection of objects. An object is created as an instance of a class when memory is allocated and its state is initialized at runtime. Classes and objects are key building blocks in classical object-oriented programming languages like Java, C++ or C# that support polymorphism, inheritance, and encapsulation.
Structs in Golang
In Go, classes are replaced by user-defined data types called structs. These structs combine multiple elements or properties and allow for the inclusion of methods – functions that operate on instances of the struct. While structs share some similarities with classes such as inheritance and interfaces, there are also notable differences. For instance, Go does not have a constructor – a unique method that initializes an object’s state at creation, as is typically found in class-based languages.
Interfaces in Golang
Go does have a concept that is like objects: interfaces. An interface is a collection of methods that describe a behavior or functionality, but without specifying how it is implemented. Interfaces provide a way to achieve polymorphism in Go, allowing different types to be used interchangeably if they satisfy the interface’s requirements. This is a powerful concept that enables Go to achieve a high degree of flexibility and extensibility without sacrificing performance or simplicity.
Use of Structs and Interfaces in Golang
The following example illustrates the use of struct and interfaces in Go.
import "fmt"
type Human struct {
name string
}
type Worker struct {
Human
job string
}
func (h Human) Info() {
fmt.Printf("I am %s\n", h.name)
}
func (w Worker) Info() {
fmt.Printf("I am %s. I am a %s.\n", w.name, w.job)
}
type Person interface {
Info()
}
funcmain() {
John := Worker{Human{"John"}, "worker"}
Doe := Human{"Doe"}
John.Info()
Doe.Info()
var i Person
i = John
i.Info()
i = Doe
i.Info()
}
We have created two structs, Human and Worker, in the code above. The functions that print the messages are then created. The structs are called in the main() function of the Person interface. We can see that the Info method has been implemented by both the Worker and the Human, therefore the variable i with the type Person interface will function correctly and print the output.
Output
Go has a unique approach to encapsulation. Encapsulation is accomplished in conventional object-oriented programming by making fields private and making them accessible through getter and setter methods. In Go, encapsulation is achieved by using lowercase field names, which are only visible within the same package. This strategy is more straightforward and unambiguous, which makes it simpler to understand how the code behaves and lowers the possibility of unforeseen side effects.
Go provides a simplified form of polymorphism. Object-oriented programming has a feature called polymorphism that enables objects from various classes to be treated as instances of the same class. Go does not provide the conventional class-based inheritance, but it does support interfaces, which can be used to specify a set of methods that a struct must implement. Go also offers type assertions, which let programmers examine an interface variable’s content and get the underlying concrete value.
Conclusion
Go doesn’t have classes or objects in the classic sense; instead, it uses structs and interfaces, which offer comparable functionality in a more straightforward and lightweight way. Go has gained popularity among developers who prioritize simplicity, readability, and performance by embracing a different philosophy and grammar. Although getting acquainted with Go’s methodology could take some time, its distinctive features and design result in more effective and maintainable code.