Debian Codegraph @

mwc/mwc.go

raw
1 // Package mwc implements MultiWriteClosers.
2 package mwc
3
4 import "io"
5
6 type mwc struct {
7 wcs []io.WriteCloser
8 }
9
10 // Write implements the Writer interface.
11 func (t *mwc) Write(p []byte) (n int, err error) {
12 for _, w := range t.wcs {
13 n, err = w.Write(p)
14 if err != nil {
15 return
16 }
17 if n != len(p) {
18 err = io.ErrShortWrite
19 return
20 }
21 }
22 return len(p), nil
23 }
24
25 // Close implements the Closer interface.
26 func (t *mwc) Close() error {
27 for _, wc := range t.wcs {
28 err := wc.Close()
29 if err != nil {
30 return err
31 }
32 }
33 return nil
34 }
35
36 // MultiWriteCloser creates a WriteCloser that duplicates its writes to
37 // all the provided writers, similar to the Unix tee(1) command.
38 func MultiWriteCloser(wc ...io.WriteCloser) io.WriteCloser {
39 wcs := make([]io.WriteCloser, len(wc))
40 copy(wcs, wc)
41 return &mwc{wcs}
42 }