Junior — Middle
Tell us about your practical experience applying object-oriented programming principles in projects.
sobes.tech AI
Answer from AI
In Go, despite the absence of a traditional object-oriented model with inheritance, it is possible to apply the main principles of OOP — encapsulation, polymorphism, and composition.
For example, encapsulation is achieved using structures and methods:
package main
import "fmt"
type User struct {
name string
age int
}
func (u *User) Greet() {
fmt.Printf("Hello, my name is %s and I am %d years old\n", u.name, u.age)
}
func main() {
user := User{name: "Ivan", age: 30}
user.Greet()
}
Polymorphism is achieved through interfaces:
type Greeter interface {
Greet()
}
func SayHello(g Greeter) {
g.Greet()
}
Thus, in my projects, I used structures with methods and interfaces to create a flexible architecture, which facilitated support and extension of the code.