In Go, the `select` statement lets a goroutine wait on multiple communication operations. A `select` blocks untill one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.
```go
ch1 := make(chan int)
ch2 := make(chan int, 100)
x := 0
for {
select {
case ch1 <- x:
x := x + 1
case y <- ch2:
fmt.Println("Received on channel")
default:
fmt.Println("Default")
}
}
```
In Fluid, we should be able to do the same:
```python
ch1 = fluid.make_chan(dtype=INT)
ch2 = fluid.make_chan(dtype=INT, 100)
sel = fluid.select()
with sel.case(ch1, 'w', X):
fluid.layers.increment(X)
with sel.case(ch2, 'r', Y):
fluid.print("Received on Channel")
with sel.default():
fluid.print("Default")
```
In the above code snippet, `X` and `Y` are variables. Now let us look at each of these statements one by one.
- `sel.case(ch1, 'w', X)` : This specifies that we are writing to `ch1` and we want to write the integer in variable `X` to the channel. The character `w` is used here to make the syntax familar to write syntax in Python I/O.
- `sel.case(ch2, 'r', Y)` : This specifies that we would like to read the result from `ch2` into variable `Y`. The character `r` is used here to make the syntax familar to read syntax in Python I/O.
- `sel.default()` : This is equivalent to the default in Go `select`. If none of the channels are ready for read or write, then the fluid code in the default block will be executed.
<spanid="select"></span><h3>Select<aclass="headerlink"href="#select"title="Permalink to this headline">¶</a></h3>
<spanid="select"></span><h3>Select<aclass="headerlink"href="#select"title="Permalink to this headline">¶</a></h3>
<p>In Go, the <codeclass="docutils literal"><spanclass="pre">select</span></code> statement lets a goroutine wait on multiple communication operations. A <codeclass="docutils literal"><spanclass="pre">select</span></code> blocks untill one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.</p>
<spanclass="nx">fmt</span><spanclass="p">.</span><spanclass="nx">Println</span><spanclass="p">(</span><spanclass="s">"Received on channel"</span><spanclass="p">)</span>
<spanclass="n">fluid</span><spanclass="o">.</span><spanclass="k">print</span><spanclass="p">(</span><spanclass="s2">"Received on Channel"</span><spanclass="p">)</span>
<p>In the above code snippet, <codeclass="docutils literal"><spanclass="pre">X</span></code> and <codeclass="docutils literal"><spanclass="pre">Y</span></code> are variables. Now let us look at each of these statements one by one.</p>
<ulclass="simple">
<li><codeclass="docutils literal"><spanclass="pre">sel.case(ch1,</span><spanclass="pre">'w',</span><spanclass="pre">X)</span></code> : This specifies that we are writing to <codeclass="docutils literal"><spanclass="pre">ch1</span></code> and we want to write the integer in variable <codeclass="docutils literal"><spanclass="pre">X</span></code> to the channel. The character <codeclass="docutils literal"><spanclass="pre">w</span></code> is used here to make the syntax familar to write syntax in Python I/O.</li>
<li><codeclass="docutils literal"><spanclass="pre">sel.case(ch2,</span><spanclass="pre">'r',</span><spanclass="pre">Y)</span></code> : This specifies that we would like to read the result from <codeclass="docutils literal"><spanclass="pre">ch2</span></code> into variable <codeclass="docutils literal"><spanclass="pre">Y</span></code>. The character <codeclass="docutils literal"><spanclass="pre">r</span></code> is used here to make the syntax familar to read syntax in Python I/O.</li>
<li><codeclass="docutils literal"><spanclass="pre">sel.default()</span></code> : This is equivalent to the default in Go <codeclass="docutils literal"><spanclass="pre">select</span></code>. If none of the channels are ready for read or write, then the fluid code in the default block will be executed.</li>
In Go, the `select` statement lets a goroutine wait on multiple communication operations. A `select` blocks untill one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.
```go
ch1 := make(chan int)
ch2 := make(chan int, 100)
x := 0
for {
select {
case ch1 <- x:
x := x + 1
case y <- ch2:
fmt.Println("Received on channel")
default:
fmt.Println("Default")
}
}
```
In Fluid, we should be able to do the same:
```python
ch1 = fluid.make_chan(dtype=INT)
ch2 = fluid.make_chan(dtype=INT, 100)
sel = fluid.select()
with sel.case(ch1, 'w', X):
fluid.layers.increment(X)
with sel.case(ch2, 'r', Y):
fluid.print("Received on Channel")
with sel.default():
fluid.print("Default")
```
In the above code snippet, `X` and `Y` are variables. Now let us look at each of these statements one by one.
- `sel.case(ch1, 'w', X)` : This specifies that we are writing to `ch1` and we want to write the integer in variable `X` to the channel. The character `w` is used here to make the syntax familar to write syntax in Python I/O.
- `sel.case(ch2, 'r', Y)` : This specifies that we would like to read the result from `ch2` into variable `Y`. The character `r` is used here to make the syntax familar to read syntax in Python I/O.
- `sel.default()` : This is equivalent to the default in Go `select`. If none of the channels are ready for read or write, then the fluid code in the default block will be executed.
<p>In Go, the <codeclass="docutils literal"><spanclass="pre">select</span></code> statement lets a goroutine wait on multiple communication operations. A <codeclass="docutils literal"><spanclass="pre">select</span></code> blocks untill one of its cases can run, then it executes that case. It chooses one at random if multiple are ready.</p>
<spanclass="nx">fmt</span><spanclass="p">.</span><spanclass="nx">Println</span><spanclass="p">(</span><spanclass="s">"Received on channel"</span><spanclass="p">)</span>
<spanclass="n">fluid</span><spanclass="o">.</span><spanclass="k">print</span><spanclass="p">(</span><spanclass="s2">"Received on Channel"</span><spanclass="p">)</span>
<p>In the above code snippet, <codeclass="docutils literal"><spanclass="pre">X</span></code> and <codeclass="docutils literal"><spanclass="pre">Y</span></code> are variables. Now let us look at each of these statements one by one.</p>
<ulclass="simple">
<li><codeclass="docutils literal"><spanclass="pre">sel.case(ch1,</span><spanclass="pre">'w',</span><spanclass="pre">X)</span></code> : This specifies that we are writing to <codeclass="docutils literal"><spanclass="pre">ch1</span></code> and we want to write the integer in variable <codeclass="docutils literal"><spanclass="pre">X</span></code> to the channel. The character <codeclass="docutils literal"><spanclass="pre">w</span></code> is used here to make the syntax familar to write syntax in Python I/O.</li>
<li><codeclass="docutils literal"><spanclass="pre">sel.case(ch2,</span><spanclass="pre">'r',</span><spanclass="pre">Y)</span></code> : This specifies that we would like to read the result from <codeclass="docutils literal"><spanclass="pre">ch2</span></code> into variable <codeclass="docutils literal"><spanclass="pre">Y</span></code>. The character <codeclass="docutils literal"><spanclass="pre">r</span></code> is used here to make the syntax familar to read syntax in Python I/O.</li>
<li><codeclass="docutils literal"><spanclass="pre">sel.default()</span></code> : This is equivalent to the default in Go <codeclass="docutils literal"><spanclass="pre">select</span></code>. If none of the channels are ready for read or write, then the fluid code in the default block will be executed.</li>