Skip to content

Commit 2819bde

Browse files
committed
fix
1 parent da2b836 commit 2819bde

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

sequential_fan_in.go

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package main
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
"sync"
6+
)
47

58
type Message struct {
69
msg string
@@ -24,12 +27,18 @@ func BoringSequentialFanIn(syncedChans ... <-chan Message) <-chan string {
2427
c := make(chan string)
2528

2629
go func() {
27-
var msgs []Message
28-
29-
for _, v := range syncedChans {
30-
msgs = append(msgs, <-v)
30+
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)
3138
}
3239

40+
wg.Wait()
41+
3342
for _, v := range msgs {
3443
c <- v.msg
3544
}

0 commit comments

Comments
 (0)