Skip to content

Commit f022c75

Browse files
docs: add Go concurrency, channels, and runtime articles (#134)
Adds five Go articles to the knowledge base: goroutines and main exit, main running as a goroutine, the G/GMP runtime scheduler architecture, channels, and buffered vs unbuffered channels. Links them under a new Concurrency section in the Go hub. Also escapes a mermaid label in go-errors.md that failed to parse due to a quote inside the node label.
1 parent 5d73af0 commit f022c75

7 files changed

Lines changed: 875 additions & 2 deletions
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
sidebar_position: 37
3+
---
4+
5+
# Buffered vs Unbuffered Channels in Go
6+
7+
## TLDR
8+
9+
- Two differences, and they are the same difference seen from two sides.
10+
- **Unbuffered** (`make(chan T)`): a send and its receive are one synchronous handoff. The sender blocks until a receiver is present, and when the send returns the receiver has the value.
11+
- **Buffered** (`make(chan T, n)`): a send drops the value into the buffer and returns immediately if there is room. The receiver can join later, up to `n` values late.
12+
- So with unbuffered you can conclude "the listener got it." With buffered you cannot, because the value may still be sitting in the buffer.
13+
- "Later" is not "never": once the buffer is full, the next send blocks until someone receives.
14+
- Pick unbuffered when the reader must be there and you want the handoff guaranteed. Pick buffered when the reader joins later and you do not want to block now, and choose `n` as how far ahead you are willing to run.
15+
16+
## The one difference
17+
18+
An unbuffered channel has no storage. For a send to complete, a receiver must be parked on the channel at that instant. The two goroutines meet, the value passes directly from one to the other, and both proceed. This is why the send is a **synchronous handoff**: the moment your `ch <- v` returns, you know the other goroutine took `v`.
19+
20+
A buffered channel has storage. A send copies the value into the buffer and returns if there is room, whether or not anyone is reading. The receiver can come along later and drain it. This is an **asynchronous drop-off**.
21+
22+
<div style={{display: 'flex', justifyContent: 'center'}}>
23+
24+
```mermaid
25+
flowchart TD
26+
A{"unbuffered send"} --> A1["blocks until a receiver is ready"]
27+
A1 --> A2["value passes directly, sender knows it was received"]
28+
B{"buffered send, room available"} --> B1["copies value into buffer and returns"]
29+
B1 --> B2["receiver may not have run yet"]
30+
B2 --> B3["sender cannot assume it was received"]
31+
```
32+
33+
</div>
34+
35+
## What that means when you send
36+
37+
The practical consequence is what you said: the mode decides what the sender is allowed to conclude.
38+
39+
With an unbuffered channel, a returned send means the receiver has the value. That makes it useful for a handoff, for a signal, or for request/response, anywhere "it has arrived" must be true.
40+
41+
With a buffered channel, a returned send only means the value was accepted. The receiver may run much later, or not yet at all. That is the point when the reader joins later and you do not want to block now. It is also the trap: if you send and then assume the other side has acted, you can be wrong by up to `n` values.
42+
43+
This is the whole difference in two programs:
44+
45+
```go
46+
// unbuffered: this send returns only once the receiver has taken "ping"
47+
ch := make(chan string)
48+
go func() { time.Sleep(100 * time.Millisecond); fmt.Println("got", <-ch) }()
49+
ch <- "ping"
50+
fmt.Println("send returned, receiver has it")
51+
52+
// buffered: this send returns immediately, receiver has not run yet
53+
buf := make(chan string, 1)
54+
go func() { time.Sleep(100 * time.Millisecond); fmt.Println("got", <-buf) }()
55+
buf <- "ping"
56+
fmt.Println("send returned, value still in the buffer")
57+
```
58+
59+
```
60+
send returned, receiver has it
61+
got ping
62+
```
63+
64+
```
65+
send returned, value still in the buffer
66+
got ping
67+
```
68+
69+
## A buffer only lets the reader be late, not absent
70+
71+
A buffer does not remove the need for a receiver. It removes the need for one *right now*, for the first `n` sends. After that the buffer is full and the next send blocks, exactly like an unbuffered one.
72+
73+
```go
74+
ch := make(chan int, 2)
75+
ch <- 1 // ok, buffer has room
76+
ch <- 2 // ok, buffer now full
77+
ch <- 3 // blocks until someone receives
78+
```
79+
80+
If nobody ever receives, that third send never completes and the program reports `fatal error: all goroutines are asleep - deadlock!`. So the buffer buys you a bounded amount of look-ahead, and `n` is the size of that allowance. If the reader might join much later, or never, a buffer is the wrong fix; you want a reader that always drains, or a way to cancel the send.
81+
82+
## Summary
83+
84+
Unbuffered is a synchronous handoff: the sender waits for a receiver and, when the send returns, the value has been received. Buffered is an asynchronous drop-off: the send returns once the value is in the buffer, so the receiver can join later, up to `n` values late, and the sender cannot assume it was received. That is the entire difference. Pick unbuffered when you need the arrival guarantee and the reader must be present; pick buffered when the reader will join later and you do not want to block now, sizing the buffer as how far ahead you are willing to run.
85+
86+
## Sources
87+
88+
- The Go Programming Language Specification, [Channel types](https://go.dev/ref/spec#Channel_types) - "If the capacity is zero or absent, the channel is unbuffered and communication succeeds only when both a sender and receiver are ready. Otherwise, the channel is buffered and communication succeeds without blocking if the buffer is not full (sends) or not empty (receives)."
89+
- The Go Programming Language Specification, [Send statements](https://go.dev/ref/spec#Send_statements) - "A send on an unbuffered channel can proceed if a receiver is ready. A send on a buffered channel can proceed if there is room in the buffer."
90+
- Effective Go, [Channels](https://go.dev/doc/effective_go#channels) - "Unbuffered channels combine communication ... with synchronization"; "If the channel is unbuffered, the sender blocks until the receiver has received the value. If the channel has a buffer, the sender blocks only until the value has been copied to the buffer."
91+
- Rob Pike, [Go Concurrency Patterns](https://go.dev/talks/2012/concurrency.slide) - "Buffering removes synchronization. Buffering makes them more like Erlang's mailboxes. Buffered channels can be important for some problems but they are more subtle to reason about."
92+
- Go by Example, [Channel Buffering](https://gobyexample.com/channel-buffering) - "By default channels are unbuffered ... Buffered channels accept a limited number of values without a corresponding receiver for those values."
93+
- A Tour of Go, [Buffered Channels](https://go.dev/tour/concurrency/3) - "Sends to a buffered channel block only when the buffer is full. Receives block when the buffer is empty."

0 commit comments

Comments
 (0)