Last active
July 30, 2016 19:15
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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