Skip to content

Commit

Permalink
TableDrivenTests: link to doc/faq for loopvar changes
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
Commit-Queue: Ian Lance Taylor <[email protected]>
Auto-Submit: Ian Lance Taylor <[email protected]>
  • Loading branch information
seankhliao authored and gopherbot committed Jul 6, 2024
1 parent a9394ff commit ae427b9
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CommonMistakes.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ When new programmers start using Go or when old Go programmers start using a new

# 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
Expand Down Expand Up @@ -74,6 +76,8 @@ The same issue can be demonstrated also when the loop variable is being used in

# 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
Expand Down Expand Up @@ -145,3 +149,4 @@ func (v *val) MyMethod() {
}
```

[loopvar-blog]: https://go.dev/blog/loopvar-preview
4 changes: 2 additions & 2 deletions TableDrivenTests.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ tests := map[string]struct {
}

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 {
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestTLog(t *testing.T) {
{"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)
Expand Down
1 change: 0 additions & 1 deletion index.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ Here are some of the places where you can find Gophers online. To get a sense of
- [GoStrings](GoStrings)
- [String Matching](http://blog.gopheracademy.com/advent-2014/string-matching/)
- [Comments](Comments)
- [CommonMistakes](CommonMistakes)
- [Errors](Errors)
- [GcToolchainTricks](GcToolchainTricks)
- [InterfaceSlice](InterfaceSlice)
Expand Down

0 comments on commit ae427b9

Please sign in to comment.