github.com/traefik/yaegi@v0.15.1/_test/sieve.go (about) 1 // A concurrent prime sieve 2 3 package main 4 5 // Send the sequence 2, 3, 4, ... to channel 'ch'. 6 func Generate(ch chan<- int) { 7 for i := 2; ; i++ { 8 ch <- i // Send 'i' to channel 'ch'. 9 } 10 } 11 12 // Copy the values from channel 'in' to channel 'out', 13 // removing those divisible by 'prime'. 14 func Filter(in <-chan int, out chan<- int, prime int) { 15 for { 16 i := <-in // Receive value from 'in'. 17 if i%prime != 0 { 18 out <- i // Send 'i' to 'out'. 19 } 20 } 21 } 22 23 // The prime sieve: Daisy-chain Filter processes. 24 func main() { 25 ch := make(chan int) // Create a new channel. 26 go Generate(ch) // Launch Generate goroutine. 27 28 for i := 0; i < 10; i++ { 29 prime := <-ch 30 println(prime) 31 ch1 := make(chan int) 32 go Filter(ch, ch1, prime) 33 ch = ch1 34 } 35 } 36 37 // Output: 38 // 2 39 // 3 40 // 5 41 // 7 42 // 11 43 // 13 44 // 17 45 // 19 46 // 23 47 // 29