Object-Oriented Approach in Go

Özge Büyükaşık
BiTaksi Product & Tech
3 min readMay 13, 2022

Object-Oriented approach is a controversial topic in Go. Some claim Go is not a suitable programming language for OOP while others claim Go can’t be described as suitable for OOP but it has a different object-oriented approach. So let’s talk about the features of OOP and how they work in GO!

Class vs. Struct

Object-oriented approach is based on classes and objects but Go doesn’t support classes. Instead of classes, Go has structs. Structs are user-defined types. Unlike classes, structs hold only the state, not the behavior. Go developers handle this problem through methods.

Method vs Function

Functions are blocks that can take a few parameters and return output according to the parameters. Methods are function struct type that is defined on struct types.

There is an example of a function.

How can we write this logic as a method?

Is 2 equal to 3?:  false

Both code snippets return the same output. isEqual() method provides functionality to number struct. That’s how Go developers simulate classes in OOP.

Inheritance

There is no class hierarchy in Go, which makes Go's OOP approach unusual. Well, how Go developers can simulate the inheritance? The answer is composition. The main difference between inheritance and composition is that inheritance provides is a/an relation between classes but composition provides has a/an relation between structs.

Composition vs. Inheritance

Composition is based on the principle of embedding small objects into larger objects. Bathroom object is embedded into House object in the example above. That means a House object has a Bathroom object.

Encapsulation

We need encapsulation to hide private information from other classes. Some programming languages have private keyword to hide information but Go has not that kind of keyword. Go developers hide private information by naming them with lowercase letters. If a struct, field, or function’s name starts with a lowercase letter, that means it’s private and other packages can’t reach it.

Polymorphism

Polymorphism is the ability to take on several different forms. You can use objects of different classes through their common points. Programming languages can apply polymorphism with interfaces and Go also has interfaces.

According to the example above, all animals can speak but make different sounds. Cat, Dog, and Duck implement Speak() method of the Animal interface.

As a result, we can’t say Go is a pure object-oriented programming language but it allows us to code in object-oriented style through structs, methods, and interfaces. I wish you enjoy reading. If you have any feedback or opinion to discuss, please let me know :).

References

  1. https://www.geeksforgeeks.org/object-oriented-programming-in-golang/
  2. The Go Programming Language by Alan A. A. Donovan and Brian W. Kernighan
  3. https://www.techtarget.com/searchapparchitecture/definition/object-oriented-programming-OOP#:~:text=Object%2Doriented%20programming%20(OOP)%20is%20a%20computer%20programming%20model,has%20unique%20attributes%20and%20behavior.

--

--