~/Gather Scatter Pattern in Go Explained
Sep 14, 2019
The gather scatter pattern, also called scatter gather, is a parallel computation strategy in Go which divides work into independent tasks using goroutines, then collects the results after all are done.
Scatter Phase
Work is split across multiple goroutines. Each goroutine processes a chunk of data independently.
Gather Phase
Results from all goroutines are combined. Commonly, a channel is used for goroutines to report their result to a central goroutine that aggregates the data.
Example
|
|
This pattern is useful for parallelizing CPU-bound or IO-bound tasks such as web scraping, batch processing, and parallel API calls.
Links
- Go goroutines
- Go channels
- Article on the pattern