VOOZH about

URL: https://thenewstack.io/golang-1-22-redefines-the-for-loop-for-easier-concurrency/

⇱ Golang 1.22 Redefines the For Loop for Easier Concurrency - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-04-04 13:30:03
Golang 1.22 Redefines the For Loop for Easier Concurrency
Go

Golang 1.22 Redefines the For Loop for Easier Concurrency

Golang 1.22 fixes a quirk in the for loop that has long-thwarted closure users. There's also a simpler way to iterate and even a promise of Go generators.
Apr 4th, 2024 1:30pm by Joab Jackson
👁 Featued image for: Golang 1.22 Redefines the For Loop for Easier Concurrency
Feature image by Prawny from Pixabay.  

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
3
5

…Rather than this, where Go 1.21 would just keep repeating the initial variable value:

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.

TRENDING STORIES
Joab Jackson is a senior editor for The New Stack, covering cloud native computing and system operations. He has reported on IT infrastructure and development for over 30 years, including stints at IDG and Government Computer News. Before that, he...
Read more from Joab Jackson
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.