1
0
mirror of https://github.com/golang/go synced 2024-11-25 02:07:58 -07:00

- fixed sieve.go example (channel directions were wrong)

- cosmetic adjustments

R=r
DELTA=30  (0 added, 0 deleted, 30 changed)
OCL=35010
CL=35012
This commit is contained in:
Robert Griesemer 2009-09-25 14:11:03 -07:00
parent 3c8a1de720
commit e1e7619f01

View File

@ -416,7 +416,7 @@ the (possibly multi-byte) UTF-8 encoding of individual <i>characters</i>.
Thus inside a string literal <code>\377</code> and <code>\xFF</code> represent 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>, a single byte of value <code>0xFF</code>=255, while <code>ÿ</code>,
<code>\u00FF</code>, <code>\U000000FF</code> and <code>\xc3\xbf</code> represent <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. U+00FF.
</p> </p>
@ -1095,8 +1095,8 @@ to receive. This constraint is called a channel's <i>direction</i>; either
</p> </p>
<pre> <pre>
chan T // can be used to send and receive values of type T chan T // can be used to send and receive values of type T
chan &lt;- float // can only be used to send floats chan&lt;- float // can only be used to send floats
&lt;-chan int // can only be used to receive ints &lt;-chan int // can only be used to receive ints
</pre> </pre>
@ -2796,7 +2796,7 @@ a channel and a value (expression):
</p> </p>
<pre> <pre>
ch <- 3 ch &lt;- 3
</pre> </pre>
<p> <p>
@ -2817,10 +2817,10 @@ These two examples are equivalent:
</p> </p>
<pre> <pre>
ok := ch <- 3; ok := ch &lt;- 3;
if ok { print("sent") } else { print("not sent") } if ok { print("sent") } else { print("not sent") }
if ch <- 3 { print("sent") } else { print("not sent") } if ch &lt;- 3 { print("sent") } else { print("not sent") }
</pre> </pre>
<p> <p>
@ -2836,7 +2836,7 @@ is the element type of the channel.
</p> </p>
<pre> <pre>
<-ch &lt;-ch
</pre> </pre>
<p> <p>
@ -2847,10 +2847,10 @@ discarded.
</p> </p>
<pre> <pre>
v1 := <-ch v1 := &lt;-ch
v2 = <-ch v2 = &lt;-ch
f(<-ch) f(&lt;-ch)
<-strobe // wait until clock pulse &lt;-strobe // wait until clock pulse
</pre> </pre>
<p> <p>
@ -2858,9 +2858,9 @@ If a receive expression is used in an assignment or initialization of the form
</p> </p>
<pre> <pre>
x, ok = <-ch x, ok = &lt;-ch
x, ok := <-ch x, ok := &lt;-ch
var x, ok = <-ch var x, ok = &lt;-ch
</pre> </pre>
<p> <p>
@ -3079,12 +3079,12 @@ order. Otherwise, the order of evaluation is unspecified.
For example, in the assignment For example, in the assignment
</p> </p>
<pre> <pre>
y[f()], ok = g(h(), i() + x[j()], <-c), k() y[f()], ok = g(h(), i() + x[j()], &lt;-c), k()
</pre> </pre>
<p> <p>
the function calls and communication happen in the order the function calls and communication happen in the order
<code>f()</code>, <code>h()</code>, <code>i()</code>, <code>j()</code>, <code>f()</code>, <code>h()</code>, <code>i()</code>, <code>j()</code>,
<code><-c</code>, <code>g()</code>, and <code>k()</code>. <code>&lt;-c</code>, <code>g()</code>, and <code>k()</code>.
However, the order of those events compared to the evaluation However, the order of those events compared to the evaluation
and indexing of <code>x</code> and the evaluation and indexing of <code>x</code> and the evaluation
of <code>y</code> is not specified. of <code>y</code> is not specified.
@ -3166,7 +3166,7 @@ ExpressionStmt = Expression .
<pre> <pre>
f(x+y) f(x+y)
<-ch &lt;-ch
</pre> </pre>
@ -3212,7 +3212,7 @@ or the <a href="#Blank_identifier">blank identifier</a>.
x = 1 x = 1
*p = f() *p = f()
a[i] = 23 a[i] = 23
k = <-ch k = &lt;-ch
</pre> </pre>
<p> <p>
@ -3648,7 +3648,7 @@ for the invoked function to complete.
<pre> <pre>
go Server() go Server()
go func(ch chan <- bool) { for { sleep(10); ch <- true; }} (c) go func(ch chan&lt;- bool) { for { sleep(10); ch &lt;- true; }} (c)
</pre> </pre>
@ -4252,37 +4252,37 @@ package main
import "fmt" import "fmt"
// Send the sequence 2, 3, 4, ... to channel 'ch'. // Send the sequence 2, 3, 4, ... to channel 'ch'.
func generate(ch chan <- int) { func generate(ch chan&lt;- int) {
for i := 2; ; i++ { for i := 2; ; i++ {
ch <- i // Send 'i' to channel 'ch'. ch &lt;- i; // Send 'i' to channel 'ch'.
} }
} }
// Copy the values from channel 'in' to channel 'out', // Copy the values from channel 'in' to channel 'out',
// removing those divisible by 'prime'. // removing those divisible by 'prime'.
func filter(src chan <- int, dst <-chan int, prime int) { func filter(src &lt;-chan int, dst chan&lt;- int, prime int) {
for i := range src { // Loop over values received from 'src'. for i := range src { // Loop over values received from 'src'.
if i % prime != 0 { if i%prime != 0 {
dst <- i // Send 'i' to channel 'dst'. dst &lt;- i; // Send 'i' to channel 'dst'.
} }
} }
} }
// The prime sieve: Daisy-chain filter processes together. // The prime sieve: Daisy-chain filter processes together.
func sieve() { func sieve() {
ch := make(chan int); // Create a new channel. ch := make(chan int); // Create a new channel.
go generate(ch); // Start generate() as a subprocess. go generate(ch); // Start generate() as a subprocess.
for { for {
prime := <-ch; prime := &lt;-ch;
fmt.Print(prime, "\n"); fmt.Print(prime, "\n");
ch1 := make(chan int); ch1 := make(chan int);
go filter(ch, ch1, prime); go filter(ch, ch1, prime);
ch = ch1 ch = ch1;
} }
} }
func main() { func main() {
sieve() sieve();
} }
</pre> </pre>