Unique loop variables and the Go 1.22 fix

June 3, 2024

If you haven't joined me for a livestream in the past, consider [joining today](https://youtube.com/live/BYOsoZ5ywx8)! I'll be doing some refactoring, and adding new features to [Kivik](https://kivik.io/), with TDD.

For statements with for clause

Each iteration has its own separate declared variable (or variables) [Go 1.22].

This seemingly simple sentence carries a lot of weight. So that’s what we’ll be talking about today.

Prior to Go 1.22 (which only came out earlier this year!), the loop variable was re-used, rather than re-declared for each loop iteration. This lead to a lot of confusion and bugs. I won’t go into those details here, since, thankfully, it’s no longer a problem… usually. If you’re interested, I recommend Fixing For Loops in Go 1.22 for a description of the problem and fix.

For our purposes, you’ll only care about this if either of the following things are true:

  1. You’re reading a book or tutorial that warns against this problem. You can disregard this advice, unless the following is also true:

  2. You’re building a Go module that must support old versions of Go. The version found in your go.mod controls this behavior, even if you’ve installed Go 1.22.0 or newer. If you’re building a library that needs to maintain backward compatibility, you’ll need to consider the old looping behavior for as long as you’re supporting older versions of Go.

So what’s the TL;DR; solution if you need to support this old behavior?

Replace this:

for i := 0; i < 10; i++ {
  /* do something with i */
}

with this:

for i := 0; i < 10; i++ {
  i := i
  /* do something with i */
}

This re-declares a scope-specific instance of i, which is not shared between loop iterations. And this fix is also completely safe (though unnecessary) in Go 1.22.0+, so you needn’t worry about removing it later.

Quotes from The Go Programming Language Specification Language version go1.22 (Feb 6, 2024)


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Final notes on for loop variable scope

For statements with for clause … The variable used by the first iteration is declared by the init statement. The variable used by each subsequent iteration is declared implicitly before executing the post statement and initialized to the value of the previous iteration’s variable at that moment. So let’s break this down. In fact, the pseudo code I showed you during my earlier breakdown really more closely demonstrated the behavior prior to Go 1.


Iteration over strings

For statements with range clause … For a string value, the “range” clause iterates over the Unicode code points in the string starting at byte index 0. On successive iterations, the index value will be the index of the first byte of successive UTF-8-encoded code points in the string, and the second value, of type rune, will be the value of the corresponding code point. If the iteration encounters an invalid UTF-8 sequence, the second value will be 0xFFFD, the Unicode replacement character, and the next iteration will advance a single byte in the string.


Iteration over arrays and slices

For statements with range clause … For an array, pointer to array, or slice value a, the index iteration values are produced in increasing order, starting at element index 0. If at most one iteration variable is present, the range loop produces iteration values from 0 up to len(a)-1 and does not index into the array or slice itself. For a nil slice, the number of iterations is 0. From this above paragraph, we can be assured that ranging over an array, pointer to array, or slice, will operate in a defined order–from index 0, upward.

Get daily content like this in your inbox!

Subscribe