← Back to archive

Stdle #40 · defer · 2026-07-16

What does this Go snippet output?

A past puzzle — fully playable. 4 attempts, hints on wrong guesses.

package main
import "fmt"
type R struct{ id int }
func (r R) Name() string { return fmt.Sprintf("R%d", r.id) }
func main() {
items := []R{{id: 1}, {id: 2}}
for _, it := range items {
defer fmt.Println(it.Name())
}
fmt.Println("loop done")
}
Defer
3 lines
4 attempts left

Answer & explanation