go:concurrency:select
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| go:concurrency:select [2026/01/30 23:10] – [Syntax] phong2018 | go:concurrency:select [2026/01/30 23:14] (current) – [Using len(ch) to decide send/receive] phong2018 | ||
|---|---|---|---|
| Line 30: | Line 30: | ||
| ==== Syntax ==== | ==== Syntax ==== | ||
| - | <code text> select { case ch <- value: // send succeeded default: // send would block, so skip } </ | + | <code text> |
| + | select { | ||
| + | | ||
| + | | ||
| + | } | ||
| + | </ | ||
| ==== Explanation ==== | ==== Explanation ==== | ||
| Line 56: | Line 61: | ||
| ==== Syntax ==== | ==== Syntax ==== | ||
| - | < | + | < |
| + | select { | ||
| + | | ||
| + | | ||
| + | } | ||
| + | </ | ||
| ==== Explanation ==== | ==== Explanation ==== | ||
| Line 108: | Line 118: | ||
| ==== Ignoring the result of append-like behavior ==== | ==== Ignoring the result of append-like behavior ==== | ||
| - | < | + | < |
| + | select { | ||
| + | | ||
| + | | ||
| + | } | ||
| + | </ | ||
| If the send fails, the value is silently dropped. | If the send fails, the value is silently dropped. | ||
| Line 119: | Line 134: | ||
| This is unsafe: | This is unsafe: | ||
| - | < | + | < |
| + | if len(ch) < cap(ch) { | ||
| + | ch <- v // race condition. | ||
| + | } | ||
| + | </ | ||
| 📌 race condition /reɪs kənˈdɪʃ.ən/ | 📌 race condition /reɪs kənˈdɪʃ.ən/ | ||
go/concurrency/select.1769814605.txt.gz · Last modified: by phong2018
