Created
June 5, 2016 18:47
-
-
Save matthew-andrews/527f4b73a85f2ee7515668c54432c16f to your computer and use it in GitHub Desktop.
This file contains 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 main | |
import ( | |
"crypto/md5" | |
"fmt" | |
"io" | |
"os" | |
) | |
func ComputeMd5(filePath string) ([]byte, error) { | |
var result []byte | |
file, err := os.Open(filePath) | |
if err != nil { | |
return result, err | |
} | |
defer file.Close() | |
hash := md5.New() | |
if _, err := io.Copy(hash, file); err != nil { | |
return result, err | |
} | |
// io.Copy has already passed the entire contents of the file to hash, so at this point we just | |
// need to call Sum. Ideally there would be a way to do this without passing in any arguments, | |
// but Go is strict so we have to provide something so provide `nil` instead. | |
return hash.Sum(nil), nil | |
} | |
func main() { | |
if b, err := ComputeMd5("main.go"); err != nil { | |
fmt.Printf("Err: %v", err) | |
} else { | |
fmt.Printf("main.go md5 checksum is: %x", b) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment