详见代码块。
```
package main
import (
"fmt"
)
func GenerateNatural() chan int {
ch := make(chan int)
go func() {
for i := 2; ; i++ {
fmt.Println("GenerateNatural", i)
ch <- i
}
}()
return ch
}
func PrimeFilter(in <-chan int, prime int) chan int {
out := make(chan int)
go func() {
for {
fmt.Println()
if i := <-in; i%prime != 0 {
out <- i
}
}
}()
return out
}
func main() {
ch := GenerateNatural()
for i := 0; i < 100; i++ {
prime := <-ch
fmt.Printf("%v:%v\n", i+1, prime)
ch = PrimeFilter(ch, prime)
}
}
```
这个是官方白皮书的上的经典案例
https://mp.weixin.qq.com/s?__biz=MzIzMjYyMTM1Nw==&mid=2247483686&idx=1&sn=4b5466ab6e8c52fdd99125d6cccd65cf&chksm=e8935564dfe4dc7234f039abe55fd4ed6fb507798c89eac7ed30b26782eee5506e68cd83bd90&scene=0&xtrack=1#rd
之前写过流程
#1