3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Test basic concurrency: the classic prime sieve.
8 // Do not run - loops forever.
12 // Send the sequence 2, 3, 4, ... to channel 'ch'.
13 func Generate(ch chan<- int) {
15 ch <- i // Send 'i' to channel 'ch'.
19 // Copy the values from channel 'in' to channel 'out',
20 // removing those divisible by 'prime'.
21 func Filter(in <-chan int, out chan<- int, prime int) {
23 i := <-in // Receive value of new variable 'i' from 'in'.
25 out <- i // Send 'i' to channel 'out'.
30 // The prime sieve: Daisy-chain Filter processes together.
32 ch := make(chan int) // Create a new channel.
33 go Generate(ch) // Start Generate() as a subprocess.
38 go Filter(ch, ch1, prime)