Created
October 6, 2015 16:19
-
-
Save bprosnitz/cbe4a75a8298d5ed7e99 to your computer and use it in GitHub Desktop.
Tee Writer - Write to two writers
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 teewriter | |
type TeeWriter struct { | |
w1 io.Writer | |
w2 io.Writer | |
} | |
func (tw *TeeWriter) Write(p []byte) (n int, err error) { | |
n1, err1 := tw.w1.Write(p) | |
n2, err2 := tw.w2.Write(p) | |
if err1 != err2 { | |
return 0, fmt.Errorf("errors differ: %v and %v", err1, err2) | |
} | |
if n1 != n2 { | |
return 0, fmt.Errorf("amts written differ: %v and %v", n1, n2) | |
} | |
return n1, err1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment