MDGSF Software Engineer

[GO] 生成素数

2017-09-20
GO

package main

import "fmt"

func main() {
    origin, wait := make(chan int), make(chan struct{})
    Processor(origin, wait)
    for num := 2; num < 1000; 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)
    }()
}

weixingongzhonghao

上一篇 [GO] log

Comments

Content