Articles

Fixing For Loops in Go 1.22

Sep 19, 2023 The Go Blog Golang

David Chase and Russ Cox explain the Go team's fix for the loop-variable-capture footgun that's bitten essentially every Go developer who's launched goroutines from inside a for-range loop: as of Go 1.22, each iteration gets its own copy of the loop variable instead of sharing one. What I like about this post is the transparency about the tradeoffs of changing well-established language semantics after a decade, and how they made the change safely without breaking existing code. If you've ever debugged "why do all my goroutines print the same value," this is the origin story and the fix.

Read the full article ↗

← Back to all articles