Skip to content

Instantly share code, notes, and snippets.

@odeke-em
Last active July 30, 2016 19:15
Show Gist options
  • Save odeke-em/72b44d81cc81cbc5c6c357bf2ec1e86d to your computer and use it in GitHub Desktop.
Save odeke-em/72b44d81cc81cbc5c6c357bf2ec1e86d to your computer and use it in GitHub Desktop.
Duplicate a stream n times, and have a notifier to tell you when it is done.
package mcopy
import (
"io"
)
func DuplicateToManyStreams(r io.Reader, n uint) ([]io.Reader, chan bool) {
var readers []io.Reader
var writers []io.Writer
for i := uint(0); i < n; i++ {
prc, pwc := io.Pipe()
readers = append(readers, prc)
writers = append(writers, pwc)
}
doneChan := make(chan bool, 1)
go func() {
defer func() { doneChan <- true; close(doneChan) }()
mw := io.MultiWriter(writers...)
_, _ = io.Copy(mw, r)
for _, w := range writers {
if c, ok := w.(io.Closer); ok {
_ = c.Close()
}
}
}()
return readers, doneChan
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment