sock/multiwriter.go

29 lines
552 B
Go
Raw Normal View History

2024-02-15 17:09:59 +01:00
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 {
2024-02-15 17:49:22 +01:00
w.Write(p)
2024-02-15 17:09:59 +01:00
}
return len(p), nil
}
2024-02-15 17:49:22 +01:00
func newMultiWriter(writers ...io.Writer) io.Writer {
2024-02-15 17:09:59 +01:00
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}
}