Go Reference ยท Concepts
Go method sets
Reference for Go method sets: which methods belong to T and *T, why a value may not satisfy an interface, addressability rules, and how embedding changes the sets.
The method set of a type is the list of methods that can be called on a value of that type, and it decides whether the type implements an interface. The rule is short: the method set of T contains the methods declared with a value receiver (t T), and the method set of *T contains the methods with a value receiver and those with a pointer receiver (t *T).
This looks odd at first, because you can write v.Scale(2) on an addressable variable v even when Scale has a pointer receiver. That works because the compiler rewrites it to (&v).Scale(2). But the rewrite is a convenience for calls, not a change to the method set. When a value is stored in an interface, no such rewrite is possible, so only the real method set counts.
type Counter struct{ n int } func (c Counter) Value() int { return c.n } // in the method sets of Counter and *Counter func (c *Counter) Inc() { c.n++ } // only in the method set of *Counter
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
| Type of the value | Method set contains |
|---|---|
T |
methods with receiver T |
*T |
methods with receiver T and receiver *T |
struct S embedding T |
methods promoted from T (receiver T) |
struct S embedding *T |
methods promoted from T and *T |
*S embedding T or *T |
methods promoted from T and *T |
interface I |
the methods named in I |
A type X implements interface I when the method set of X includes all of I's methods. Method sets of interface types and of pointer-to-interface types are different: a pointer to an interface (*I) has no methods at all, which is why *io.Reader is almost always a mistake.
Calling rule (not the same thing). A call x.M() is allowed if M is in the method set of x's type, or if x is addressable and &x has M in its set. Addressable values are variables, pointer indirections, slice elements, fields of addressable structs and elements of addressable arrays. Map elements, function results, string bytes and constants are not addressable.
Errors and edge cases: the compile error is Type does not implement Iface (method M has pointer receiver). Two methods of the same name on T and *T are rejected as a duplicate. A nil *T can still call a method declared on *T, but a nil *T calling a T method panics, because it must dereference.
Usage notes
- Pick one receiver kind per type. If any method needs a pointer (it mutates, or the struct is large, or it contains a mutex), make all of them pointer receivers. Mixed sets surprise users; the Go style guidance says the same.
- Interfaces mean pointers for mutable types. Store
&Counter{}in an interface or a slice of interfaces, and return*Tfrom constructors. - Value receivers work on both.
Value()above can be called through either aCounteror a*Counter, and both satisfyValuer. The pointer copy is made for you. - Value receivers copy. A method with a value receiver changes a copy; callers do not see the change. Copying a struct with a
sync.Mutexalso copies the lock, whichgo vetflags. - Embedding changes method sets. Embedding
TgivesSthe value methods ofT, and*Sthe pointer methods too. Embedding*Tgives both toSas well. See Struct embedding. - Generics. In a constraint, the type argument's own method set is used. If a method needs a pointer receiver, you often write a constraint on
*T(theinterface{ *T; Set(string) }pattern). go vetreports mistakes such as copying locks and misuse of thecopylocksrule; the compiler reports the interface errors.