Fundamentals 10 min read

Understanding Method Sets, Interfaces, and Embedding in Go

This article explains Go's method sets, the relationship between method receivers and method sets, how interfaces work, and the role of embedding, providing code examples that illustrate value vs pointer receivers, interface implementation rules, and embedding differences for struct types.

360 Tech Engineering
360 Tech Engineering
360 Tech Engineering
Understanding Method Sets, Interfaces, and Embedding in Go

Go's high‑concurrency features make it popular in cloud development, but many developers are still unclear about its method set mechanics. This article reviews what a method set is, how it differs for value types (T) and pointer types (*T), and why certain methods appear in each set.

For a type type Student struct { age int8; name string } , the method set of Student includes only methods with a value receiver, while the method set of *Student includes both value‑receiver and pointer‑receiver methods. The compiler automatically converts calls from a value to its pointer when needed.

The article then explores the relationship between method sets and method receivers, showing that the compiler can invoke any method on a value or pointer as long as the receiver type matches, and that method overloading is not supported.

Next, it introduces Go interfaces as collections of method signatures. A type implements an interface implicitly when its method set contains all the interface's methods. Example code defines a Personer interface with showName() and demonstrates embedding an interface in a struct.

Interface implementation details are covered, including how the interface value consists of an interface table and a data pointer, and how the runtime stores type information and method pointers.

The article also discusses how implementing an interface with a value receiver versus a pointer receiver affects which types satisfy the interface. A value type implements the interface for both its value and pointer, while a pointer‑receiver implementation requires a pointer to satisfy the interface.

Embedding (anonymous fields) is presented as Go's way to achieve inheritance‑like behavior. By embedding a struct, its fields and methods become part of the outer struct's method set. The differences between embedding a value type versus a pointer type are illustrated, showing default zero values and nil pointer pitfalls.

Finally, the interaction between embedding and method sets is summarized: if a struct embeds a value type T, both the struct and *struct method sets include T's methods; if it embeds *T, both include T and *T methods. The article applies these rules to determine whether various struct variations implement a Human interface that requires showName() and setName(string) .

type Human interface { showName(); setName(string) }

Through multiple code snippets, the article demonstrates which struct instances and pointers satisfy the Human interface, highlighting the importance of receiver types and embedding choices.

Goembeddinginterface{}structmethod setpointer-receiver
360 Tech Engineering
Written by

360 Tech Engineering

Official tech channel of 360, building the most professional technology aggregation platform for the brand.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.