Methods in Golang (original) (raw)
Last Updated : 25 Oct, 2024
Go methods are like functions but with a key difference: they have a receiver argument, which allows access to the receiver's properties. The receiver can be a struct or non-struct type, but both must be in the same package. Methods cannot be created for types defined in other packages, including built-in types like int or string; otherwise, the compiler will raise an error.
**Example:
Go `
package main
import "fmt"
// Defining a struct type person struct { name string age int }
// Defining a method with struct receiver func (p person) display() { fmt.Println("Name:", p.name) fmt.Println("Age:", p.age) }
func main() { // Creating an instance of the struct a := person{name: "a", age: 25}
// Calling the method
a.display()}
`
Syntax
func(receiver_name Type) method_name(parameter_list) (return_type) {
// Code
}
- **Receiver: Can be accessed within the method.
Table of Content
- Methods with Struct Type Receiver
- Methods with Non-Struct Type Receiver
- Methods with Pointer Receiver
- Methods Accepting Both Pointer and Value
Methods with Struct Type Receiver
In Go, you can define a method where the receiver is of a **struct type. The receiver is accessible inside the method. The previous example showcases this approach with a struct type receiver.
Methods with Non-Struct Type Receiver
Go also allows methods with **non-struct type receivers, as long as the receiver's type and method definition are present in the same package. You cannot define a method with a receiver type from another package (e.g., int, string).
Example:
Using the same example above, we can demonstrate a method with a non-struct receiver:
Go `
package main
import "fmt"
// Creating a custom type based on int type number int
// Defining a method with a non-struct receiver func (n number) square() number { return n * n }
func main() { a := number(4) b := a.square()
fmt.Println("Square of", a, "is", b)}
`
Methods with Pointer Receiver
In Go, methods can have **pointer receivers. This allows changes made in the method to reflect in the caller, which is not possible with value receivers.
Syntax:
func (p *Type) method_name(...Type) Type { // Code}
Example:
Go `
package main
import "fmt"
// Defining a struct type person struct { name string }
// Method with pointer receiver to modify data func (p *person) changeName(newName string) { p.name = newName }
func main() { a := person{name: "a"}
fmt.Println("Before:", a.name)
// Calling the method to change the name
a.changeName("b")
fmt.Println("After:", a.name)}
`
Methods Accepting Both Pointer and Value
Unlike functions, Go methods can accept both **value and pointer receivers. You can pass either a pointer or a value, and the method will handle it accordingly.
Example:
Go `
package main import "fmt"
type person struct { name string } // Method with pointer receiver func (p *person) updateName(newName string) { p.name = newName }
// Method with value receiver func (p person) showName() { fmt.Println("Name:", p.name) }
func main() { a := person{name: "a"}
// Calling pointer method with value
a.updateName("b")
fmt.Println("After pointer method:", a.name)
// Calling value method with pointer
(&a).showName()}
`
Output
After pointer method: b Name: b
Difference Between Method and Function
| **Method | **Function |
|---|---|
| Contains a receiver | Does not contain a receiver |
| Methods with the same name but different types can be defined in the program | Functions with the same name but different types are not allowed |
| Cannot be used as a first-order object | Can be used as first-order objects |