On Day 1 you could call a pointer method on a plain variable, task.Finish(), and Go added the & for you. Put the same variable into an interface and that convenience disappears: the compiler rejects it. This lesson explains the rule and, more usefully, why it exists, because you will meet it again the moment you write to a strings.Builder.
The rule: when Finish has a pointer receiver, the type *Task has a Finish method but the type Task does not. So a *Task satisfies an interface that requires Finish, and a Task does not. A direct call such as task.Finish() on a variable still works, because Go can take that variable's address for the call.
Predict and run
Which record has changed at the end?
package main
import (
"fmt"
)
type Task struct{ Done bool }
func (t *Task) Finish() { t.Done = true }
type Finisher interface{ Finish() }
func finish(f Finisher) { f.Finish() }
func main() {
task := Task{}
task.Finish()
other := Task{}
finish(&other)
fmt.Println(task.Done, other.Done)
}Check your prediction
true trueRead the compiler message
Change finish(&other) to finish(other). The program no longer compiles:
cannot use other (variable of struct type Task) as Finisher value in argument to finish: Task does not implement Finisher (method Finish has pointer receiver)Read it from the end. method Finish has pointer receiver is the reason. Task does not implement Finisher is the consequence: a plain Task value doesn't have Finish in its method set. Restore the &.
Why Go refuses
Remember what an interface value holds: a copy of the value you stored. finish(other) would store a copy of other inside f. If Go allowed f.Finish() on it, the method would set Done on that hidden copy, and other would stay unfinished. That is the silent bug from Day 1's tracker, where a value receiver changed a copy. Rather than let it happen invisibly, Go makes it a compile error.
With &other, the interface holds a copy of the address, which still leads to other. Changes made through it land on the original, so the call is allowed. Value-receiver methods have no such danger, because they never change anything, so both Task and *Task have them.
You have already met this rule
strings.Builder's Write method has a pointer receiver, because writing has to grow the builder's own text. That is why the next lesson writes report(&output). Leave out the & and you get the same message about a different type:
strings.Builder does not implement io.Writer (method Write has pointer receiver)Practice
Your turn
The starter has an interface for anything that can be reset, a Stopwatch and a Counter, and resetAll, which resets every item in a slice. Give both types a Reset method that sets their field back to zero, choosing the receivers so that the resets reach the originals. Then call resetAll with both, and print the two values. The output should be:
0 0package main
import (
"fmt"
)
type Resetter interface{ Reset() }
type Stopwatch struct{ Seconds int }
type Counter struct{ Count int }
func resetAll(items []Resetter) {
for _, item := range items {
item.Reset()
}
}
func main() {
watch := Stopwatch{Seconds: 90}
counter := Counter{Count: 4}
fmt.Println(watch.Seconds, counter.Count)
}Hint 1: the idea
Ask of each method: does it change its receiver? A method that must change the caller's value needs a pointer receiver, and that decides which type, the value or its pointer, satisfies Resetter, and so what the slice has to hold.
Hint 2: a first step
Write func (s *Stopwatch) Reset() { s.Seconds = 0 }, then try resetAll([]Resetter{watch}) and read the compiler message. It tells you what the slice must hold instead.
Worked solution
package main
import (
"fmt"
)
type Resetter interface{ Reset() }
type Stopwatch struct{ Seconds int }
type Counter struct{ Count int }
func (s *Stopwatch) Reset() { s.Seconds = 0 }
func (c *Counter) Reset() { c.Count = 0 }
func resetAll(items []Resetter) {
for _, item := range items {
item.Reset()
}
}
func main() {
watch := Stopwatch{Seconds: 90}
counter := Counter{Count: 4}
resetAll([]Resetter{&watch, &counter})
fmt.Println(watch.Seconds, counter.Count)
}0 0Try giving Counter a value receiver instead. []Resetter{&watch, counter} now compiles, because a value-receiver method belongs to Counter too, but it prints 0 4: the interface held a copy, and the copy was reset. That is the very bug the pointer-receiver rule prevents, and why a method that changes its receiver should take a pointer.
Completion is saved in this browser only. It does not measure mastery.