(ctx context.Context, cs ...chan interface{})
| 53 | } |
| 54 | |
| 55 | func merge(ctx context.Context, cs ...chan interface{}) chan interface{} { |
| 56 | var wg sync.WaitGroup |
| 57 | out := make(chan interface{}) |
| 58 | |
| 59 | // Start an output goroutine for each input channel in cs. output |
| 60 | // copies values from c to out until c is closed, then calls wg.Done. |
| 61 | output := func(c <-chan interface{}) { |
| 62 | for n := range c { |
| 63 | select { |
| 64 | case <-ctx.Done(): |
| 65 | return |
| 66 | case out <- n: |
| 67 | } |
| 68 | } |
| 69 | wg.Done() |
| 70 | } |
| 71 | wg.Add(len(cs)) |
| 72 | for _, c := range cs { |
| 73 | go output(c) |
| 74 | } |
| 75 | |
| 76 | // Start a goroutine to close out once all the output goroutines are |
| 77 | // done. This must start after the wg.Add call. |
| 78 | go func() { |
| 79 | wg.Wait() |
| 80 | close(out) |
| 81 | }() |
| 82 | return out |
| 83 | } |
| 84 | |
| 85 | func mergeError(ctx context.Context, cs ...chan error) chan error { |
| 86 | var wg sync.WaitGroup |
no test coverage detected