Writing concurrent programs
Up to this point, the discussions about goroutines and channels remained deliberately separated to ensure that each topic is properly covered. However, the true power of channels and goroutines are realized when they are combined to create concurrent programs, as covered in this section.
Synchronization
One of the primary uses of channels is synchronization between running goroutines. To illustrate this use case, let us examine the following code, which implements a word histogram. The program reads the words from the data
slice then, on a separate goroutine, collects the occurrence of each word:
func main() { data := []string{ "The yellow fish swims slowly in the water", "The brown dog barks loudly after a drink ...", ...
Get Go: Design Patterns for Real-World Projects now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.