Skip to content

Instantly share code, notes, and snippets.

@bprosnitz
Created October 6, 2015 16:19
Show Gist options
  • Save bprosnitz/cbe4a75a8298d5ed7e99 to your computer and use it in GitHub Desktop.
Save bprosnitz/cbe4a75a8298d5ed7e99 to your computer and use it in GitHub Desktop.
Tee Writer - Write to two writers
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