English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Go language supports methods. Go methods are similar to Go functions, but there is one difference, that is, methods contain a receiver parameter. With the help of the receiver parameter, this method can access the receiver's properties. In this case, the receiver can be a struct type or a non-struct type. When creating a method in the code, the receiver and the receiver type must be in the same package. It is not allowed to create a method where the receiver type is already defined in another package, including built-in types like int, string, etc. If you try to do this, the compiler will throw an error.
Syntax:
func(receiver_name Type) method_name(parameter_list)(return_type){ // Code }
Here, you can access the receiver within the method.
In Go language, it allows you to define methods with a receiver of a struct type. You can access this receiver within the method, as shown in the following example:
package main import "fmt" //Author struct type author struct { name string branch string particles int salary int } //Receiver's method 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() { //Initialization values //Author structure res := author{ name: "Sona", branch: "CSE", particles: 203, salary: 34000, } //Invoke method res.show() }
Output:
Author's Name: Sona Branch Name: CSE Published articles: 203 Salary: 34000
In Go language, as long as the type and method definitions exist in the same package, you can use non-struct type receivers to create methods. If they exist in different packages such as int, string, etc., the compiler will throw an error because they are defined in different packages.
package main import "fmt" //Type definition type data int //Define a method //Non-struct type receiver func (d1 data) multiply(d2 data) data { return d1 * d2 } /* //If you try to run this code, //Then the compiler will throw an error func(d1 int)multiply(d2 int)int{ return d1 * d2 } */ func main() { value1 := data(23) value2 := data(20) res := value1.multiply(value2) fmt.Println("Final result: ", res) }
Output:
Final result: 460
In Go language, it allows you to usePointerReceiver creation method. With the help of pointer receivers, if the changes made in the method will reflect in the caller, which is not possible for value receivers.
Syntax:
func (p *Type) method_name(...Type) Type { // Code }
package main import "fmt" // Author struct type author struct { name string branch string particles int } //Method, using an author type receiver func (a *author) show(abranch string) { (*a).branch = abranch } // Main function func main() { //Initialize the author structure res := author{ name: "Sona", branch: "CSE", } fmt.Println("Author's name: ", res.name) fmt.Println("Branch Name (Before): ", res.branch) //Create a pointer p := &res //Invoke the show method 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
It is well known that in Go, when a function has a value parameter, it will only accept the value of the parameter. If you try to pass a pointer to a value function, it will not accept it, and vice versa. However, Go methods can accept both values and pointers, regardless of whether they are defined with a pointer or value receiver. As shown in the following example:
package main import "fmt" // Author struct type author struct { name string branch string } //Pointer method //Receiver of author type func (a *author) show_1(abranch string) { (*a).branch = abranch } //Method with value //Receiver of author type func (a author) show_2() { a.name = "Gourav" fmt.Println("Author's name (Before): ", a.name) } func main() { //Initialization values //Author struct res := author{ name: "Sona", branch: "CSE", } fmt.Println("Branch Name (Before): ", res.branch) //Calling show_1Method //(Pointer method) with value res.show_1("ECE") fmt.Println("Branch Name (After): ", res.branch) //Calling show_2Method //Pointer-based (value method) (&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
Method | Function |
---|---|
It contains a receiver. | It does not contain a receiver. |
It can accept both pointers and values. | It cannot accept both pointers and values at the same time. |
It is possible to define methods with the same name but different types in the program. | Functions with the same name but different types are not allowed in the program. |