Skip to content

Instantly share code, notes, and snippets.

@nathany
Created August 11, 2016 23:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathany/ca7a3b5aac1d89d821014e644230d691 to your computer and use it in GitHub Desktop.
Save nathany/ca7a3b5aac1d89d821014e644230d691 to your computer and use it in GitHub Desktop.
Using MultiWriter to do two things at once
// copyAndChecksum calculates a checksum while writing to another output
func copyAndChecksum(w io.Writer, r io.Reader) (string, error) {
h := md5.New()
mw := io.MultiWriter(w, h)
if _, err := io.Copy(mw, r); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment