AlgoMaster Logo

Loop Variable Change

Last Updated: May 22, 2026

High Priority
13 min read

Go 1.22 changed the scoping rules for for and range loop variables. Before 1.22, the loop variable was a single variable reused across every iteration, which broke code that spawned goroutines or registered callbacks inside the loop. Since 1.22, each iteration gets its own copy, and a whole class of subtle bugs disappears. This lesson explains the old behavior, the classic bug that motivated the change, the new semantics, and how to migrate safely.

Premium Content

Subscribe to unlock full access to this content and more premium articles.