Created
December 30, 2020 12:55
-
-
Save danfoust/e9166285cc1927b29667a29c04d9afe4 to your computer and use it in GitHub Desktop.
Work will only take as long as slowest process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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