Skip to content

Instantly share code, notes, and snippets.

@yumed15
Created November 22, 2023 12:12
Show Gist options
  • Save yumed15/5f8d44e1c00f10bc71d5e830018816c7 to your computer and use it in GitHub Desktop.
Save yumed15/5f8d44e1c00f10bc71d5e830018816c7 to your computer and use it in GitHub Desktop.
// GOOD
var wg sync.WaitGroup
for _, salutation := range []string{"hello", "greetings", "good day"} {
wg.Add(1)
go func(salutation string) {
defer wg.Done()
fmt.Println(salutation)
}(salutation) // <-- we pass in the current iteration's variable to the closure.
// a copy of the string struct is made
// when the goroutine is run, we'll be refering to the proper string
}
wg.Wait()
// good day
// hello
// greetings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment