func CalcFileMD5(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
return "", err
}
if stat.IsDir() {
return "", nil
}
hash := md5.New()
_, err = io.Copy(hash, file)
if err != nil {
return "", err
}
md5Bytes := hash.Sum(nil)
md5Str := hex.EncodeToString(md5Bytes)
return md5Str, nil
}
虽然不知道是什么原理,但测试了几次,发现计算一个2G的mp4文件,这么写确实会稍快一点
func CalcFileMD5(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()
stat, err := file.Stat()
if err != nil {
return "", err
}
if stat.IsDir() {
return "", nil
}
}
虽然不知道是什么原理,但测试了几次,发现计算一个2G的mp4文件,这么写确实会稍快一点