Commit 2ccd20a6 authored by Rob Pike's avatar Rob Pike

tutorial: replace the forever loops with finite counts in sieve programs.

Fixes #1742.
I hope.

Also this picks up an update to go_tutorial.html that should already have happened.

R=brainman, rsc, peterGo
CC=golang-dev
https://golang.org/cl/4452050
parent 5eeaca12
...@@ -741,26 +741,27 @@ Building on the <code>file</code> package, here's a simple version of the Unix u ...@@ -741,26 +741,27 @@ Building on the <code>file</code> package, here's a simple version of the Unix u
24 case nr &gt; 0: 24 case nr &gt; 0:
25 if nw, ew := file.Stdout.Write(buf[0:nr]); nw != nr { 25 if nw, ew := file.Stdout.Write(buf[0:nr]); nw != nr {
26 fmt.Fprintf(os.Stderr, &quot;cat: error writing from %s: %s\n&quot;, f.String(), ew.String()) 26 fmt.Fprintf(os.Stderr, &quot;cat: error writing from %s: %s\n&quot;, f.String(), ew.String())
27 } 27 os.Exit(1)
28 } 28 }
29 } 29 }
30 } 30 }
31 }
<p> <p>
32 func main() { 33 func main() {
33 flag.Parse() // Scans the arg list and sets up flags 34 flag.Parse() // Scans the arg list and sets up flags
34 if flag.NArg() == 0 { 35 if flag.NArg() == 0 {
35 cat(file.Stdin) 36 cat(file.Stdin)
36 } 37 }
37 for i := 0; i &lt; flag.NArg(); i++ { 38 for i := 0; i &lt; flag.NArg(); i++ {
38 f, err := file.Open(flag.Arg(i)) 39 f, err := file.Open(flag.Arg(i))
39 if f == nil { 40 if f == nil {
40 fmt.Fprintf(os.Stderr, &quot;cat: can't open %s: error %s\n&quot;, flag.Arg(i), err) 41 fmt.Fprintf(os.Stderr, &quot;cat: can't open %s: error %s\n&quot;, flag.Arg(i), err)
41 os.Exit(1) 42 os.Exit(1)
42 } 43 }
43 cat(f) 44 cat(f)
44 f.Close() 45 f.Close()
45 }
46 } 46 }
47 }
</pre> </pre>
<p> <p>
By now this should be easy to follow, but the <code>switch</code> statement introduces some By now this should be easy to follow, but the <code>switch</code> statement introduces some
...@@ -858,10 +859,11 @@ and use it from within a mostly unchanged <code>cat()</code> function: ...@@ -858,10 +859,11 @@ and use it from within a mostly unchanged <code>cat()</code> function:
67 nw, ew := file.Stdout.Write(buf[0:nr]) 67 nw, ew := file.Stdout.Write(buf[0:nr])
68 if nw != nr { 68 if nw != nr {
69 fmt.Fprintf(os.Stderr, &quot;cat: error writing from %s: %s\n&quot;, r.String(), ew.String()) 69 fmt.Fprintf(os.Stderr, &quot;cat: error writing from %s: %s\n&quot;, r.String(), ew.String())
70 } 70 os.Exit(1)
71 } 71 }
72 } 72 }
73 } 73 }
74 }
</pre> </pre>
<p> <p>
(We could also do the wrapping in <code>main</code> and leave <code>cat()</code> mostly alone, except (We could also do the wrapping in <code>main</code> and leave <code>cat()</code> mostly alone, except
...@@ -1238,7 +1240,7 @@ together: ...@@ -1238,7 +1240,7 @@ together:
28 func main() { 28 func main() {
29 ch := make(chan int) // Create a new channel. 29 ch := make(chan int) // Create a new channel.
30 go generate(ch) // Start generate() as a goroutine. 30 go generate(ch) // Start generate() as a goroutine.
31 for { 31 for i := 0; i &lt; 100; i++ { // Print the first hundred primes.
32 prime := &lt;-ch 32 prime := &lt;-ch
33 fmt.Println(prime) 33 fmt.Println(prime)
34 ch1 := make(chan int) 34 ch1 := make(chan int)
...@@ -1318,7 +1320,7 @@ Now <code>main</code>'s interface to the prime sieve is a channel of primes: ...@@ -1318,7 +1320,7 @@ Now <code>main</code>'s interface to the prime sieve is a channel of primes:
<pre> <!-- progs/sieve1.go /func.main/ /^}/ --> <pre> <!-- progs/sieve1.go /func.main/ /^}/ -->
46 func main() { 46 func main() {
47 primes := sieve() 47 primes := sieve()
48 for { 48 for i := 0; i &lt; 100; i++ { // Print the first hundred primes.
49 fmt.Println(&lt;-primes) 49 fmt.Println(&lt;-primes)
50 } 50 }
51 } 51 }
......
...@@ -28,7 +28,7 @@ func filter(in, out chan int, prime int) { ...@@ -28,7 +28,7 @@ func filter(in, out chan int, prime int) {
func main() { func main() {
ch := make(chan int) // Create a new channel. ch := make(chan int) // Create a new channel.
go generate(ch) // Start generate() as a goroutine. go generate(ch) // Start generate() as a goroutine.
for { for i := 0; i < 100; i++ { // Print the first hundred primes.
prime := <-ch prime := <-ch
fmt.Println(prime) fmt.Println(prime)
ch1 := make(chan int) ch1 := make(chan int)
......
...@@ -45,7 +45,7 @@ func sieve() chan int { ...@@ -45,7 +45,7 @@ func sieve() chan int {
func main() { func main() {
primes := sieve() primes := sieve()
for { for i := 0; i < 100; i++ { // Print the first hundred primes.
fmt.Println(<-primes) fmt.Println(<-primes)
} }
} }
...@@ -74,7 +74,6 @@ gomake clean ...@@ -74,7 +74,6 @@ gomake clean
time gomake ogle time gomake ogle
) || exit $? ) || exit $?
[ "$GOHOSTOS" == windows ] ||
(xcd ../doc/progs (xcd ../doc/progs
time ./run time ./run
) || exit $? ) || exit $?
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment