Go language support methods. Go methods are similar to Go function with one difference, i.e, the method contains a receiver argument in it. With the help of the receiver argument, the method can access the properties of the receiver. Here, the receiver can be of struct type or non-struct type. When you create a method in your code the receiver and receiver type must be present in the same package. And you are not allowed to create a method in which the receiver type is already defined in another package including inbuilt type like int, string, etc. If you try to do so, then the compiler will give an error.
Syntax:
func(reciver_name Type) method_name(parameter_list)(return_type){
// Code
}
Here, the receiver can be accessed within the method.
Method with struct type receiver
In Go language, you are allowed to define a method whose receiver is of a struct type. This receiver is accessible inside the method as shown in the below example:
Example:
Go
package main
import "fmt"
type author struct {
name string
branch string
particles int
salary int
}
func (a author) show() {
fmt.Println( "Author's Name: " , a.name)
fmt.Println( "Branch Name: " , a.branch)
fmt.Println( "Published articles: " , a.particles)
fmt.Println( "Salary: " , a.salary)
}
func main() {
res := author{
name: "Sona" ,
branch: "CSE" ,
particles: 203 ,
salary: 34000 ,
}
res.show()
}
|
Output:
Author's Name: Sona
Branch Name: CSE
Published articles: 203
Salary: 34000
Method with Non-Struct Type Receiver
In Go language, you are allowed to create a method with non-struct type receiver as long as the type and the method definitions are present in the same package. If they present in different packages like int, string, etc, then the compiler will give an error because they are defined in different packages.
Example:
Go
package main
import "fmt"
type data int
func (d1 data) multiply(d2 data) data {
return d1 * d2
}
func main() {
value1 := data( 23 )
value2 := data( 20 )
res := value1.multiply(value2)
fmt.Println( "Final result: " , res)
}
|
Output:
Final result: 460
Methods with Pointer Receiver
In Go language, you are allowed to create a method with a pointer receiver. With the help of a pointer receiver, if a change is made in the method, it will reflect in the caller which is not possible with the value receiver methods.
Syntax:
func (p *Type) method_name(...Type) Type {
// Code
}
Example:
Go
package main
import "fmt"
type author struct {
name string
branch string
particles int
}
func (a *author) show(abranch string ) {
(*a).branch = abranch
}
func main() {
res := author{
name: "Sona" ,
branch: "CSE" ,
}
fmt.Println( "Author's name: " , res.name)
fmt.Println( "Branch Name(Before): " , res.branch)
p := &res
p.show( "ECE" )
fmt.Println( "Author's name: " , res.name)
fmt.Println( "Branch Name(After): " , res.branch)
}
|
Output:
Author's name: Sona
Branch Name(Before): CSE
Author's name: Sona
Branch Name(After): ECE
Method Can Accept both Pointer and Value
As we know that in Go, when a function has a value argument, then it will only accept the values of the parameter, and if you try to pass a pointer to a value function, then it will not accept and vice versa. But a Go method can accept both value and pointer, whether it is defined with pointer or value receiver. As shown in the below example:
Example:
Go
package main
import "fmt"
type author struct {
name string
branch string
}
func (a *author) show_1(abranch string ) {
(*a).branch = abranch
}
func (a author) show_2() {
a.name = "Gourav"
fmt.Println( "Author's name(Before) : " , a.name)
}
func main() {
res := author{
name: "Sona" ,
branch: "CSE" ,
}
fmt.Println( "Branch Name(Before): " , res.branch)
res.show_1( "ECE" )
fmt.Println( "Branch Name(After): " , res.branch)
(&res).show_2()
fmt.Println( "Author's name(After): " , res.name)
}
|
Output:
Branch Name(Before): CSE
Branch Name(After): ECE
Author's name(Before) : Gourav
Author's name(After): Sona
Difference Between Method and Function
Method |
Function |
It contains a receiver. |
It does not contain a receiver. |
Methods of the same name but different types can be defined in the program. |
Functions of the same name but different type are not allowed to be defined in the program. |
It cannot be used as a first-order object. |
It can be used as first-order objects and can be passed |
Last Updated :
16 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...