Created
November 22, 2023 12:12
-
-
Save yumed15/5f8d44e1c00f10bc71d5e830018816c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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