snapshot of https://github.com/uber-go/guide.git (master, 2026-09-15; blobs > 131072B omitted)
This commit is contained in:
29
src/channel-size.md
Normal file
29
src/channel-size.md
Normal file
@@ -0,0 +1,29 @@
|
||||
# Channel Size is One or None
|
||||
|
||||
Channels should usually have a size of one or be unbuffered. By default,
|
||||
channels are unbuffered and have a size of zero. Any other size
|
||||
must be subject to a high level of scrutiny. Consider how the size is
|
||||
determined, what prevents the channel from filling up under load and blocking
|
||||
writers, and what happens when this occurs.
|
||||
|
||||
<table>
|
||||
<thead><tr><th>Bad</th><th>Good</th></tr></thead>
|
||||
<tbody>
|
||||
<tr><td>
|
||||
|
||||
```go
|
||||
// Ought to be enough for anybody!
|
||||
c := make(chan int, 64)
|
||||
```
|
||||
|
||||
</td><td>
|
||||
|
||||
```go
|
||||
// Size of one
|
||||
c := make(chan int, 1) // or
|
||||
// Unbuffered channel, size of zero
|
||||
c := make(chan int)
|
||||
```
|
||||
|
||||
</td></tr>
|
||||
</tbody></table>
|
||||
Reference in New Issue
Block a user