Concurrency in Go
#
Communicating Sequential ProcessesGo concurrency is based on Communicating Sequential Processes, a paper from C.A.R. Hoare, in 1978.
Go has 4 tools to achieve concurrency:
- Go routines
- Channels
- Select
- Sync package
We'll go over all 4 of them in depth.
#
Goroutines#
ChannelsChannels can be thought of as a bucket chain. We have a sender, passing buckets down the chain, the buffer, which is the chain itself, and then the receiver. The buffer is optional. It helps to think of channels as data streams, and there are a lot of patterns that follow from this:
- Fan-out
- Funnel
- Turnout
There's a lot of situations where channels can be blocking. Here are some examples:
unbuffered := make(chan int)
// This blocks, no receiverunbuffered <- 1// This also blocks, no senderi := <- unbuffered
// We have to add a receiver in a separate goroutine:go func() { i := <- unbuffered}()
// This will synchronise both goroutinesunbuffered <- 1
Channels can be closed. This should always happen on the sender side!