I am trying to complete several tasks at the same time and get a result or error.
//data channels
ch := make(chan int)
ch2 := make(chan int)
ch2 := make(chan int)
//error channels
errCh := make(chan error)
errCh2 := make(chan error)
errCh3 := make(chan error)
//functions
go taskF(ch, errCh)
go taskF2(ch2, errCh2)
go taskF3(ch3, errCh3)
Then I start checking every error. If there is any mistake, we print it differently, we print the result of each task
err := <-errCh
if err != nil{
print('we have an error ')
return
}
err2 := <-errCh2
if err2 != nil{
print('we have an error 2')
return
}
err3 := <-errCh3
if err3!= nil{
print('we have an error 3')
return
}
Then, if there is no error, I collect the values returned, although each channel
task := <-ch
task2 := <-ch2
task3 := <-ch3
print("task %v task2 %v task3 %v", task, task2, task3)
I am wondering if I am doing this correctly. I worry that the code is pretty verbose. I was thinking of using buffered channels for errors, but I cannot figure out how to check all errors. I think it would be nice to somehow synchronize the errors inside the goroutines, so if there is an error in one goroutine, the other goroutines stop, but I do not know how to do this in unlocking.