package main import ( "fmt" "math/rand" ) func main() { pFn := func(done <-chan interface{}, fn func() int) <-chan int { valueStream := make(chan int) go func() { defer close(valueStream) for { select { case <-done: return case valueStream <- fn(): } } }() return valueStream } task := func(done <-chan interface{}, valueStream <-chan int, num int) <-chan int { taskStream := make(chan int) go func() { defer close(taskStream) for i := 0; i < num; i++ { select { case <-done: return case value := <-valueStream: taskStream <- value } } }() return taskStream } done := make(chan interface{}) defer close(done) fn := func() int { return rand.Int() } for i := range task(done, pFn(done, fn), 10) { fmt.Println(i) } }
【版权申明】未经博主同意,谢绝转载!(请尊重原创,博主保留追究权) https://www.cnblogs.com/facetwitter/p/18293852
标签:pipeline,并发,func,博主,go,main From: https://www.cnblogs.com/facetwitter/p/18293852