Hi All,
I am new learner to golang. On reading the structs, functions and method topic, Triggered a doubt.
We can pass a struct to a function as a argument than a method. Then why is significance of method in golang?? Can anyone explain with example
In simple terms Curious to know a case where, " A method can perform which is not possible to implement using function." or what benefit with method logic.
Sample code for method or function call using struct
package main
import (
“fmt”
)
type Person struct {
name string
age int
}
// Function takes struct argument
func functionCall(first Person) {
fmt.Println(“Normal Funtion call”, first)
}
//Method call on struct type
func (second Persoan) methodCall() {
fmt.Println(“Method Function call”, second)
}
func main() {
p1 := Person{“satish”, 23}
p1.name = “kumar”
functionCall(p1)
p1.name = “Yes”
p1.methodCall()
}
Output:
Normal Funtion call {kumar 23}
Method Function call {Yes 23}