Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@ import (
"sync"
)

// This will works fine on Go.v1.22 (range notes part)
// See docs for details : https://tip.golang.org/doc/go1.22
func main() {
var wg sync.WaitGroup
// This code will not cause a race condition, since `salutation` is a new variable for each iteration of the range loop.
for _, salutation := range []string{"hello", "greetings", "good day"} {
wg.Add(1)
go func(salutation string) { // <1>
go func() {
defer wg.Done()
fmt.Println(salutation)
}(salutation) // <2>
}()
}
wg.Wait()
}