-
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpipe.go
More file actions
43 lines (31 loc) · 544 Bytes
/
pipe.go
File metadata and controls
43 lines (31 loc) · 544 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
39
40
41
42
43
package variant
import (
"bytes"
"errors"
"io"
"io/ioutil"
)
func Pipe() (func() (*bytes.Buffer, error), io.WriteCloser) {
r, w := io.Pipe()
out := &bytes.Buffer{}
outDone := make(chan error, 1)
go func() {
bs, err := ioutil.ReadAll(r)
if err != nil {
outDone <- err
return
}
if _, err := out.Write(bs); err != nil {
outDone <- err
return
}
outDone <- nil
}()
return func() (*bytes.Buffer, error) {
err := <-outDone
if !errors.Is(err, io.EOF) {
return nil, err
}
return out, nil
}, w
}