A collection of utilities for concurrent programming in Go.
func someWork(id int) chan error {
errs := make(chan error)
go func(result *Result) {
// ...
errs <- err
}(&Result{ID: id})
return errs
}
for err := range asyncutil.Collect(
someWork(1),
someWork(2),
) {
if err != nil {
fmt.Println("Error:", err)
}
}$ go test -bench . -count 1 .
goos: darwin
goarch: amd64
pkg: github.com/sanggonlee/asyncutil
5 functions running, each taking 50 milliseconds:
BenchmarkCollect_5_functions_of_50_milliseconds_each-8 21 52155152 ns/op
BenchmarkSequential_5_functions_of_50_milliseconds_each-8 4 262524126 ns/op
2 functions running, each taking 30 milliseconds:
BenchmarkCollect_2_functions_of_30_milliseconds_each-8 38 32424379 ns/op
BenchmarkSequential_2_functions_of_30_milliseconds_each-8 18 64462037 ns/op
5 functions running, each taking 200 milliseconds:
BenchmarkCollect_5_functions_of_200_milliseconds_each-8 5 202613911 ns/op
BenchmarkSequential_5_functions_of_200_milliseconds_each-8 1 1012167372 ns/op