Pointer receiver (*T) — operates on the original value, can modify it. Value receiver (T) — operates on a copy, modifications are discarded.
When you declare a method with a pointer receiver, the method gets a pointer to the receiver. Any changes to the receiver's fields persist after the method returns. Value receivers get a snapshot — the caller never sees changes.
1type BankAccount struct {2 balance float643}45// Pointer receiver — mutates original6func (a *BankAccount) Deposit(amount float64) {7 a.balance += amount8}910// Value receiver — read-only, safe11func (a BankAccount) Balance() float64 {12 return a.balance13}1415acct := BankAccount{balance: 100}16acct.Deposit(50)17fmt.Println(acct.Balance()) // 150
When to use:
Common mistakes:
v and call an interface method on &v, you must pass &v, not v.