-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit.go
53 lines (45 loc) · 852 Bytes
/
split.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package github.com/tyoung3/sw
import "sync"
// var version string = "v0.0.1"
/*
Split copies all input from port 0, to all active ports 1,..N
*/
func Split(wg *sync.WaitGroup, arg []string, cs []chan interface{}) {
var wg2 sync.WaitGroup
var c2s []chan interface{}
defer wg.Done()
for range cs[1:] {
c2s = append(c2s, make(chan interface{}))
}
for j := range cs[1:] { /* Start a routine for each output */
wg2.Add(1)
go func(c chan interface{}, c2 chan interface{}) {
defer wg2.Done()
for {
ip, ok := <-c2
if ok != true {
close(c)
return
}
c <- ip
}
}(cs[j+1], c2s[j])
}
wg2.Add(1)
go func() {
for {
ip, ok := <-cs[0]
if ok != true {
for j := range c2s {
close(c2s[j])
}
wg2.Done()
return
}
for j := range c2s {
c2s[j] <- ip
}
}
}()
wg2.Wait()
}