What is the difference between a value receiver and a pointer receiver in Go?
Learn the difference between value and pointer receivers in Go, when to use each for mutation and performance, and how method sets affect interfaces.
Expected Interview Answer
A value receiver (func (t T) M()) operates on a copy of the value, so mutations inside the method do not affect the original, while a pointer receiver (func (t *T) M()) operates on the address, so it can modify the original and avoids copying large structs.
Choose a pointer receiver when the method must mutate the receiver, when the struct is large enough that copying is wasteful, or when you need consistency because some methods already use pointers. Value receivers are fine for small, immutable values and are safe for concurrent reads. A key subtlety is method sets: a pointer receiver method is only in the method set of the pointer type, so a non-addressable value cannot satisfy an interface through a pointer-receiver method; conventionally, keep all of a type's receivers the same kind.
- Pointer receivers allow in-place mutation of the original
- Pointer receivers avoid copying large structs
- Value receivers are safe, copy-based, and good for small immutable types
- Consistent receiver kind keeps method sets predictable
- Correct choice affects interface satisfaction and addressability
AI Mentor Explanation
A value receiver is like handing a coach a photocopy of the team sheet: any notes they scribble stay on their copy and never reach the real sheet. A pointer receiver is like giving them the original sheet, so their changes update the team for everyone. If the method must actually change the lineup, you pass the original — the pointer — not a disposable copy that gets thrown away.
Step-by-Step Explanation
Step 1
Decide on mutation
If the method must change the receiver's state, use a pointer receiver.
Step 2
Consider size
For large structs, prefer a pointer receiver to avoid expensive copies on every call.
Step 3
Check the method set
Remember pointer-receiver methods belong only to the pointer type's method set.
Step 4
Mind addressability
Ensure the value is addressable when calling a pointer method, or store it in a variable first.
Step 5
Stay consistent
Use the same receiver kind across all of a type's methods to keep interface satisfaction predictable.
What Interviewer Expects
- That value receivers work on a copy and pointer receivers on the original
- When to prefer pointer receivers: mutation, large structs, consistency
- Understanding of method sets and interface satisfaction differences
- Awareness of addressability rules when calling pointer methods
- The convention of keeping receiver kinds consistent per type
Common Mistakes
- Expecting a value receiver to mutate the original value
- Mixing pointer and value receivers on the same type without reason
- Not realizing a value type's method set excludes pointer-receiver methods
- Copying large structs unnecessarily via value receivers
- Assuming a map or slice element is addressable enough to call a pointer method
Best Answer (HR Friendly)
“A value receiver gives a method its own copy of the data, so changes don't stick, while a pointer receiver works on the real thing and can change it. You use a pointer receiver when the method needs to update the value or when the data is large and you don't want to copy it.”
Code Example
type Counter struct {
Count int
}
// value receiver: works on a copy, no lasting effect
func (c Counter) IncByValue() {
c.Count++
}
// pointer receiver: mutates the original
func (c *Counter) IncByPointer() {
c.Count++
}
func main() {
c := Counter{}
c.IncByValue()
fmt.Println(c.Count) // 0
c.IncByPointer()
fmt.Println(c.Count) // 1
}Follow-up Questions
- How do method sets differ between T and *T?
- Why might a value stored in a map fail to call a pointer-receiver method?
- When are value receivers preferable for concurrency?
- How does Go automatically take the address for pointer method calls?
- Why is consistency of receiver kind recommended across a type?
MCQ Practice
1. What does a value receiver method operate on?
A value receiver receives a copy, so mutations inside the method do not affect the original.
2. Which receiver should you use to mutate the receiver's fields?
Only a pointer receiver operates on the original, allowing in-place mutation.
3. A pointer-receiver method is part of the method set of:
Pointer-receiver methods belong only to *T's method set, which affects interface satisfaction.
Flash Cards
Value receiver behaviour? — Operates on a copy; mutations don't affect the original.
Pointer receiver behaviour? — Operates on the original via its address; can mutate it and avoids copying.
When to use a pointer receiver? — For mutation, large structs, or consistency with other methods.
Method set of *T vs T? — *T includes pointer- and value-receiver methods; T includes only value-receiver methods.
Convention? — Keep all of a type's receivers the same kind.
Continue Learning
Related Interview Questions
What is a struct in Go and how does struct embedding provide composition?
medium
What is the empty interface (interface{} / any) in Go and when is it used?
medium
What are Go interfaces and how does implicit implementation work?
medium
How does error handling work in Go and why does it not use exceptions?
medium