GO and the OOP

Go and OOP
Go is not designed as a purely object-oriented programming (OOP) language in the traditional sense, like Java. However, Go includes certain features that allow for the use of OOP principles, but in a simplified and more flexible manner.


Go is not designed as a purely object-oriented programming (OOP) language in the traditional sense, like Java. However, Go includes certain features that allow for the use of OOP principles, but in a simplified and more flexible manner.

Go features related with OOP

Below are the main features related to OOP:

Structs en Lugar de Clases

In Go, the classes doesn’t exist. In his place, Go uses structs to group data.

[gist 3c8780d2b63615414959ce838e84c7f6]

Methods

You can define methods in structs, it are similar to the classes methods.

[gist 04fc3c62ac6d7d06f206f8c1680eddde]

Interfaces

Go has interfaces, which are types that define a set of methods. Structs can implement interfaces simply by defining the methods specified in the interface, without the need for an explicit implementation declaration.

[gist f3db15b7c72e13f149513f4ff6d92b4d]

Compositión over Inheritance

Instead Inheritance, Go promotes Composition. Including structs in other structs to reuse code.

[gist ed2d9fdf3a07f6c338620cd568d16d74]

Key differences with the traditional OOP

Below are mentioned the key differences with the traditional OOP

  1. There’s no classes or Inheritance:
    • Go avoids the class inheritance, a core concept in the traditional OOP. Simplifying the object herarchy avoiding the multiple inheritance problems.
  2. Implicit Interfaces:
    • In Go, an struct implements an interface implicitly if it defines the methods required by the interface, allowing for greather flexibility and reduced copupling.
  3. Focus on Composition:
    • Go favors composition over the inheritance. This means that instead of creating class hierarchies, you can build complex structures by combining smaller structs.

Full Go Exampe

This is an example of a Go code.
[gist 733a28c6c39e76dcc44d3f289a4b8d07]

Conclusion

Although Go is not purely an object-oriented language like Java, provides suffiecient tools to implements OOP principles in a simply and flexible manner. Its focus on the simplicity, composition and concurrency makes it appropiated for certain types of applications, while still allowing for the application of OOP concepts when is needed.

As you see Go don’t use OOP, but is open to use it. So changing the object-oriented mindset to use Go, it should be not difficult when start to learn Go.

Happy Learning!!!

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *