Answer the question
In order to leave comments, you need to log in
Is this a feature or a bug?
There will always be 2
package main
import (
"fmt"
"sync"
)
func main() {
q := []string{"1", "2"}
wg := new(sync.WaitGroup)
for _, val := range q {
wg.Add(1)
go func(){
fmt.Println(val)
wg.Done()
}()
}
wg.Wait()
}
Answer the question
In order to leave comments, you need to log in
read about closures ( frequent mistakes of beginners ), how a variable is captured so that the result is expected here is a code example
package main
import (
"fmt"
"sync"
)
func main() {
q := []string{"1", "2"}
wg := new(sync.WaitGroup)
for _, val := range q {
wg.Add(1)
go func(tVal string){
fmt.Println(tVal)
wg.Done()
}(val)
}
wg.Wait()
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question