Object-Oriented Programming in GoLang
Last Updated :
22 Jun, 2020
Object-oriented programming is a programming paradigm which uses the idea of “objects” to represent data and methods. Go does not strictly support object orientation but is a lightweight object Oriented language. Object Oriented Programming in Golang is different from that in other languages like C++ or Java due to factors mentioned below:
1. Struct
Go does not support custom types through classes but structs. Structs in Golang are user-defined types that hold just the state and not the behavior. Structs can be used to represent a complex object comprising more than one key-value pairs. We can add functions to the struct that can add behavior to it as shown below:
Example:
package main
import (
"fmt"
)
type Book struct {
name string
author string
pages int
}
func (book Book) print_details(){
fmt.Printf( "Book %s was written by %s." , book.name, book.author)
fmt.Printf( "\nIt contains %d pages.\n" , book.pages)
}
func main() {
book1 := Book{ "Monster Blood" , "R.L.Stine" , 131}
book1.print_details()
book1.name = "Vampire Breath"
book1.pages = 162
book1.print_details()
}
|
Output:
Book Monster Blood was written by R.L.Stine.
It contains 131 pages.
Book Vampire Breath was written by R.L.Stine.
It contains 162 pages.
2. Encapsulation
It means hiding sensitive data from users. In Go, encapsulation is implemented by capitalizing fields, methods, and functions which makes them public. When the structs, fields, or functions are made public, they are exported on a package level. Some examples of public and private members are:
package gfg
// this function is public as
// it begins with a capital letter
func Print_this(){
// implementation
}
// public struct
type Book struct{
// public field
Name string
// private field, only
// available in gfg package
author string
}
3. Inheritance
When a class acquires the properties of its superclass then we can say it is inheritance. Here, subclass/child class are the terms used for the class which acquire properties. For this one, one must use a struct to achieve inheritance in Golang. Here, users have to compose using structs to form the other objects.
4. Interfaces
Interfaces are types that have multiple methods. Objects that implement all the methods of the interface automatically implement the interface, i.e., interfaces are satisfied implicitly. By treating objects of different types in a consistent way, as long as they stick to one interface, Golang implements polymorphism.
Example:
package main
import (
"fmt"
)
type Sport interface{
sportName() string
}
type Human struct {
name string
sport string
}
func (h Human) sportName() string{
return h.name + " plays " + h.sport + "."
}
func main() {
human1 := Human{ "Rahul" , "chess" }
fmt.Println(human1.sportName())
human2 := Human{ "Riya" , "carrom" }
fmt.Println(human2.sportName())
}
|
Output:
Rahul plays chess.
Riya plays carrom.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...