-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathchannel.go
More file actions
38 lines (33 loc) · 906 Bytes
/
Copy pathchannel.go
File metadata and controls
38 lines (33 loc) · 906 Bytes
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
// 2019, Georg Sauthoff <mail@gms.tf>
// SPDX-License-Identifier: GPL-3.0-or-later
package main
type channel_writer struct {
out chan<- []byte
}
func new_channel_writer(out chan<- []byte) *channel_writer {
return &channel_writer{out}
}
// implements io.Writer interface
// expects that caller doesn't modify the array the slice p points to
func (w *channel_writer) Write(p []byte) (n int, err error) {
w.out <- p
return len(p), nil
}
func (w *channel_writer) Close() error {
close(w.out)
return nil
}
func tee_words(in <-chan []byte, out1, out2 chan<- []byte) {
for x := range in {
o1, o2 := out1, out2
for i := 0; i < 2; i++ {
// sending something into a nil channel always blocks
select {
case o1 <- x: o1 = nil
case o2 <- x: o2 = nil
}
}
}
close(out1)
close(out2)
}