go
Go changed a loop, and nothing broke

Go 1.22 came out on the sixth of February. The headline change is that the
variables declared by a for loop are now created fresh on each iteration,
where before they were created once and then updated.
That changes the meaning of code that already exists and already compiles, and a language with Go's compatibility promise normally doesn't do that.
The bug it removes
Every Go tutorial has a warning about this one.
for _, id := range ids {
go func() {
process(id) // pre-1.22: which id?
}()
}
Before 1.22, id is one variable that the loop updates. The goroutines close over
that single variable, so they see whatever value it holds when they happen to
run. Usually that's the last one, sometimes not, it depends on scheduling. The
same trap catches anything that captures the variable and outlives the
iteration, so defer, a closure appended to a slice, a callback registered with
something else.
The fix everyone learned was to shadow it:
for _, id := range ids {
id := id // the line nobody could explain to a newcomer
go func() { process(id) }()
}
That line is in an enormous amount of Go code. It does nothing visible, and you can't justify it to someone learning the language without a paragraph about variable lifetime.
In 1.22 the original version is correct and the shadowing line is redundant.
The version line in go.mod
The obvious objection is that some program somewhere relies on the old semantics, and changing them silently breaks that program.
I think two things made it defensible. Almost every program that relied on the old behaviour relied on it by accident (the old semantics produce surprising results, and the surprise is nearly always a bug).
Then there's the mechanism, which I'd happily steal. The new behaviour is tied
to the language version declared in go.mod. A module that says go 1.21 keeps
the old semantics even when it's compiled by a 1.22 toolchain. Nothing changes
until you edit that line, and editing it is something a person does, and that
person can then run the tests.
So you install the new toolchain, change nothing, and everything behaves as
before. Later you bump the language version in go.mod as its own commit, and
that commit is where the semantic change lands. It can be reviewed and reverted
separately from the toolchain upgrade, which would otherwise have carried it in
without anyone seeing.
It's a much better answer than a flag. A flag is a global setting and somebody eventually turns it on for the wrong module. With per-module versioning the decision only reaches the module that declared it.
The other one I have used more
Less discussed, but I use it more often: ranging over an integer.
for i := range 10 {
fmt.Println(i)
}
for i := 0; i < 10; i++ isn't hard to write, but it has three places to make a
mistake and this has none. Most of my loops that aren't over a collection are
over a count, so this is the one I write now.
1.22 also ships a preview of range-over-function iterators, which lets a type
expose a sequence that range can consume without materialising it into a slice.
I'm watching that one and not using it yet. It is a preview, and iterator
conventions are the kind of thing I'd want settled before a codebase commits to
them.
What I took from it
Mostly the versioning mechanism, because it generalises.
Legacy behaviour survives in languages for decades because fixing it means
breaking somebody, and you can't break them at a moment of their choosing. Tie
the semantics to a version declared in the project's own manifest and the
breaking change becomes an opt-in, with a natural review point. Go could do this
because go.mod already had a language version in it for other reasons.
I wrote last year that what keeps me returning to Go is that the things it's strict about are the things that break. Same instinct here, pointed at its own history. The loop variable was a known wart for a decade, and the fix waited until there was a way to ship it that didn't surprise anybody.
Tagged


