New Blog Post From the
@golang team: Fixing For Loops in Go 1.22
You can read the original blog post here:
go.dev/blog/loopvar-preview, summary below.
The problem
The below should print a,b,c right?
It will actually print c,c,c. This is due to
go.dev/doc/faq#closures_and_…
Its also possible to face this issue without concurrency:
In Go 1.22, the Go team are changing for loops to have per iteration scope instead of per loop scope.
This is kind of a big change, so this will only apply to go programs that declare go 1.22 in their go.mod file, or use the GOEXPERIMENT=loopvar environment variable.
Google have been running this change in production since May 2023 and have yet to see any issues with it.
Another great milestone and another great blog post by the Go team!