I have a question about go routines.
My code:
func main() {
ok := make(chan bool, 1)
i := 0
fmt.Println("Starting...")
for i <= 3 {
fmt.Println("Loop: ", i)
go long(ok, i)
time.Sleep(1 * time.Second)
i = i + 1
select {
case _ = <-ok:
default:
fmt.Println("Default")
}
}
fmt.Println("Done...")
}
func long(c chan bool, i int){
fmt.Println("Inside long: ", i)
time.Sleep(3 * time.Second)
fmt.Println("Done with loop: ", i)
c <- true
}
This gives me output:
Starting...
Loop: 0
Inside long: 0
Default
Loop: 1
Inside long: 1
Default
Loop: 2
Inside long: 2
Done with loop: 0
Loop: 3
Inside long: 3
Done with loop: 1
Done...
Because I use the default
in the select
, the channel is non blocking. And the main function exits and so do all current routines. I then read about sync
and WaitGrops
.
func main() {
ok := make(chan bool, 1)
var wg sync.WaitGroup
i := 0
fmt.Println("Starting...")
for i <= 3 {
fmt.Println("Loop: ", i)
wg.Add(1)
go long(ok, i)
time.Sleep(1 * time.Second)
i = i + 1
select {
case _ = <-ok:
default:
fmt.Println("Default")
}
}
wg.Wait()
fmt.Println("Done...")
}
Which gives me:
Starting...
Loop: 0
Inside long: 0
Default
Loop: 1
Inside long: 1
Default
Loop: 2
Inside long: 2
Done with loop: 0
Loop: 3
Inside long: 3
Done with loop: 1
Done with loop: 2
Done with loop: 3
We are now closer to my desired execution which is: The for loop makes all it calls to the function, and then I get the result async. This would be wonderful if it all worked. But an error is generated:
fatal error: all goroutines are asleep - deadlock!
Why is that and how should I fix it? (Is it possible to fix without knowing how many times wg.Add() will be executed?)