With a plain function, the subtotal of one dish is written subtotal(meatballs, 3): a shared function given the dish and the number of portions. Go has a way that sticks closer to the data: meatballs.Subtotal(3).
That second form is a method, a function attached to a type. Think of the menu cards in the till at Bimo's Meatball Stall, each with its own buttons, "work out the subtotal" and "raise the price". The buttons belong to that card, so you press them through the card, not through a shared machine.
Writing a method
package main
import "fmt"
type Menu struct {
Name string
Price int
}
func (m Menu) Subtotal(portions int) int {
return m.Price * portions
}
func main() {
meatballs := Menu{Name: "Tendon Meatballs", Price: 15}
fmt.Println(meatballs.Subtotal(3))
}
$ go run stall.go
45
(m Menu)betweenfuncand the method's name is the receiver: the value that owns the method. Inside the method,mis used like an ordinary parameter.meatballs.Subtotal(3)calls it with a dot. Go fillsmwithmeatballs, andportionswith3.- A method name that starts with a capital letter (
Subtotal) can be called from other packages, just likefmt.Println. - Methods can only be made for types you define yourself in that package.
func (n int) Double() intis refused withcannot define new methods on non-local type int, buttype Dollars intand thenfunc (d Dollars) Text() stringis fine.
Value receiver or pointer receiver
Guess first: the method RaisePrice below contains m.Price += amount. After meatballs.RaisePrice(1), is meatballs.Price 15 or 16?
A receiver is an argument too, so it is copied, just as in the Functions lesson:
func (m Menu) RaisePrice(amount int) {
m.Price += amount
}
meatballs.RaisePrice(1)
fmt.Println(meatballs.Price) // 15
m is a copy of meatballs. The copy's price goes up, then the copy is thrown away as soon as the method ends, so meatballs.Price stays 15. The compiler gives no warning at all.
Put * in front of the type, and the receiver becomes a pointer receiver:
func (m *Menu) RaisePrice(amount int) {
m.Price += amount
}
meatballs.RaisePrice(1)
fmt.Println(meatballs.Price) // 16
fmt.Println(meatballs.Subtotal(3)) // 48
*Menumeansmholds the address ofmeatballs, som.Price += amountchanges the realmeatballs.- You don't need to write
&meatballs. Becausemeatballsis a variable, Go takes its address for you:meatballs.RaisePrice(1)is the same as(&meatballs).RaisePrice(1). Subtotalstays a value receiver and reads the latest price: 16 × 3 = 48.
| Method | Receiver | Why |
|---|---|---|
Subtotal(portions) |
(m Menu) |
only reads, the struct is small |
RaisePrice(amount) |
(m *Menu) |
changes a field |
| a method on a big struct | (m *Menu) |
avoids copying every field on each call |
Mixing the two as here is allowed. Many Go teams switch every method of a type to a pointer receiver as soon as one method needs it, to stay consistent.
In the exercise: Circle
The exercise uses the same pattern with a struct Circle that has a field Radius float64. Use the number 3.14 from the brief, not math.Pi:
| Call | Formula | Printed |
|---|---|---|
c.Area(), radius 7 |
3.14 × 7 × 7 | 153.86 |
c.Perimeter(), radius 7 |
2 × 3.14 × 7 | 43.96 |
c.Resize(2) then c.Radius |
7 × 2 | 14 |
Area and Perimeter only read, so a value receiver (c Circle) is enough. Resize changes Radius, so it needs a pointer receiver (c *Circle); otherwise the radius stays 7.
📌 Method sets: who may call what
A method set is the list of methods a type has. Menu has only the value-receiver methods (Subtotal), while *Menu has all of them (Subtotal and RaisePrice). A call through a variable still works because Go takes its address for you, but a value with no address can't:
Menu{Name: "Iced Tea", Price: 5}.RaisePrice(1)
./stall.go:15:35: cannot call pointer method RaisePrice on Menu
This rule comes back in the Interface lesson: an interface that asks for RaisePrice can only hold &meatballs, not meatballs.
⚠️ Changes that vanish and numbers that don't match the brief
- A value receiver on a method that changes things. It compiles, there's no warning, but the change is lost. If the method sets a field, use
*. - Forgetting
*in the exercise.func (c Circle) Resize(...)leavesc.Radiusat 7, not 14. math.Piwhen the brief asks for 3.14.math.Pi * 7 * 7prints153.93804002589985, not153.86.- Calling a pointer method on a value with no address.
Menu{...}.RaisePrice(500)is refused. Store it in a variable first.
TL;DR: A method is a function with a receiver: func (m Menu) Subtotal(portions int) int, called as meatballs.Subtotal(3). A receiver is copied like an argument, so a value receiver (m Menu) only changes a copy and meatballs.Price stays 15. A pointer receiver (m *Menu) changes the original (16), and Go takes &meatballs for you when you call through a variable. Use a pointer receiver when the method changes a field or the struct is big; a value receiver for methods that only read. In the exercise: Area 153.86 and Perimeter 43.96 with 3.14, then Resize with (c *Circle) makes the radius 14. *Menu has every method, Menu only the value-receiver ones.
⚡ A function attached to a type
Write:
func (m Menu) Subtotal(portions int) int {
return m.Price * portions
}
fmt.Println(meatballs.Subtotal(3)) // 45
Meaning: (m Menu) between func and the method's name is the receiver, the value that owns the method. You call it with a dot: meatballs.Subtotal(3).
⚡ A receiver is copied too
Guess first: func (m Menu) RaisePrice(amount int) contains m.Price += amount. After meatballs.RaisePrice(1), what is meatballs.Price?
Still 15. m is only a copy of meatballs, and the compiler gives no warning at all.
Meaning: A receiver is an argument too, so it gets copied.
⚡ A pointer receiver
Write:
func (m *Menu) RaisePrice(amount int) {
m.Price += amount
}
Meaning: With *, m holds the address of meatballs, so the real price becomes 16. Use a pointer receiver when the method changes a field or the struct is big. A method that only reads is fine with a value receiver.