开俩个协程,一个协程生产数据,另一个协程对数据进行处理,处理完后再把数据发回去,使用管道如何实现?

题目来源:京东

答案:

该问题适合使用pipeline模式(pipeline是一系列将数据输入,执行操作并将数据传回的系统,我们称这些操作都是pipeline的一个stage),代码如下:

  1. package main
  2. import (
  3. "fmt"
  4. "math/rand"
  5. )
  6. func main() {
  7. // 随机数生成器
  8. producer := func(num int, done <-chan struct{}) <-chan int {
  9. produceStream := make(chan int)
  10. go func() {
  11. defer close(produceStream)
  12. for i := 0; i < num; i++ {
  13. select {
  14. case <-done:
  15. return
  16. case produceStream <- rand.Intn(99):
  17. }
  18. }
  19. }()
  20. return produceStream
  21. }
  22. // 随机数倍增器
  23. consumer := func(done <-chan struct{}, produceStream <-chan int) <-chan int {
  24. consumerStream := make(chan int)
  25. go func() {
  26. defer close(consumerStream)
  27. for v := range produceStream {
  28. select {
  29. case <-done:
  30. return
  31. case consumerStream <- v * 2:
  32. }
  33. }
  34. }()
  35. return consumerStream
  36. }
  37. done := make(chan struct{})
  38. defer close(done)
  39. produceStream := producer(10, done)
  40. consumerStream := consumer(done, produceStream)
  41. for output := range consumerStream {
  42. fmt.Println("Output: ", output)
  43. }
  44. }