Unbuffered channels in Go
An unbuffered send blocks until another goroutine receives: that is the handshake.
func handshake() string {
done := make(chan string)
go func() {
done <- "worker finished"
}()
message := <-done // blocks until the send happens
return message
}
How it works
- Send and receive rendezvous; neither proceeds alone.
- A send with no receiver deadlocks, which the runtime detects.
- That synchronization is the point, not a limitation.
Keywords and builtins used here
chanfuncgomakereturnstring
The run, in numbers
- Lines
- 10
- Characters to type
- 163
- Tokens
- 32
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 19 seconds.
Step 1 of 4 in Channels, step 4 of 25 in Concurrency.