Skip to content

Instantly share code, notes, and snippets.

@techjanitor
Last active August 29, 2015 14:07
Show Gist options
  • Save techjanitor/531002af54b105b1129a to your computer and use it in GitHub Desktop.
Save techjanitor/531002af54b105b1129a to your computer and use it in GitHub Desktop.
Get MD5 hash with TeeReader
// Make new md5
hasher := md5.New()
// Make a new file
image, err := os.Create(imagefile)
if err != nil {
err = errors.New("problem creating file")
return
}
// Save file and also read into hasher for md5
_, err = io.Copy(image, io.TeeReader(file, hasher))
if err != nil {
os.RemoveAll(imagefile)
err = errors.New("problem copying file")
return
}
// Set md5sum from hasher
md5sum = hex.EncodeToString(hasher.Sum(nil))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment