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.
Have a great day.
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 mainOreo-Time
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)
}
Have a great day.
Comments
Post a Comment