A method set in Go is the strict collection of methods attached to a specific type, primarily used to determine whether a type satisfies an interface. While method sets strictly dictate interface implementation, they do not strictly limit direct method invocation; Go provides syntactic sugar that allows pointer methods to be called directly on addressable value instances.Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
The Rules of Method Sets
The Go specification defines method sets based on the receiver type and struct embedding rules:- Value Type (
T): The method set of a defined typeTconsists of all methods declared with a value receiver of typeT, plus promoted methods with value receivers from embedded value fields (and all promoted methods from embedded pointer fields). - Pointer Type (
*T): The method set of a pointer type*Tconsists of all methods declared with a pointer receiver*T, all methods declared with a value receiverT, and all promoted methods from embedded fields.
Syntax Visualization
- Method set of
Document(Value):Read(),GetID()(promoted value method). - Method set of
*Document(Pointer):Read(),Write(),GetID(),SetID()(promoted value and pointer methods).
Method Sets vs. Direct Invocation
It is critical to distinguish between strict method sets (used for interface assignment) and flexible method calls (used for direct invocation on variables). When invoking a method directly on a variable, Go provides syntactic sugar for addressable values. If a method requires a pointer receiver (*T) but is called on an addressable value v of type T, Go implicitly translates the call to (&v).Method().
Write() is not in the method set of the value type Document, the call succeeds because d is an addressable variable in memory.
Addressability and Interface Satisfaction
The strictness of method sets applies primarily to interfaces. When a value is assigned to an interface, it loses its addressability. If Go allowed a value type (T) to satisfy an interface requiring a pointer method, the compiler would need to implicitly take the address of the value inside the interface. Since interface values are not addressable, this would cause memory safety issues.
Therefore, *T methods are strictly excluded from the T method set for interface satisfaction.
Conversely, a pointer (*T) can always be safely dereferenced (*p) by the compiler to create a copy for a value receiver (T). Because this dereferencing is universally safe, Go includes T methods in the *T method set.
Interface Method Sets
For interface types, the method set is exactly the set of methods explicitly declared within the interface definition, including any methods embedded from other interfaces. A concrete typeT or *T implements an interface if and only if its method set is a superset of the interface’s method set.
Document example:
*DocumentimplementsReadWriterbecause its method set contains bothRead()andWrite().Documentdoes not implementReadWriterbecause its method set lacksWrite(). Assigning aDocumentvalue to aReadWritervariable results in a compile-time error, regardless of whether the originalDocumentvariable was addressable.
Master Go with Deep Grasping Methodology!Learn More





