We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent da2b836 commit 2819bdeCopy full SHA for 2819bde
sequential_fan_in.go
@@ -1,6 +1,9 @@
1
package main
2
3
-import "fmt"
+import (
4
+ "fmt"
5
+ "sync"
6
+)
7
8
type Message struct {
9
msg string
@@ -24,12 +27,18 @@ func BoringSequentialFanIn(syncedChans ... <-chan Message) <-chan string {
24
27
c := make(chan string)
25
28
26
29
go func() {
- var msgs []Message
-
- for _, v := range syncedChans {
30
- msgs = append(msgs, <-v)
+ msgs := make([]Message, len(syncedChans))
31
+ wg := sync.WaitGroup{}
32
+ wg.Add(len(syncedChans))
33
+ for i, v := range syncedChans {
34
+ go func(idx int, x <-chan Message) {
35
+ msgs[idx] = <-x
36
+ wg.Done()
37
+ } (i, v)
38
}
39
40
+ wg.Wait()
41
+
42
for _, v := range msgs {
43
c <- v.msg
44
0 commit comments