OOP Principles in Golang
Shariful Islam
???????????? ???????????????? ???????????????? - ???????????? | ???????????? | ???????????? | ?????? | ?????????????? ???????????????? | ????
The object-oriented programming paradigm has 4 core principles
Encapsulation?
Encapsulation keeps data safe from external interfaces. In Golang this is done at the package level.
Golang does not have access modifiers such as public, private, or protected. It has exported and unexported fields. This difference comes from the first letter of variables. Lowercase means unexported and Uppercase means Exported.
type Car struct
id ? int
name string
}
func (c *Car) GetID() int {
return c.id
}
func (c *Car) GetName() string {
return c.name
}
Abstraction
?In Go, abstraction is achieved through interfaces, An interface is a collection of methods. It defines a set of behaviors that any types of implementing
type Car interface {
????Drive() string
????Stop() string
}
type BMW struct {
????Model string
}
func (b BMW) Drive() string {
????return "Driving " + b.Model
}
func (b BMW) Stop() string {
????return "Stopping " + b.Model
}
func DriveCar(c Car) {
????fmt.Println(c.Drive())
????fmt.Println(c.Stop())
}
Inheritance
In OOP, computer programs are designed in such a way that everything is an object that interacts with one another. It basically helps in reusing the code and establishing a relationship between different classes.
type Vehicle struct
Seats int
Color string
}
type Car struct {
Vehicle
}
type MotorCycle struct {
Base Vehicle
}
In the above example, Car struct is being embedded by another structure.
func main()
car := &Car{
Vehicle{
Seats: 4,
Color: "blue",
},
}
fmt.Println(car.Seats)
fmt.Println(car.Color)
motorCycle := &MotorCycle{
Vehicle{
Seats: 2,
Color: "red",
},
}
fmt.Println(motorCycle.Base.Seats)
fmt.Println(motorCycle.Base.Color)
}
Polymorphism
Golang supports polymorphism through an interface.?
type Animal interface
? ? Sound() string
}
type Dog struct {
? ? Name string
}
func (d Dog) Sound() string {
? ? return "Woof!"
}
type Cat struct {
? ? Name string
}
func (c Cat) Sound() string {
? ? return "Meow!"
}
Conclusion
Golang promotes an interesting take on object-oriented programing with the use of interfaces although it is not meant to be an OOP language.
You can follow me on LinkedIn for more articles on Golang
Thanks for a short and clear description. Inheritance has, in my eyes, become a nuisance as it quite often has no real value for the program nor the programmer. In small and specific situations (such as the Vehicle example) it seems reasonable to apply but for more complex designs it brings more confusion than clarity. Often inheritance goes very deep for no good reason other that "support the OOP idea". Finding and generalizing a common trait can often be very artificial. Say you want to model a chair. The only common "chair trait" is "something to sit on". So this includes tables, logs, toilets, low fences, swings, the ground, ... Now the abstraction is almost of no use. Of course this should be implemented by a Sitable interface. And essentially every inheritable parent class could easily and more to the point be broken into one or more small interfaces, removing most need of inheritance. Code-sharing can be handled through other means than through inheritance. The classic OOP approach to inheritance put forward in Java and C# had, after 15 years in Java, led me away from these. Too.many people put too much effort into (deep) inheritance, cluttering up the code for no really good reason.