i am new to the Go language, and looking for some help for the concurrency model. say I want to make 2 http calls concurrently, and wait for both of them to finish then process/merge the response data. here is the code i have
func main() {
var wg sync.WaitGroup
wg.Add(2)
c1 := make(chan string)
c2 := make(chan string)
go foo(c1, &wg)
go bar(c2, &wg)
wg.Wait()
foo := <-c1
bar := <-c2
fmt.Println("foo: ", foo)
fmt.Println("bar: ", bar)
}
func foo(c chan string, wg *sync.WaitGroup) {
defer wg.Done()
c <- "foo"
}
func bar(c chan string, wg *sync.WaitGroup) {
defer wg.Done()
c <- "bar"
}
however when i run it, it gives the error fatal error: all goroutines are asleep - deadlock!
I can get it working without the WaitGroup, but just curious why this gets to the deadlock, and whats the best way of doing it