User Tools

Site Tools


go:concurrency:select

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
go:concurrency:select [2026/01/30 23:10] – [Syntax] phong2018go: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>+<code text>  
 +select {  
 +    case ch <- value: // send succeeded  
 +    default: // send would block, so skip  
 + 
 +</code>
  
 ==== Explanation ==== ==== Explanation ====
Line 56: Line 61:
 ==== Syntax ==== ==== Syntax ====
  
-<code go> select { case value := <-ch: // receive succeeded default: // no value available } </code>+<code text 
 +select {  
 +    case value := <-ch: // receive succeeded  
 +    default: // no value available  
 + 
 +</code>
  
 ==== Explanation ==== ==== Explanation ====
Line 108: Line 118:
 ==== Ignoring the result of append-like behavior ==== ==== Ignoring the result of append-like behavior ====
  
-<code go> select { case ch <- v: default: } </code>+<code text 
 +select {  
 +    case ch <- v:  
 +    default:  
 + 
 +</code>
  
 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:
  
-<code go> if len(ch) < cap(ch) { ch <- v // race condition } </code>+<code text 
 +if len(ch) < cap(ch) {  
 +   ch <- v // race condition.  
 + 
 +</code>
  
 📌 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