Skip to content

Commit

Permalink
doc/articles/race_detector: update for Go 1.22 loop variable changes
Browse files Browse the repository at this point in the history
One of the examples was no longer racy. Change the example
to use a variable declared outside of the loop, to make it racy.

Fixes golang/go#69261

Change-Id: I79d5c9372d4badbdaccdea53e8b395291805dc47
Reviewed-on: https://go-review.googlesource.com/c/website/+/610975
Reviewed-by: Dmitri Shuralyov <[email protected]>
Commit-Queue: Ian Lance Taylor <[email protected]>
Reviewed-by: Ian Lance Taylor <[email protected]>
Auto-Submit: Ian Lance Taylor <[email protected]>
Reviewed-by: Dmitri Shuralyov <[email protected]>
Auto-Submit: Ian Lance Taylor <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
  • Loading branch information
ianlancetaylor authored and gopherbot committed Sep 5, 2024
1 parent e264570 commit ac2f5a4
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions _content/doc/articles/race_detector.html
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ <h3 id="Race_on_loop_counter">Race on loop counter</h3>
func main() {
var wg sync.WaitGroup
wg.Add(5)
for i := 0; i &lt; 5; i++ {
var i int
for i = 0; i &lt; 5; i++ {
go func() {
fmt.Println(i) // Not the 'i' you are looking for.
wg.Done()
Expand All @@ -221,7 +222,8 @@ <h3 id="Race_on_loop_counter">Race on loop counter</h3>
func main() {
var wg sync.WaitGroup
wg.Add(5)
for i := 0; i &lt; 5; i++ {
var i int
for i = 0; i &lt; 5; i++ {
go func(j int) {
fmt.Println(j) // Good. Read local copy of the loop counter.
wg.Done()
Expand Down

0 comments on commit ac2f5a4

Please sign in to comment.