sock/multiwriter.go
2024-02-15 17:09:59 +01:00

32 lines
607 B
Go

package sock
import "io"
type multiWriter struct {
writers []io.Writer
}
var _ io.Writer = &multiWriter{}
func (t *multiWriter) Write(p []byte) (n int, err error) {
for _, w := range t.writers {
n, err = w.Write(p)
if err != nil || n != len(p) {
continue
}
}
return len(p), nil
}
func MultiWriter(writers ...io.Writer) io.Writer {
allWriters := make([]io.Writer, 0, len(writers))
for _, w := range writers {
if mw, ok := w.(*multiWriter); ok {
allWriters = append(allWriters, mw.writers...)
} else {
allWriters = append(allWriters, w)
}
}
return &multiWriter{allWriters}
}