diff options
author | Russ Cox <rsc@golang.org> | 2009-03-20 11:32:58 -0700 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2009-03-20 11:32:58 -0700 |
commit | 5231226d0894e7ac4b241dadef97c31aea85f042 (patch) | |
tree | ea48563056f767780a96052cd26e853088223652 /test/chan | |
parent | ae8458449bc9670093c4a473c7666478cd761081 (diff) | |
download | golang-5231226d0894e7ac4b241dadef97c31aea85f042.tar.gz |
range over channels.
also fix multiple-evaluation bug in range over arrays.
R=ken
OCL=26576
CL=26576
Diffstat (limited to 'test/chan')
-rw-r--r-- | test/chan/sieve.go | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/test/chan/sieve.go b/test/chan/sieve.go index 0cebdc641..7d06e98fa 100644 --- a/test/chan/sieve.go +++ b/test/chan/sieve.go @@ -19,8 +19,7 @@ func Generate(ch chan<- int) { // Copy the values from channel 'in' to channel 'out', // removing those divisible by 'prime'. func Filter(in <-chan int, out chan<- int, prime int) { - for { - i := <-in; // Receive value of new variable 'i' from 'in'. + for i := range in { // Loop over values received from 'in'. if i % prime != 0 { out <- i // Send 'i' to channel 'out'. } @@ -32,6 +31,7 @@ func Sieve(primes chan<- int) { ch := make(chan int); // Create a new channel. go Generate(ch); // Start Generate() as a subprocess. for { + // Note that ch is different on each iteration. prime := <-ch; primes <- prime; ch1 := make(chan int); @@ -45,7 +45,7 @@ func main() { go Sieve(primes); a := []int{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97}; for i := 0; i < len(a); i++ { - if <-primes != a[i] { panic(a[i])} + if x := <-primes; x != a[i] { panic(x, " != ", a[i]) } } sys.Exit(0); } |