Thus inside a string literal <code>\377</code> and <code>\xFF</code> represent
a single byte of value <code>0xFF</code>=255, while <code>ΓΏ</code>,
<code>\u00FF</code>, <code>\U000000FF</code> and <code>\xc3\xbf</code> represent
-the two bytes <code>0xc3 0xbf</code> of the UTF-8 encoding of character
+the two bytes <code>0xc3</code> <code>0xbf</code> of the UTF-8 encoding of character
U+00FF.
</p>
</p>
<pre>
-chan T // can be used to send and receive values of type T
-chan <- float // can only be used to send floats
+chan T // can be used to send and receive values of type T
+chan<- float // can only be used to send floats
<-chan int // can only be used to receive ints
</pre>
</p>
<pre>
-ch <- 3
+ch <- 3
</pre>
<p>
</p>
<pre>
-ok := ch <- 3;
+ok := ch <- 3;
if ok { print("sent") } else { print("not sent") }
-if ch <- 3 { print("sent") } else { print("not sent") }
+if ch <- 3 { print("sent") } else { print("not sent") }
</pre>
<p>
</p>
<pre>
-<-ch
+<-ch
</pre>
<p>
</p>
<pre>
-v1 := <-ch
-v2 = <-ch
-f(<-ch)
-<-strobe // wait until clock pulse
+v1 := <-ch
+v2 = <-ch
+f(<-ch)
+<-strobe // wait until clock pulse
</pre>
<p>
</p>
<pre>
-x, ok = <-ch
-x, ok := <-ch
-var x, ok = <-ch
+x, ok = <-ch
+x, ok := <-ch
+var x, ok = <-ch
</pre>
<p>
For example, in the assignment
</p>
<pre>
-y[f()], ok = g(h(), i() + x[j()], <-c), k()
+y[f()], ok = g(h(), i() + x[j()], <-c), k()
</pre>
<p>
the function calls and communication happen in the order
<code>f()</code>, <code>h()</code>, <code>i()</code>, <code>j()</code>,
-<code><-c</code>, <code>g()</code>, and <code>k()</code>.
+<code><-c</code>, <code>g()</code>, and <code>k()</code>.
However, the order of those events compared to the evaluation
and indexing of <code>x</code> and the evaluation
of <code>y</code> is not specified.
<pre>
f(x+y)
-<-ch
+<-ch
</pre>
x = 1
*p = f()
a[i] = 23
-k = <-ch
+k = <-ch
</pre>
<p>
<pre>
go Server()
-go func(ch chan <- bool) { for { sleep(10); ch <- true; }} (c)
+go func(ch chan<- bool) { for { sleep(10); ch <- true; }} (c)
</pre>
import "fmt"
// Send the sequence 2, 3, 4, ... to channel 'ch'.
-func generate(ch chan <- int) {
+func generate(ch chan<- int) {
for i := 2; ; i++ {
- ch <- i // Send 'i' to channel 'ch'.
+ ch <- i; // Send 'i' to channel 'ch'.
}
}
// Copy the values from channel 'in' to channel 'out',
// removing those divisible by 'prime'.
-func filter(src chan <- int, dst <-chan int, prime int) {
- for i := range src { // Loop over values received from 'src'.
- if i % prime != 0 {
- dst <- i // Send 'i' to channel 'dst'.
+func filter(src <-chan int, dst chan<- int, prime int) {
+ for i := range src { // Loop over values received from 'src'.
+ if i%prime != 0 {
+ dst <- i; // Send 'i' to channel 'dst'.
}
}
}
// The prime sieve: Daisy-chain filter processes together.
func sieve() {
- ch := make(chan int); // Create a new channel.
- go generate(ch); // Start generate() as a subprocess.
+ ch := make(chan int); // Create a new channel.
+ go generate(ch); // Start generate() as a subprocess.
for {
- prime := <-ch;
+ prime := <-ch;
fmt.Print(prime, "\n");
ch1 := make(chan int);
go filter(ch, ch1, prime);
- ch = ch1
+ ch = ch1;
}
}
func main() {
- sieve()
+ sieve();
}
</pre>