
Ricky Lin
Thursday, June 22, 2023
15 Rules of Channels and Their Implementation
Operation rules
| Operation | nil | Closed channel | Open buffered channel | Open unbuffered channel |
|---|
| Close | panic | panic | Close succeeds; buffered values can be read. After buffer drains, further reads return the zero value of the channel type. | Close succeeds; further reads return the zero value of the channel type. |
| Receive | Block | Non-blocking; returns the zero value of the channel type | Non-blocking; reads values normally | Block |
| Send | Block | panic | Non-blocking; writes values normally | Block |
Compile-time rules
| Operation | Channel type | Result |
|---|
| Receive | Send-only channel | Compile error |
| Send | Receive-only channel | Compile error |
| Close | Receive-only channel | Compile error |