Skip to content

Instantly share code, notes, and snippets.

@danfoust
Created December 30, 2020 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danfoust/e9166285cc1927b29667a29c04d9afe4 to your computer and use it in GitHub Desktop.
Save danfoust/e9166285cc1927b29667a29c04d9afe4 to your computer and use it in GitHub Desktop.
Work will only take as long as slowest process
// Scatter
c := make(chan result, 10)
for i := 0; i < cap(c); i++ {
go func() {
val, err := process()
c <- result{val, err}
}()
}
// Gather
var total int
for i := 0; i < cap(c); i++ {
res := <-c
if res.err != nil {
total += res.val
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment