Thursday, June 13, 2019

Object Oriented paradigm and golang

Welcome back , I hope you enjoy previous article. If you write a code before most of us in academics or enterprizes follow object oriented paradigm. In Java, c# and other languages "class" keyword represent this is an object oriented paradigm. But in golang there is no class keyword exist. "Is Oop Exist?" Generally speaking golang is not object oriented language, however it support oop at some extend. You will see just in a moment.

Methods Working :
First of all in oop there is class who have methods and properties. Methods are used to set properties. If you want to access class you have a reference of our class. Perhaps, in golang oop is little bit different. In go, you have a method or a reference of a struct. Now the question what is method?

Method is just like function, but it has one additional parameter called receiver, which is the same type of struct. Methods usually are fast and efficient. Methods give you direct access to structs reference. 
                             func (res Result) Add(x, y)


What is the receiver? 
          A receiver is an additional parameter that directly access structs fields and apply operation on it. Receiver concept inherit from old oop paradigm. 

package main
import (
"fmt"
)
type Result struct {
y int
}
var res Result
func main() {
res.Add(2, 4)
}
func (res Result) Add(x, y int) {
res.y = y
fmt.Println(x + res.y)
}
                                         Oreo-Time
                                                                                                    Have a great day.



       

No comments:

Post a Comment

Do you support us?

Business address
0x1D24D8f27ea73ff604C7685246bdC6ae55bddaEF

Mighty Roman Empire's trail and err

  The Roman Empire's strength, fuel by economical corridor and best men's for the service but Golden days might be over when corrup...

Achieves