Skip to main content

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.

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.

The Rules of Method Sets

The Go specification defines method sets based on the receiver type and struct embedding rules:
  1. Value Type (T): The method set of a defined type T consists of all methods declared with a value receiver of type T, plus promoted methods with value receivers from embedded value fields (and all promoted methods from embedded pointer fields).
  2. Pointer Type (*T): The method set of a pointer type *T consists of all methods declared with a pointer receiver *T, all methods declared with a value receiver T, and all promoted methods from embedded fields.

Syntax Visualization

type Base struct {
    ID int
}

func (b Base) GetID() int { return b.ID }
func (b *Base) SetID(id int) { b.ID = id }

type Document struct {
    Base // Embedded field
    Title string
}

// Value receiver
func (d Document) Read() string {
    return d.Title
}

// Pointer receiver
func (d *Document) Write(title string) {
    d.Title = title
}
Based on the declarations above, the method sets are evaluated as follows:
  • 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().
var d Document
d.Write("New Title") // Valid: Go translates this to (&d).Write("New Title")
Even though 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 type T or *T implements an interface if and only if its method set is a superset of the interface’s method set.
type ReadWriter interface {
    Read() string
    Write(string)
}
In the context of the Document example:
  • *Document implements ReadWriter because its method set contains both Read() and Write().
  • Document does not implement ReadWriter because its method set lacks Write(). Assigning a Document value to a ReadWriter variable results in a compile-time error, regardless of whether the original Document variable was addressable.
Master Go with Deep Grasping Methodology!Learn More