blogs

Object-oriented Programming in Go

Posted 29 June 2022

In Go does not have class but normally use struct data type to define a reusable code, can add properties and Functions

Properties

Can define properties with struct

type Book struct {
   ID            int 
   Title         string 
   Author        string 
   YearPublished int 
}

Class Constructor

And also Go does not have concept of constructor. Normally follow specific method create class constructor with adding New keyword before name of the struct and return initialized struct

func NewBook(id int, title, author string, yearPublished int) Book {
   return Book{
      ID:            id,
      Title:         title,
      Author:        author,
      YearPublished: yearPublished,
   }
}

Functions

Can bind a function with struct. It does not define inside the struct but outside of the struct as below. Struct function bit different its not usual function definition.

Normal function define as below

func author(b Book) string {
   return b.Author
}

But struct function define as below

func (b *Book) getAuthor() string {
   return b.Author
}

Notice: in struct function you need to pass struct with pointer. if you don’t struct does not maintain state when update struct data type instead of it create copy of struct and update it. For Example

book := books.NewBook(5, "GO OOP", "Jhon Doe", 2022)
fmt.Println(book)

/*** It prints values
ID: 5
Title: "GO OOP"
Author: "Jhon Doe"
Published: 2022

/****
// When you change Author 
book.SetAuthor("Maria")
fmt.Println(book)

/****It prints values sames as above 
ID: 5
Title: "GO OOP"
Author: "Jhon Doe"
Published: 2022

/****

Because it update copy of book data instead of existing struct in memory. So it worse it consume more memory and every time create copy of data. If you need to get work  above one

book := books.NewBook(5, "GO OOP", "Jhon Doe", 2022)
fmt.Println(book)
book = book.SetAuthor("Maria")
fmt.Println(book)

You need to update variable every time. Instead of pass * pointer variable inside the struct function it normally update existing struct in memory

Private and Public methods and properties

In Go private methods and properties start with lower case. Public method start with uppercase. Private method and Properties are not protected in struct but it protected in packages.

type Book struct {
   ID            int
   Title         string
   Author        string
   YearPublished int
   ibn           int
}
func (b *Book) setAuthor(author string) {
   b.Author = author
}

Private properties and methods cannot use outside of the package. But can use with in the package

Interfaces

In Go add interface type is set of method signature. able to define as below

type Shop interface {
   Price(price int) Shop
   ShipmentAvailableCountry() []ShipmentAvailableCountry
}

As other languages don’t want implement or extend interface. Instead of implement methods of interface inside the struct data type. For Example Book struct need to implement both function above Price and ShipmentAvailableCountry

When we set a parameter of function or type of variables without implement interface function inside the struct it will throw as below

func GetShop(s Shop) Shop {
   return s
}
books.GetShop(book)

It will throw “cannot use book (variable of type books.Book) as type books.Shop in argument to books.GetShop:
books.Book does not implement books.Shop (missing Price method)

Inheritance and Compositions

In go Does not support inheritance we can’t inherit function in go struct But we can composite function and variables with embedding

type Account struct {
}

func (a *Account) AvailableFunds() float32 {....}

func (a *Account) ProcessPayment(amount float32) bool {...}

type CreditAccount struct {
   Account
}

Now we can call AvailableFunds and ProcessPayment Method through  CreditAccount struct

ca := &CreditAccount{}
funds := ca.AvailableFunds()

 

Polymorphism

Is ability to transparently substitute a family of types that implement a common set of behaviors . We able to do it with implicitly implement interface function it makes Polymorphism in go

 

Conclusion

Golang not as other traditional OOP languages but still full capable of follow Best OOP techniques , SOLID and Better design patterns  as other languages

 

Thanks

 

Leave a Reply

Your email address will not be published. Required fields are marked *