Curriculum Idioms & ecosystem Common gotchas exercise 4 · fill-word
Common gotchas
Fill in the keyword. Loop variable capture in
goroutines before Go 1.22 captured the SHARED loop
variable (all goroutines saw the FINAL value). Go 1.22's
fix gives each iteration its OWN loop variable.
The bulletproof workaround that works on EVERY Go version:
shadow the loop var inside the loop body before launching
the goroutine.
``go
for i := 0; i < 3; i++ {
i := i // shadow — same name, NEW scope binding
go ${word} () { fmt.Println(i) }()
}
``
Type the keyword that introduces the anonymous function.
TypeScript reference
Fill the blanks →
About this theme
The "what bit me in code review" survival kit:
- Loop variable capture (Go 1.22+ helped, but you should still know).
- Nil interface vs nil concrete (a non-nil pointer inside an interface variable makes the interface non-nil).
- Slice aliasing — two slices sharing a backing array.
- Goroutine leaks — a goroutine waiting on a channel that nobody will ever send to.