package main
import (
"flag"
"fmt"
"strconv"
)
var count *int
func init() {
count = flag.Int("num", 22, "计算素数")
}
func main() {
flag.Parse()
origin, wait := make(chan int), make(chan struct{})
Processor(origin, wait)
for num := 2; num < *count; num++ {
origin <- num
}
close(origin)
<-wait
}
func Processor(seq chan int, wait chan struct{}) {
go func() {
prime, ok := <-seq
if !ok {
close(wait)
return
}
fmt.Println(prime)
out := make(chan int)
Processor(out, wait)
for num := range seq {
fmt.Println("write " +strconv.Itoa(num)+"==%="+strconv.Itoa(prime))
if num%prime != 0 {
out <- num
}
}
close(out)
}()
}
有疑问加站长微信联系(非本文作者)