Stage 1 · Code
Methods, Interfaces & Errors
Methods
Attach behaviour to types with methods, choose the right receiver, and design clean APIs around your data types.
Method Syntax
A method is a function with a receiver — an extra parameter that appears before the function name. The receiver binds the function to a type, making it callable with dot notation.
Value vs Pointer Receivers
A value receiver gets a copy of the value. A pointer receiver gets the address of the value. Use a pointer receiver when the method needs to modify the receiver, or when copying the value would be expensive.
| Receiver type | Can mutate? | Use when |
|---|---|---|
Value (c T) | No — gets a copy | Read-only; small, cheap-to-copy type |
Pointer (c *T) | Yes — gets the address | Mutation needed; large struct; type has a mutex |
If any method on a type needs a pointer receiver, make all methods use pointer receivers. Mixing value and pointer receivers on the same type is confusing and causes surprising interface-satisfaction behaviour.
Method Sets
The method set of a type is the complete list of methods you can call on it. A *T value can call all methods defined with *T and T receivers. A plain T value can only call methods with T receivers.
Methods on Non-Struct Types
You can define methods on any named type defined in your package, not just structs. This is commonly used to add display, conversion, or validation behaviour to primitive-based types.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.