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:11] – [Syntax] phong2018go:concurrency:select [2026/01/30 23:14] (current) – [Using len(ch) to decide send/receive] phong2018
Line 31: Line 31:
  
 <code text>  <code text> 
-<code go>  +select {  
-select { case ch <- value: // send succeeded default: // send would block, so skip } </code>+    case ch <- value: // send succeeded  
 +    default: // send would block, so skip  
 + 
 +</code>
  
 ==== Explanation ==== ==== Explanation ====
Line 58: 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 110: 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 121: 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.1769814706.txt.gz · Last modified: by phong2018