Go 1.22's for-range semantic change is overall good, but 3-clause-for 
change creates more gotchas. See:
* https://go101.org/blog/2024-03-01-for-loop-semantic-changes-in-go-1.22.html
* https://github.com/golang/go/issues/66070
* https://github.com/golang/go/issues/66156
* https://github.com/golang/go/issues/66388
* https://github.com/spq/pkappa2/issues/238
  
https://github.com/spq/pkappa2/blob/960201260a88fd62221166e32f9415a768072f52/internal/index/writer.go#L791

The gotchas will appear when go version is upgraded from 1.22- to 1.22+.
Some of them are hard to detect in time.
Go officials never has a plan to develop tools to detect such gotchas.

So, personally, I recommended you to keep the "i := i" lines in 
3-clause-for loops, even in 1.22+ error.
It is best to let compiler instead of humam to remove such lines.
On Tuesday, March 10, 2026 at 6:28:14 PM UTC Slawomir Pryczek wrote:

> Hey Guys, i have some simple golang code which i run trough claude, it 
> claims i have "a classic goroutine closure bug as all_params is passed to 
> goroutine by reference", but instance of this variable should be created in 
> each loop iteration as it's defined in the loop's body, not outside. So it 
> doesn't matter (as i understand) if that's a reference or not.
>
> Loop Variable Capture bug was fixed in go 1.22 and it's 2 years old 
> already, and I don't need that code to compile in some ancient golang 
> versions. By just capturing variables everywhere instead of passing, 
> everything's more readable. Is that considered correct / safe approach or 
> considered bad practice?
>
> Apart from the main topic i have impression that this AI is just 
> hallucinating and telling me what I want to hear. Eg. when i put some code 
> in there looking for bugs it often finds (probably non-existing) bugs. Then 
> I claim these bugs are not there and this code should be working as 
> expected, and it (again) completely agrees with me telling me that there is 
> no issue :D
>
>  You have some good (better) model for that ?
>
> --------------------------------------------
> const MAX_PARALLEL_CALLS = 10
>
> ret := make([]string, MAX_PARALLEL_CALLS)
> if data.GetParamExists("mdata-0") { // check if we're doing multi-call
> wg := sync.WaitGroup{}
>
> for i := 0; i < MAX_PARALLEL_CALLS; i++ {
> pname := fmt.Sprintf("mdata-%d", i)
> if !data.GetParamExists(pname) {
> break
> }
> all_params := get_all_params(data.GetParamBUnsafe(pname, []byte{}))
>
> wg.Add(1)
> go func() {
> ret[i] = this.runOpenRTB(all_params, is_debug)
> wg.Done()
> }()
> }
> wg.Wait()

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/golang-nuts/1c3c5a67-3a9f-49b8-96b9-56beafc084aen%40googlegroups.com.

Reply via email to