In below code snipped I'm trying to loop some data and for each iteration start new go routine. I have created error channel and if some errors appear goroutine it should write it to the channel. So I have 2 questions:
- How to catch if all goroutines done and close error channel for breaking from "for" loop to continue main function execution?
- if in each routine I'll call some functions which appends values to global variables will there be any issues with race condition? If yes, how to handle this.
package main
import (
"fmt"
)
var errc = make(chan error, 15)
func fetchAll() {
var N = 15
for i := 0; i < N; i++ {
go func(i int) {
err := error(nil)
if err != nil {
errc <- err
}
// Doing some stuff there
fmt.Println(i)
}(i)
}
}
func main() {
fetchAll()
for {
select {
case err := <-errc:
fmt.Println(err)
break
}
}
fmt.Println("Finished All")
}