From 2ccd20a646f3e6b46c4f3e779c3833071b735886 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Wed, 27 Apr 2011 09:59:27 -0700 Subject: [PATCH] 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 --- doc/go_tutorial.html | 52 +++++++++++++++++++++++--------------------- doc/progs/sieve.go | 2 +- doc/progs/sieve1.go | 2 +- src/run.bash | 1 - 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/doc/go_tutorial.html b/doc/go_tutorial.html index 4d9c63e1587..c87254ecb37 100644 --- a/doc/go_tutorial.html +++ b/doc/go_tutorial.html @@ -741,26 +741,27 @@ Building on the file package, here's a simple version of the Unix u 24 case nr > 0: 25 if nw, ew := file.Stdout.Write(buf[0:nr]); nw != nr { 26 fmt.Fprintf(os.Stderr, "cat: error writing from %s: %s\n", f.String(), ew.String()) -27 } -28 } -29 } -30 } +27 os.Exit(1) +28 } +29 } +30 } +31 }

-32 func main() { -33 flag.Parse() // Scans the arg list and sets up flags -34 if flag.NArg() == 0 { -35 cat(file.Stdin) -36 } -37 for i := 0; i < flag.NArg(); i++ { -38 f, err := file.Open(flag.Arg(i)) -39 if f == nil { -40 fmt.Fprintf(os.Stderr, "cat: can't open %s: error %s\n", flag.Arg(i), err) -41 os.Exit(1) -42 } -43 cat(f) -44 f.Close() -45 } -46 } +33 func main() { +34 flag.Parse() // Scans the arg list and sets up flags +35 if flag.NArg() == 0 { +36 cat(file.Stdin) +37 } +38 for i := 0; i < flag.NArg(); i++ { +39 f, err := file.Open(flag.Arg(i)) +40 if f == nil { +41 fmt.Fprintf(os.Stderr, "cat: can't open %s: error %s\n", flag.Arg(i), err) +42 os.Exit(1) +43 } +44 cat(f) +45 f.Close() +46 } +47 }

By now this should be easy to follow, but the switch statement introduces some @@ -858,10 +859,11 @@ and use it from within a mostly unchanged cat() function: 67 nw, ew := file.Stdout.Write(buf[0:nr]) 68 if nw != nr { 69 fmt.Fprintf(os.Stderr, "cat: error writing from %s: %s\n", r.String(), ew.String()) -70 } -71 } -72 } -73 } +70 os.Exit(1) +71 } +72 } +73 } +74 }

(We could also do the wrapping in main and leave cat() mostly alone, except @@ -1238,7 +1240,7 @@ together: 28 func main() { 29 ch := make(chan int) // Create a new channel. 30 go generate(ch) // Start generate() as a goroutine. -31 for { +31 for i := 0; i < 100; i++ { // Print the first hundred primes. 32 prime := <-ch 33 fmt.Println(prime) 34 ch1 := make(chan int) @@ -1318,7 +1320,7 @@ Now main's interface to the prime sieve is a channel of primes:

 
 46    func main() {
 47        primes := sieve()
-48        for {
+48        for i := 0; i < 100; i++ { // Print the first hundred primes.
 49            fmt.Println(<-primes)
 50        }
 51    }
diff --git a/doc/progs/sieve.go b/doc/progs/sieve.go
index fb649e0496c..c7c3e781222 100644
--- a/doc/progs/sieve.go
+++ b/doc/progs/sieve.go
@@ -28,7 +28,7 @@ func filter(in, out chan int, prime int) {
 func main() {
 	ch := make(chan int)  // Create a new channel.
 	go generate(ch)  // Start generate() as a goroutine.
-	for {
+	for i := 0; i < 100; i++ { // Print the first hundred primes.
 		prime := <-ch
 		fmt.Println(prime)
 		ch1 := make(chan int)
diff --git a/doc/progs/sieve1.go b/doc/progs/sieve1.go
index 71468d06ec7..e785e203557 100644
--- a/doc/progs/sieve1.go
+++ b/doc/progs/sieve1.go
@@ -45,7 +45,7 @@ func sieve() chan int {
 
 func main() {
 	primes := sieve()
-	for {
+	for i := 0; i < 100; i++ { // Print the first hundred primes.
 		fmt.Println(<-primes)
 	}
 }
diff --git a/src/run.bash b/src/run.bash
index ccdbc1aa9c1..bb3d06c45ca 100755
--- a/src/run.bash
+++ b/src/run.bash
@@ -74,7 +74,6 @@ gomake clean
 time gomake ogle
 ) || exit $?
 
-[ "$GOHOSTOS" == windows ] ||
 (xcd ../doc/progs
 time ./run
 ) || exit $?