TableDrivenTests: link to doc/faq for loopvar changes
Link CommonMistakes to loopvar change blogpost.
Fixes golang/go#65658
Fixes golang/go#65659
Change-Id: I8b5f4c1d1cfaeab248db25b76ff3657491ca8068
Reviewed-on: https://go-review.googlesource.com/c/wiki/+/563215
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Commit-Queue: Ian Lance Taylor <iant@golang.org>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
diff --git a/CommonMistakes.md b/CommonMistakes.md
index 8449ad2..a20d4d9 100644
--- a/CommonMistakes.md
+++ b/CommonMistakes.md
@@ -15,6 +15,8 @@
# Using reference to loop iterator variable
+**NOTE:** the following section applies to Go < 1.22. Go versions >= 1.22 uses variables scoped to the iteration, see [Fixing For Loops in Go 1.22][loopvar-blog] for details.
+
In Go, the loop iterator variable is a single variable that takes different values in each loop iteration. This is very efficient, but might lead to unintended behavior when used incorrectly. For example, see the following program:
```go
@@ -74,6 +76,8 @@
# Using goroutines on loop iterator variables
+**NOTE:** the following section applies to Go < 1.22. Go versions >= 1.22 uses variables scoped to the iteration, see [Fixing For Loops in Go 1.22][loopvar-blog] for details.
+
When iterating in Go, one might attempt to use goroutines to process data in parallel. For example, you might write something like this, using a closure:
```go
@@ -145,3 +149,4 @@
}
```
+[loopvar-blog]: https://go.dev/blog/loopvar-preview
diff --git a/TableDrivenTests.md b/TableDrivenTests.md
index d756765..b17cc44 100644
--- a/TableDrivenTests.md
+++ b/TableDrivenTests.md
@@ -77,7 +77,7 @@
}
for name, test := range tests {
- test := test
+ // test := test // NOTE: uncomment for Go < 1.22, see /doc/faq#closures_and_goroutines
t.Run(name, func(t *testing.T) {
t.Parallel()
if got, expected := reverse(test.input), test.result; got != expected {
@@ -114,7 +114,7 @@
{"test 4"},
}
for _, test := range tests {
- test := test // NOTE: /wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
+ // test := test // NOTE: uncomment for Go < 1.22, see /doc/faq#closures_and_goroutines
t.Run(test.name, func(t *testing.T) {
t.Parallel() // marks each test case as capable of running in parallel with each other
t.Log(test.name)
diff --git a/index.md b/index.md
index 6a1f97b..0513a50 100644
--- a/index.md
+++ b/index.md
@@ -166,7 +166,6 @@
- [GoStrings](GoStrings)
- [String Matching](http://blog.gopheracademy.com/advent-2014/string-matching/)
- [Comments](Comments)
-- [CommonMistakes](CommonMistakes)
- [Errors](Errors)
- [GcToolchainTricks](GcToolchainTricks)
- [InterfaceSlice](InterfaceSlice)