-
Book Overview & Buying
-
Table Of Contents
Go Recipes for Developers
By :
More often than not, multiple goroutines have to communicate and coordinate to distribute work, manage state, and collate results of computations. Channels are the preferred mechanism for this. A channel is a synchronization mechanism with an optional fixed-size buffer.
Tip:
The following recipes show channels that are closed. Closing a channel is a method for communicating end of data. If you do not close a channel, it will be garbage collected when it is no longer referenced. In other words, you don't need to close a channel if you don't need to signal end of data to the receivers.
A goroutine can send to a channel if there is another goroutine waiting to receive from it, or in the case of a buffered channel, there is space available in the channel buffer. Otherwise, the goroutine is blocked until it can send.
A goroutine can receive from a channel if there is another...