package main
import (
"fmt"
"math/rand"
"sync"
)
func main() {
numberChan1 := make(chan int64, 3)
numberChan2 := make(chan int64, 3)
numberChan3 := make(chan int64, 3)
var waitGroup sync.WaitGroup
waitGroup.Add(3)
go func() {
for n := range numberChan1 {
if n%2 == 0 {
numberChan2 <- n
} else {
fmt.Printf("Filter %d. [filter 2]\n", n)
}
}
close(numberChan2)
waitGroup.Done()
}()
go func() {
for n := range numberChan2 {
if n%5 == 0 {
numberChan3 <- n
} else {
fmt.Printf("Filter %d. [filter 5]\n", n)
}
}
close(numberChan3)
waitGroup.Done()
}()
go func() {
for n := range numberChan3 {
fmt.Println("n = ", n)
}
waitGroup.Done()
}()
for i := 0; i < 100; i++ {
numberChan1 <- rand.Int63n(100)
}
close(numberChan1)
waitGroup.Wait()
}
有疑问加站长微信联系(非本文作者)