Naive Goffers often ask if Go is an Object-oriented language? The answer to the above question is both Yes and No. The concept of object-oriented programming is to keep the data and logic separate. The first OOP language was Simula in the 1960s, which brought the ideas of class, objects, inheritance, subclass, and much more.
You might not be aware of Simula but have surely heard of inspired languages like Java, C++, etc., which emerged the modern languages like Perl, Python, Objective C, Scala, Javascript, PHP, and more.
Google’s Go is an entirely new and refreshing programming language that merges the old and new concepts. The Golang programming language is a powerful language that can write anything from simple utilities to building flexible and scalable web servers. It does not use classes, but you can attain the object-oriented styles using Go. One can consider Golang as an advanced object-oriented language, that is lightweight, and which borrows its structure from Pascal/Algol/Modula language.
Golang Vs. Other Programming Languages
The big difference between all other advanced programming languages and Go:
- Golang does not have Classes, Objects, Templates, and Exceptions
- It has in-built concurrency and supports garbage-collection
- There is no type-hierarchy in Go
Let us see how OOP is attainable with the Go programming language. We will relate the traditional OOP concept with that identifiable in Golang.
Abstraction (Object) — Struct (User Defined Types)
Abstraction is the hiding of complexities and easing the work function. In object-oriented programming, Classes enable abstraction. An object, which is an instance of a class, stores the types of variables and methods for a particular task.
You don’t find objects of classes in Golang, but you are still able to build user-defined types in memory, using structures (struct). You can create native types of fields like those in real-world or even abstract things like requests, car, loan, table, etc.
Go struct, for your reference, is similar to C struct rather than C++ class. Let us see an example of a Golang named structure:
In the above example code, we have a struct with named Employee, which stores an employee’s first name, age, salary, and last-name.
Hence, for the programmers coming from the OO background, working with the Golang language is going to be very easy as it has a perfect balance between object-oriented and procedural features.
Encapsulation — Packages
Encapsulation, also known as Information hiding in OO concepts, is represented in Go using Packages. Encapsulation is the bundling of data and methods into one unit (class), which is accessible from the public main method using setter and getter methods.
However, Go language doesn’t entertain access modifiers like public, private, etc, but differentiates between them by first letter casing. So uppercase is public, and lowercase is private.
You can access a private symbol inside the package and a public symbol outside its package too. Here is an example of a package in Golang.
Packages in Go follow the initialization of imported packages, level variables, and init() functions.
Inheritance — Composition
One of OOP’s core concepts is Inheritance, where you can set up a hierarchy and derive a class from another class that inherits its properties, attributes, and methods. Well, Golang doesn’t support inheritance, which is an “is-a relationship model” and rather substitutes composition over inheritance.
Using Composition in Go, you can embed nameless structs within other structs, and you can directly access the embedded struct via promotion. Let us see how:
From the above example, you can see that there is no implicit relation between Manager and Employee, though you can call Employee methods using Manager instance.
Polymorphism — Interface
The term polymorphism means varying behavior, and in the context of Object-oriented programing, different types of objects can be accessed through the same interface. A similar concept applies to Go using Interfaces
It is not mandatory to explicitly implement an interface, define the method, and implicitly implement the interface. Here is an example to show interfaces in Go language:
In this manner, interfaces help us attain polymorphism even when it declares only one single method.
Conclusion
We conclude by saying that though Go is not meant to be an Object-oriented language, it is an advanced programming language that attains all the essential features of OOP. Bjarne Stroustrup, a Danish Computer scientist, says, “Anybody who comes to you and says he has a perfect language, is either naive or a salesman”. There is no perfect programming language, it’s upon you to choose the ideal programming language for your upcoming project.
You must read how Golang Web Development is Better than Other Programming Languages in 2020. Compared to other languages, Go has the potential to build faster and better codes for efficient web and mobile applications. Hire Go enthusiasts from us as we are the top Golang development company in the world.