1. The basics: receiver and method set
Sometimes it feels like the Go compiler was made specifically to ruin your mood. You write beautiful code, the type has a method Add, the interface also has Add, but assigning to the interface suddenly does not compile. At that moment you want to say: “Come on, compiler, I can see that the method exists!” — but the compiler sees the world a little more formally.
That formalism is useful: it protects you from ambiguities and hidden data copies. In Go, the idea that “values and pointers are different things” matters, and the set of methods available on T and on *T also differs. That set is called the method set. Go uses it to decide whether a type implements an interface.
If you remember two or three rules about the method set, you will stop “poking the compiler with a stick” and start predicting its behavior. And that, agreeably, is nicer than guessing who wins today: you or go build.
Value receiver and pointer receiver in human terms
Before going deeper into the method set, it is useful to restore intuition. A method can be declared either on a value or on a pointer. It looks like this:
func (t Task) TitleUpper() string { ... } // value receiver
func (t *Task) MarkDone() { ... } // pointer receiver
A value receiver (Task) means “the method works on a copy of the value.” Usually it either does not change anything, or changes something “inside itself”, but those changes do not get outside, because the copy was modified.
A pointer receiver (*Task) means “the method works with the same object”, and the changes are visible from the outside. This is the main choice when a method needs to mutate the state of a struct.
But there is another effect that beginners often do not think about: the receiver affects whether T implements an interface or whether *T is needed. And that is directly related to the method set.
What a method set is: a simple definition and a rule table
A type’s method set is the “list of methods the compiler considers to belong to that type” in the context of interface checks and calls. We are interested specifically in the interface part: for a type to implement an interface, its method set must include all interface methods.
In Go, the key rule is this:
| The type we are considering | Which methods are included in the method set |
|---|---|
|
methods with receiver T |
|
methods with receiver T and methods with receiver *T |
This means something very practical: if you declare a method on *T, then the value T may not implement the interface that requires that method. But *T does.
This situation is so typical that you may see it even in language examples and discussions: for example, in interface discussions in the context of methods with a pointer receiver, it is clearly visible that “the type does not satisfy the interface because the method has a pointer receiver.”
2. Example: a “task manager” and the TaskStore interface
To keep things concrete, let us continue our educational mini project. Suppose we have a task and a task store (for now in memory, without files or a DB — we are just training architectural thinking).
Let us start with the model:
package main
type Task struct {
ID int
Title string
Done bool
}
Now let us define the interface that the “consumer” needs. For example, our code wants to add a task and mark it as done:
package main
type TaskStore interface {
Add(t Task)
MarkDone(id int) bool
}
Let us make a simple in-memory implementation:
package main
type MemStore struct {
tasks []Task
}
func (s *MemStore) Add(t Task) {
s.tasks = append(s.tasks, t)
}
func (s *MemStore) MarkDone(id int) bool {
for i := range s.tasks {
if s.tasks[i].ID == id {
s.tasks[i].Done = true
return true
}
}
return false
}
Pay attention: both methods are on *MemStore, because we change the internal slice (we add elements and change fields). That seems logical.
And now — the moment of truth. We want to write a function that accepts the interface:
package main
func Seed(store TaskStore) {
store.Add(Task{ID: 1, Title: "Buy tea"})
store.Add(Task{ID: 2, Title: "Do not forget tea"})
}
And in main:
package main
import "fmt"
func main() {
var s TaskStore = MemStore{} // we want this
Seed(s)
fmt.Println("ok")
}
This code will not compile. The compiler will say something like: MemStore does not implement TaskStore (Add has pointer receiver).
Why? Because TaskStore requires the methods Add and MarkDone. The method set of MemStore (as a value) contains only methods with receiver MemStore, but our methods are declared on *MemStore. So the value MemStore{} does not implement the interface.
The correct solution is to pass a pointer:
package main
import "fmt"
func main() {
var s TaskStore = &MemStore{} // now this is *MemStore
Seed(s)
fmt.Println("ok") // ok
}
Now the type inside the interface is *MemStore, and its method set includes both MemStore and *MemStore methods, so everything needed is present.
4. “But I can call the method on a value”: the trap of syntactic sugar
This is where confusion usually begins. A student says: “Wait. I can do this: ms := MemStore{}; ms.Add(...) — and it compiles. So why not in an interface?”
Let us be honest and show it:
package main
import "fmt"
type MemStore struct{ tasks []int }
func (s *MemStore) Add(x int) {
s.tasks = append(s.tasks, x)
}
func main() {
ms := MemStore{}
ms.Add(10) // compiles!
fmt.Println(ms.tasks) // [10]
}
This compiles because Go provides convenience: if you have an addressable value (ms is a variable, so it has an address), then when calling a method with a pointer receiver the compiler implicitly inserts &ms. So in reality it calls (&ms).Add(10).
But it is important to understand: this is a call convenience, not a change to the rules for interface implementation. For an interface, the compiler looks strictly at the type’s method set. When you write var s TaskStore = MemStore{}, the compiler is not obliged to (and will not) “magically take the address” of a temporary value to satisfy the interface.
A useful thought: “method call” and “interface implementation” are checked by similar rules, but not the same ones. A call can use implicit address taking, while the interface check cannot; it relies on the method set.
5. Choosing the receiver with interfaces in mind
When a value receiver is more useful
After the previous section it is easy to draw the wrong conclusion: “so we should always make methods use pointers.” Not always.
There are types that it is logical and safe to pass by copying: small structs, immutable “values” (for example, a point on a plane), wrapper types around primitive numbers, and cases where the method does not mutate anything at all.
Let us show an example. Create a TaskID type as a “meaningful” type so that you do not accidentally mix up an ID with something else:
package main
import "fmt"
type TaskID int
func (id TaskID) String() string {
return fmt.Sprintf("task#%d", id)
}
func main() {
var x fmt.Stringer = TaskID(7)
fmt.Println(x.String()) // task#7
}
Here a value receiver is a great fit: TaskID is small, cheap to copy, and the method String() does not change state. And a nice bonus: both TaskID and *TaskID will satisfy fmt.Stringer, because the method set of *T includes the methods of T.
Pointer receiver: two scenarios and one dangerous one
When we choose a pointer receiver, it is usually one of two scenarios.
The first scenario is that we need to change state. This is our MemStore: we add tasks and change Done. In this case, a pointer receiver is almost inevitable, and then we simply accept it as a fact: the interface will be implemented by *MemStore, not by MemStore.
The second scenario is that we do not want to copy a large struct. Even if the method does not change anything, copying a large object may be unnecessary. Then a pointer receiver becomes an “optimization” decision, but its cost is reduced compatibility with interfaces (again, everywhere you will need to pass *T).
There is also a third scenario that looks harmless but can lead to strange errors: typed nil inside an interface. It is not directly about the method set, but it often appears exactly with pointer receivers, because a pointer receiver makes it possible to store nil as a “value” of type *T.
For example, error is an interface. Any type with an Error() string method implements it, including types whose method is declared on *T.
And it is easy to do this:
package main
import "fmt"
type MyErr struct{ Msg string }
func (e *MyErr) Error() string {
if e == nil {
return "<nil MyErr>"
}
return e.Msg
}
func bad() error {
var e *MyErr = nil
return e // typed nil packed into interface error
}
func main() {
fmt.Println(bad() == nil) // false
}
Why is it false? Because the error interface stores the dynamic type *MyErr, and the dynamic value is nil. The interface as a whole is not nil. This story is the classic interface nil trap, and people often warn about it separately in materials about error handling and API design.
What does this have to do with the method set? Indirectly, but practically: a pointer receiver makes it possible for a concrete type to have a nil value, and the interface we put it into can then behave “not as expected” when compared with == nil.
How to choose a receiver: think about future calls and interfaces
When you write methods for a type, you are actually designing a mini API. And the receiver is part of that API. If your type is supposed to be used through interfaces, the receiver choice affects convenience.
Imagine that you have an interface:
type Closer interface {
Close() error
}
If Close() is declared on *T, then you will need to pass *T into the interface. If Close() is declared on T, then both T and *T will work. On paper, a value receiver seems more “universal,” but it can lead to accidental copies and to the method changing a copy (if someone later decides to add mutation inside Close()).
In our task application, the rule can be stated simply: MemStore is a “container with internal state”, so a pointer receiver is justified, and the interface variable should store *MemStore. And TaskID is a “value”, so a value receiver is fine.
A useful habit: when defining an interface and the type that implements it, say it out loud: “Who will live in the interface: T or *T?” That removes half of the future questions.
6. Diagnostics: how to see what is inside an interface
When something does not compile or behaves strangely, you want to see reality, not guesses. With interfaces, fmt.Printf("%T", x) is very helpful — it shows the dynamic type of the value inside the interface (or "<nil>" if the interface is fully nil).
For example, in our TaskStore:
package main
import "fmt"
type TaskStore interface {
Add(id int)
}
type MemStore struct{ items []int }
func (s *MemStore) Add(id int) { s.items = append(s.items, id) }
func main() {
var a TaskStore = &MemStore{}
fmt.Printf("%T\n", a) // *main.MemStore
}
If you see *main.MemStore, then the interface really stores a pointer.
And conversely, if you ever suspect typed nil, this output helps too: you can see that the type inside the interface is pointer-like, even if the “value” there is nil.
7. Typical mistakes
Mistake #1: “The method is on *T, but I am trying to put T into an interface.”
This usually looks like var x I = T{...} and a compilation error about a pointer receiver. The reason is almost always the method set: the value T does not have methods declared on *T, so T does not implement the interface. The fix is either to replace it with &T{...}, or to rethink the receivers (if there are no mutations and copying is safe).
Mistake #2: “I called t.M() on a value, so it must implement the interface.”
That is a logical trap. A method call allows conveniences such as implicit address taking for an addressable variable. Interface implementation is checked more strictly by the method set. Keeping this in mind removes the feeling that “the compiler is against me.”
Mistake #3: “I made a value receiver for a method that should change state, and was surprised that nothing changed.”
This is the other side of the coin: you chose receiver T, the method honestly changed a copy, and the original value stayed the same. This bug is especially unpleasant when there is a lot of logic inside the method, and it feels like “it definitely did something.” In practice, if a method changes struct fields or should mutate an internal slice/map — you almost always need *T.
Mistake #4: “I caught typed nil in an interface and I check iface == nil as a sign of absence.”
This most often happens with error: a function returns (*MyErr)(nil) as error, and the caller gets err != nil and thinks the operation failed. This is not directly about the method set, but it typically arises where methods are on *T, and therefore nil becomes a valid “value” of a concrete type. This trap is discussed separately in materials about errors and the error contract.
Mistake #5: “I made a type for an interface, then added a method with a pointer receiver — and everything ‘broke’.”
This happens when the type used to implement the interface by value, and then you added (or changed) a method so that it became a pointer receiver. As a result, T stopped implementing the interface, and the project starts requiring *T. This is not “bad Go”; it is a change to your API contract. That is why the receiver is part of the design: it should be changed consciously, especially if the type is already used through interfaces.
GO TO FULL VERSION