I have one channel and the receiver is main. I spawn multiple goroutines that each send a string over the channel.
Now, this causes a deadlock because I didn't close the channel properly using the close function. The thing is, I have no idea how many goroutines will be created, so there's no way to know when to close the channel.
I've tried using WaitGroup, the problem is, I've read that I can't use Add in the goroutine and that I should use wg.Add(1) in the main process/goroutine, I've tried using Add in the parent goroutine spawning the child goroutine, that also caused a deadlock
package main
import (
"fmt"
"sync"
)
var i = 0
func doSomething(ch chan string, wg sync.WaitGroup) {
defer wg.Done()
ch <- fmt.Sprintf("doSomething: %d", i)
i++
if i == 10 {return}
wg.Add(1)
go doSomething(ch, wg)
}
func main() {
ch := make(chan string)
var wg sync.WaitGroup
wg.Add(1)
go doSomething(ch, wg)
wg.Wait()
for s := range ch {
fmt.Println(s)
}
}
Now, this is just a test code, so, assume that we don't know that we will create 10 goroutines only, assume that it's unknown at runtime, here I get a deadlock error instantly without any output, if I don't use WorkGroup I get the error at before printing the 10th string (because I didn't close the channel)
I've also tried not spawning a goroutine for each function call and instead use one goroutine for all the recursive calls (starting from main), and to close the channel, I made an anonymous function for go that first calls the doSomething function then calls close, so all the recursive calls will have been evaluated and we will for sure know when to close the channel. But, this is now what I'm trying to accomplish, I'm trying to get the unknown number of goroutines to work together and close the channel after they're done somehow.