Go Method Functions – Define Functions on Structs with Receivers (2025 Guide)
Introduction – What Are Method Functions in Go?
In Go, method functions are functions associated with a specific type, usually a struct. Unlike regular functions, methods use a receiver to operate on an instance of a type—similar to methods in OOP languages, but simpler and more explicit.
In this section, you’ll learn:
- How to define and call methods in Go
- The difference between value and pointer receivers
- How methods compare with regular functions
- Real-world use cases with structs and interfaces
Basic Syntax – Defining a Method on a Struct
type Person struct {
name string
}
// Method with value receiver
func (p Person) greet() {
fmt.Println("Hello,", p.name)
}
The (p Person) part is called the receiver—it tells Go that greet is a method on the Person type.
Calling a Method
p := Person{name: "Go Dev"}
p.greet()
Output:
Hello, Go Dev
Looks just like calling a function on an object.
Pointer Receiver vs Value Receiver
Value Receiver (copy):
func (p Person) rename(newName string) {
p.name = newName
}
Pointer Receiver (modifies original):
func (p *Person) rename(newName string) {
p.name = newName
}
Example:
p := Person{name: "Alice"}
p.rename("Bob")
fmt.Println(p.name) // Output depends on receiver type
Output:
- Value receiver →
Alice - Pointer receiver →
Bob
Use pointer receivers when you want to modify the original struct or avoid copying large structs.
Methods vs Functions in Go
| Feature | Method | Function |
|---|---|---|
| Requires receiver? | Yes | No |
| Syntax | func (t Type) name() | func name(t Type) |
| Used for | Type-specific behaviors | General-purpose logic |
| Works with interfaces | Yes (via method sets) | Only via adapter/wrappers |
Example – Method with Pointer Receiver
type Account struct {
balance float64
}
func (a *Account) Deposit(amount float64) {
a.balance += amount
}
func (a Account) ShowBalance() {
fmt.Printf("Current Balance: ₹%.2f\n", a.balance)
}
func main() {
acc := Account{}
acc.Deposit(1500)
acc.ShowBalance()
}
Output:
Current Balance: ₹1500.00
Deposit modifies the actual struct using a pointer receiver.
Method Sets – Interface Compatibility
Only types with the required method set can implement interfaces:
type Speaker interface {
Speak()
}
type Dog struct{}
func (d Dog) Speak() {
fmt.Println("Woof!")
}
func saySomething(s Speaker) {
s.Speak()
}
saySomething(Dog{}) // Dog implements Speaker
Method functions enable Go’s interface system, even without classes or inheritance.
Summary – Recap & Next Steps
Method functions in Go allow you to associate behavior with custom types. They’re foundational for interfaces, clean code organization, and object-like patterns in Go.
Key Takeaways:
- Method functions use receivers to bind behavior to a type
- Use value receivers for read-only logic, pointer receivers to mutate
- Methods help implement interfaces
- More explicit than traditional OOP, but just as powerful
Next: Learn about Go Variadic Functions to handle dynamic-length parameters in your functions and methods.
FAQs – Go Method Functions
What is a method in Go?
A function with a receiver that’s attached to a type, like a struct.
Can I define methods on non-struct types?
Yes, but only on named types (not built-in types like int directly).
When should I use pointer receivers?
Use them when your method needs to modify the receiver’s state or avoid copying large structs.
Can methods be used to implement interfaces?
Yes. Types implement interfaces implicitly by defining the required methods.
What happens if I define both pointer and value receiver methods?
Go automatically converts between value and pointer when calling the method—based on need.
Share Now :
