Created
September 16, 2015 00:07
-
-
Save bprosnitz/68dd3420d3392866cb0e to your computer and use it in GitHub Desktop.
Tee Copy in Go
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 debug | |
import ( | |
"fmt" | |
"io" | |
) | |
func teeCopy(w1 io.Writer, w2 io.Writer, r io.Reader) (totalN int, err error) { | |
buf := make([]byte, 1024) | |
for { | |
var n int | |
n, err = r.Read(buf) | |
totalN += n | |
if err != nil { | |
return | |
} | |
var writeN int | |
writeN, err = w1.Write(buf[:n]) | |
if err != nil { | |
return | |
} | |
if n != writeN { | |
return totalN, fmt.Errorf("incomplete data written. expected to write %d bytes, but wrote %d bytes", n, writeN) | |
} | |
writeN, err = w2.Write(buf[:n]) | |
if err != nil { | |
return | |
} | |
if n != writeN { | |
return totalN, fmt.Errorf("incomplete data written. expected to write %d bytes, but wrote %d bytes", n, writeN) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment