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.
type Person struct {
Name string Age int
}
Methods
You can define methods in structs, it are similar to the classes methods.
| func (p Person) Greet() string { | |
| //do something | |
| return "Hello, world" | |
| } |
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.
| type Greeter interface { | |
| Greet() string | |
| } | |
| func PrintGreeting(g Greeter) { | |
| fmt.Println(g.Greet()) | |
| } |
Compositión over Inheritance
Instead Inheritance, Go promotes Composition. Including structs in other structs to reuse code.
| type Employee struct { | |
| Person Position string | |
| } | |
| e := Employee{ | |
| Person: Person{ Name: "John", Age: 30 }, Position: "Developer", } | |
| fmt.Println(e.Greet()) | |
| // Output: Hello, John |

Key differences with the traditional OOP
Below are mentioned the key differences with the traditional OOP
- 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.
- Implicit Interfaces:
- In Go, an
structimplements aninterfaceimplicitly if it defines the methods required by the interface, allowing for greather flexibility and reduced copupling.
- In Go, an
- 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.
| package main | |
| import "fmt" | |
| // Definición de struct | |
| type Person struct { | |
| Name string | |
| Age int | |
| } | |
| // Método asociado a la struct Person | |
| func (p Person) Greet() string { | |
| return "Hello, " + p.Name | |
| } | |
| // Definición de interface | |
| type Greeter interface { | |
| Greet() string | |
| } | |
| // Struct Employee que compone Person | |
| type Employee struct { | |
| Person | |
| Position string | |
| } | |
| // Función que acepta cualquier tipo que implemente la interfaz Greeter | |
| func PrintGreeting(g Greeter) { | |
| fmt.Println(g.Greet()) | |
| } | |
| func main() { | |
| // Crear una instancia de Person | |
| p := Person{Name: "Alice", Age: 25} | |
| // Crear una instancia de Employee | |
| e := Employee{ | |
| Person: Person{Name: "Bob", Age: 30}, | |
| Position: "Developer", | |
| } | |
| // Usar la función con ambas instancias | |
| PrintGreeting(p) | |
| PrintGreeting(e) | |
| } |

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!!!


Leave a comment