I am looking for a way to execute asynchronously two functions in go, which returns different results and errors, waiting for them to complete and print both results. Also, if one of the functions returned an error, I do not want to wait for another function and just print the error. For example, I have the following functions:
func methodInt(error bool) (int, error) {
<-time.NewTimer(time.Millisecond * 100).C
if error {
return 0, errors.New("Some error")
} else {
return 1, nil
}
}
func methodString(error bool) (string, error) {
<-time.NewTimer(time.Millisecond * 120).C
if error {
return "", errors.New("Some error")
} else {
return "Some result", nil
}
}
Here https://play.golang.org/p/-8StYapmlg is how I implemented it, but I have too much code, I think. It can be simplified using the {} interface, but I don’t want to go that route. I want something simpler, for example, in C # can be implemented using async / await. There is probably some library that simplifies such an operation.
: ! , ! WaitGroup. , , , async . , - #. , go async, , , , , , , async, json, go -kit.