By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
A method is a function with a receiver. The receiver appears in parentheses before the method name and names the value the method is called on. Everything you already know about functions still applies: parameters, return values, and the body.
Methods let you keep the data and the behaviour of a type together. coffee.Dollars() reads better than Dollars(coffee), and the compiler knows Dollars is meant for Expense values.
package main
import "fmt"
type Expense struct {
ID int
Cents int
}
// Dollars reads the expense, so a value receiver is enough.
func (e Expense) Dollars() string {
return fmt.Sprintf("$%d.%02d", e.Cents/100, e.Cents%100)
}
// SetCents writes to the expense, so it needs a pointer receiver.
func (e *Expense) SetCents(cents int) {
e.Cents = cents
}
func main() {
coffee := Expense{ID: 1, Cents: 450}
fmt.Println("before:", coffee.Dollars())
coffee.SetCents(500)
fmt.Println("after:", coffee.Dollars())
}The receiver e behaves like a parameter inside the method body. In Dollars, e is an Expense. In SetCents, e is an *Expense - a pointer to an expense - which is why the assignment e.Cents = cents changes the caller's struct.
There are exactly two kinds of receiver, and the difference is the same copy-versus-address difference you met with function arguments.
package main
import "fmt"
type Expense struct {
Cents int
}
// Reset gets a copy: it changes the copy, then the copy is thrown away.
func (e Expense) Reset() {
e.Cents = 0
}
// Zero gets the address: it changes the real expense.
func (e *Expense) Zero() {
e.Cents = 0
}
func main() {
coffee := Expense{Cents: 450}
coffee.Reset()
fmt.Println("after Reset (value receiver):", coffee.Cents)
coffee.Zero()
fmt.Println("after Zero (pointer receiver):", coffee.Cents)
}This is the single most important idea in the lesson: a value receiver cannot change the value it was called on. If a method looks right and the data never changes, read the receiver before you debug anything else.
SetCents Uses a PointerPenny needs SetCents because it edits an expense. A setter is a writer, and a writer needs an address. The same rule covers every method that mutates: AddCents, Rename, MarkPaid.
| The method does this | Receiver | Example |
|---|---|---|
| Reads fields only | Value receiver | func (e Expense) Dollars() string |
| Changes fields | Pointer receiver | func (e *Expense) SetCents(cents int) |
| Works with large structs | Pointer receiver | func (e *Expense) Summary() string |
Large structs are the third row: copying a big struct for every call costs memory and time, so a pointer receiver avoids the copy even when the method only reads. For Penny's four-field Expense, either choice works; the important thing is that one type uses one style throughout. A consistent receiver set keeps the method set simple, which matters when interfaces arrive in the next lesson.
Go has one more convenience: when you call a pointer method on a variable, you do not need to write the address. coffee.SetCents(500) is shorthand for (&coffee).SetCents(500). Go takes the address for you because coffee is a variable with a home in memory.
package main
import "fmt"
type Counter struct {
n int
}
func (c *Counter) Inc() {
c.n++
}
func (c Counter) Value() int {
return c.n
}
func main() {
c := Counter{}
c.Inc()
c.Inc()
fmt.Println("count:", c.Value())
step := c.Inc
step()
fmt.Println("after method value:", c.Value())
}step := c.Inc creates a method value: a function that remembers which counter it belongs to. You will use method values later when a function needs to run a small action, for example http.HandleFunc("/expenses", handler.List).
Add a method func (e *Expense) AddCents(cents int) that increases e.Cents, then call it twice on coffee and print the result. Next, remove the star from the receiver, run the program again, and explain the output you see. That experiment on purpose will teach you the difference faster than any paragraph.
Key takeaways
A method is a function with a receiver, and it belongs to one type.
A value receiver works on a copy; a pointer receiver works on the original.
Methods that change the receiver need a pointer receiver.
x.M() on a variable is shorthand for (&x).M() when M has a pointer receiver.
A method value like step := c.Inc keeps its receiver and can be called later.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.