Does this cause a memory leak or other kind of problem?
func example() err {
var wg sync.WaitGroup
wg.Add(2)
errorChannel := make(chan error, 2) // can hold two
go func() {
defer wg.Done()
var err error = doThing1()
if err != nil {
errorChannel <- err
}
}()
go func() {
defer wg.Done()
var err error = doThing2()
if err != nil {
errorChannel <- err
}
}()
wg.Wait()
close(errorChannel)
err := <-errorChannel
if err != nil {
return err // with returing the first error is enought..
}
}
Excuse if may code may be wrong, I am starting to play with it.
Bonus question: Is necessary to close the channel in the context?