Method Declarations
Learn how to define methods on types in Go, the difference between value and pointer receivers, and how method sets work.
Go does not have classes in the traditional object-oriented sense, but it gives you a direct way to attach behavior to any type you define. That mechanism is the method declaration. A method is a function that carries an extra receiver argument placed between the func keyword and the function name. The receiver binds the function to a particular type, turning it into a member-like operation you can call on values of that type.
This section covers the syntax of method declarations, the critical distinction between value receivers and pointer receivers, and method sets, the rulebook Go uses to decide which types satisfy an interface. It assumes Go 1.27.
Method Syntax
How a method declaration is written, what the receiver parameter means, and which types can have methods. A method is bound to a defined type declared in the same package, and the receiver name may be omitted when the body never uses it.
Value Receivers vs Pointer Receivers
The choice that controls whether a method works on a copy of the caller's data or on the original. A value receiver is the idiomatic default for read-only operations on small types; a pointer receiver is required when the method must mutate the receiver, when the type holds a synchronisation primitive, or when the type is large enough that copying it is wasteful.
Method Sets
The compiler-computed set of methods available on a type T and on its pointer *T. Value-receiver methods belong to both; pointer-receiver methods belong only to *T. That asymmetry is what decides interface satisfaction, and it explains why a value can call a pointer method directly yet still fail to satisfy an interface that requires it.
Summary
Method declarations give Go a lightweight way to associate behavior with data. The receiver parameter defines the type on which the method operates, and the choice between value and pointer receiver controls both mutation semantics and interface eligibility. Method sets are the quiet engine behind Go's implicit interfaces: the method set of a type determines which interfaces it satisfies, and the difference in method sets between T and *T explains why sometimes a pointer is required even though Go automatically takes addresses in simple cases.
Method Syntax in Go
Understand the syntax for declaring methods in Go, from the receiver parameter to binding a method to its base type.
Value Receivers vs Pointer Receivers
Understand the key differences between value and pointer receivers in Go methods, when to use each, and the common pitfalls that arise from the choice.
Method Sets
Understand method sets in Go, the difference between T and *T method sets, and how they govern interface satisfaction and method calls.