Value vs. Pointer Receivers in Go: Why Method Changes Disappear
Reproduce lost struct mutations with value receivers, switch to pointer receivers cleanly, and keep receiver types consistent across a struct. After this, Go methods will mutate state when you expect them to.
Skip ahead
Already know value receivers copy the struct? Jump to Case 2: enforcing business rules on a real Order struct or Case 3: how receivers affect interface satisfaction.
One of the most confusing moments for a new Go developer happens when a method increments a counter or sets a status field—compiles with zero errors—and yet the caller's struct stays completely unchanged. The reason is Go's receiver rule: a value receiver (c Counter) receives a full copy of the struct, whereas a pointer receiver (c *Counter) receives a pointer to the original struct in memory.
Understanding value vs. pointer receivers also explains why a type sometimes fails to satisfy an interface even though it has the exact method name. Verified on Go 1.27.
Case 1: Value receiver silently edits a throwaway copy
Run both receivers side by side on the exact same Counter struct:
package main
import "fmt"
type Counter struct {
Name string
Count int
}
func (c Counter) IncWrong() { c.Count++ } // modifies a COPY only
func (c *Counter) IncRight() { c.Count++ } // modifies the caller's struct
func main() {
c := Counter{Name: "hits"}
c.IncWrong()
fmt.Println("after IncWrong:", c.Count)
c.IncRight()
fmt.Println("after IncRight:", c.Count)
}
after IncWrong: 0
after IncRight: 1
IncWrong incremented c.Count on its local copy and discarded that copy on return. Notice also that even though c is a plain value Counter (not *Counter), c.IncRight() worked automatically—when a variable is addressable, Go automatically inserts (&c).IncRight() for you.
Case 2: Enforcing state rules on a real Order struct
In production code, pointer receivers keep data and state transitions together—so callers cannot mutate a paid order:
type Order struct {
Customer string
Items []string
Status string
}
func (o *Order) AddItem(item string) error {
if o.Status != "open" {
return fmt.Errorf("cannot add %q to %s order", item, o.Status)
}
o.Items = append(o.Items, item)
return nil
}
func (o *Order) MarkPaid() { o.Status = "paid" }
func main() {
ord := Order{Customer: "Ana", Status: "open"}
_ = ord.AddItem("Keyboard")
ord.MarkPaid()
err := ord.AddItem("Cable")
fmt.Printf("items=%v status=%s err=%v\n", ord.Items, ord.Status, err)
}
items=[Keyboard] status=paid err=cannot add "Cable" to paid order
Case 3: Interface satisfaction—why Counter vs. *Counter matters
Go's method-set rule catches many intermediate developers off guard:
- A pointer type
*Thas all methods defined on bothTand*T. - A value type
Thas only methods defined onT—it does not include pointer-receiver methods when assigned to an interface!
For example, if func (c *Counter) Reset() has a pointer receiver and an interface Resetter requires Reset(), passing &c (type *Counter) compiles, while passing c (type Counter) fails to compile with Counter does not implement Resetter (method Reset has pointer receiver).
Case 4: Decision rules for picking a receiver
| Condition | Receiver | Why |
|---|---|---|
| Method modifies any struct field | *T (pointer) |
Value receiver edits a throwaway copy |
Struct contains sync.Mutex or large buffers |
*T (pointer) |
Copying a mutex breaks locking; copying large structs wastes memory |
Small immutable value (like time.Time, Point{X, Y}) |
T (value) |
Fast stack copy, cannot be mutated accidentally |
Any method on the type already uses *T |
*T for all methods |
Keeps the method set consistent so callers don't guess |
💡 Tip
A pointer receiver can safely handle a nil receiver if you check if o == nil { return "" }—useful for helper getters or recursive tree nodes.
Common mistakes
- Mixing value and pointer receivers on the same struct: Makes half the methods work on
Tand half only on*T, causing confusing interface errors. Stick to*Tacross the whole type once any method mutates it. - Using a value receiver on a struct containing a
sync.Mutex: Every method call copies the lock and disables mutual exclusion! Rungo vet ./...to catch copied locks. - Calling a pointer-receiver method directly on a map element (
m["k"].IncRight()): Map values are not addressable in Go; either store pointers (map[string]*Counter) or read, update, and write back.
⚠ Warning
Never assume Go warns you when a value-receiver method modifies a field—it is completely legal Go syntax, so your state change simply vanishes silently without a compiler error.
What happens if you skip this
Methods that appear to update fields silently leave structs unchanged, structs with embedded mutexes lose thread safety when copied, and interface assignments fail with puzzling method-set errors.
Related course: Go for Beginners.
About
Reproduce lost struct mutations with value receivers, switch to pointer receivers cleanly, and keep receiver types consistent across a struct. After this, Go methods will mutate state when you expect them to.
- Responsible
- Jay P
- Last Update
- 11/27/2026
- Reading Time
- 4 min
Reviews
Sign in to write a reviewComments
Sign in to commentNo comments yet.