Golang 1.22 Redefines the For Loop for Easier Concurrency
The Golang community is all abuzz over a few recent changes the programming language’s most basic control flow statements, the for loop.
In the most recent version, Go programming language, v1.22, each spin of a for loop now iterates variables with each pass. In all previous versions of Go, declared variables defining for loop did not change.
This was problematic. The change addresses a subtle, but frequently-encountered problem, especially when using closures in concurrent programs.
“This kind of unintended sharing bug hits all Go programmers, whether they are just starting to learn Go or have been using it for a decade,” The Go Wiki explained.
Now, this range in Go 1.22:
will print these numbers…
|
1 2 3 |
1 3 5 |
…Rather than this, where Go 1.21 would just keep repeating the initial variable value:
|
1 2 3 |
6 6 6 |
This a “fundamental change in the way variables are scoped,” explained a Dreams of Code instructional YouTube video. “It changes the loop variable to be per-iteration scoped rather than scoped to the entire loop.”
This will make things easier for those cases that need looping changes, such as in parallel testing, or for closures in concurrent programming.
In fact, those learning Go are often taught to work around this quirk, Dreams of Code noted. Often coders forget the manual intervention to fix this issue, and clean-up work through a linter is then required.
One service that got bit badly by this bug was the Let’s Encrypt certificate service, which in 2020, had to invalidate 3 million certificates as a result of miss-applying the Golang for loop. D’Oh!
Easy Integers in Golang
Another change to the venerable for loop: One may now range over integers. This move cuts down on the boilerplate, Carlana Johnson noted in a Changelog Go Time podcast about the release.
Here is a little syntactic sugar for regular users to enjoy as a treat. To date, they have been defining the range of a for loop with the rather cumbersome old-C style three-expression format:
With Go 1.22, you can type…
…and, just as before, the program will iterate 5 times, until the value of 4 is reached (0-1-2-3-4).
A small change, this streamlining can nonetheless save keystrokes, especially in cases such as benchmarking software, for instance, where the same set of instructions are repeatedly copied.
Go Time 302: What’s new in Go 1.22 – Listen on Changelog.com
Go’s Baby Step into Functional Programming
Soon, Golang might be able to range over functions as well. The new version of the programming language previews a change, in range-over-function iterators.
You can even enable this feature in 1.22 by adding it into your build GOEXPERIMENT=rangefunc.
“Let’s go! Let’s go! Let’s go! That’s great,” anonymous tech observer ThePrimeagen applauded in a Twitch commentary.
This capability is similar to generator functions in other languages. In a nutshell, it can turn any function into an iterator.
Since the introduction of generics in Go, developers have created many libraries. Go however did not have a pattern to iterate across these generics.
The range statement can now call a function that yields an iterator value.
“It’s a little bit funky to look at. It bends your mind a little bit,” Johnson admitted. “But when you write the code it’s not so bad.”
“You can essentially just write a little function that returns a function, and now you got an iterator,” she said.
This is good, for say, iterating over a binary tree.
Go is already about 95% functional, ThePrimeagen enthused: “It just needs better iterator support and it’s fully there.”
Golang is updated every six months. The next release is due in August.