美文网首页
通过Chan来实现素数生成

通过Chan来实现素数生成

作者: bocsoft | 来源:发表于2018-12-07 14:57 被阅读0次
package main

import "fmt"

func main() {
    origin, wait := make(chan int), make(chan struct{})
    Processor(origin, wait)
    for num := 2; num < 10000; 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 {
            if num%prime != 0 {
                out <- num
            }
        }
        close(out)
    }()
}



相关文章

网友评论

      本文标题:通过Chan来实现素数生成

      本文链接:https://www.haomeiwen.com/subject/ldmthqtx.html