Object Oriented Programming with Go

Sanchit Ahuja
3 min readMay 13, 2021

Object oriented programming (OOP) is a programming paradigm based on the concepts of Objects which can contain methods and data in form of fields.

Is Go an Object Oriented language? Well the answer can be Yes and No both. Although Go isn’t strictly object oriented but it has some features that can be used to support OOP concepts. Go picked up some parts of Functional Programming and Object Oriented Programming, and combined to make its own programming style.

Some of the popular and commonly used OOP concepts and their implementation in Go are mentioned below.

Classes and Objects

Go doesn’t support the keyword “class” and custom creation of objects through classes.

However Go supports “struct”. “struct” can be used to implement complex objects and we can also include functions within a “struct ”.

Example:

Output: Area of the Rectangle is: 100.000000

Encapsulation

Mechanism of wrapping the data (variables) and code acting on the data (methods) together as a single unit.Encapsulation is often used to hide the internal representation, or state, of an object from the outside.

In Go encapsulation is achieved via packages. Go has two type of identifiers exported and unexported identifiers

Exported identifiers: Identifiers which can be accessed from packages other than the package they are defined in. These identifiers usually start with Capital letter. Capital letter denotes that the given identifier is an exported identifier.

Unexported identifiers: Identifiers which are not accessible from packages other than the packages they are defined in. These identifiers start with small case letter. Small case letter denotes that the given identifier is not be exported to other packages.

Example:

Output: Hello greetings John Wick
Output: cannot refer to unexported name greetings.greetInternal

Inheritance

Inheritance can be defined as a mechanism which allows child objects to acquire all properties and methods of the parent class.

Go doesn’t support Inheritance. Go chose composition over inheritance.

In composition base struct can be embedded into a child struct. This is sometimes called struct embedding

Example:

Output: Number of Edges: 22

Interfaces

Interface is an another way to achieve abstraction. Interface forms a contract between a class implementing it to the rest of the program. It describes minimum capabilities and features of a class.

Interfaces in Go are compact and can be defined as a set of method signatures. It is used to express conceptual similarity between types.

Polymorphism

In simple terms Polymorphism is a concept by which we can perform a single action in different ways.

Go supports Polymorphism through Interfaces.

Example:

Output: Area of Rectangle is: 200.000000  Area of Polygon is: 200.000000  Area of Polygon is: 100.000000

Conclusion

Overall Go implementation is compact and collects important aspects of OOP. It provides enough flexibility to play around with “structs” and “types”.

The intention of this post to provide brief explanation and implementation of popular concepts of Object Oriented Programming in Go.

Sign up to discover human stories that deepen your understanding of the world.

Sanchit Ahuja
Sanchit Ahuja

Written by Sanchit Ahuja

I am a Backend Developer specializing in Software Architecture, Databases & API Development. I also have a great interest in Quantitative trading & Stock Market

No responses yet

Write a response