Pointer receiver (*T) — method operates on the original value via pointer, can modify it, avoids copying. Value receiver (T) — method operates on a copy, changes are discarded.
The receiver type determines how the method interacts with the struct. Pointer receivers get a pointer — mutations persist. Value receivers get a snapshot — mutations are local to the method.
1type Player struct {2 health int3 name string4}56// Pointer receiver — modifies health7func (p *Player) TakeDamage(amount int) {8 p.health -= amount9}1011// Value receiver — read-only12func (p Player) IsAlive() bool {13 return p.health > 014}1516hero := Player{health: 100, name: "Hero"}17hero.TakeDamage(30)18fmt.Println(hero.IsAlive()) // true
When to use:
Common mistakes:
nil pointer receivers are valid in Go — calling a method on a nil pointer can cause panics if the method dereferences it.Point) where copying is cheaper than indirection.