获取当前目录下一个文件的MD5值
package main
import (
"crypto/md5"
"fmt"
"io"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
func GetCurrentPath() string {
file, _ := exec.LookPath(os.Args[0])
path, _ := filepath.Abs(file)
path = string(path[0:strings.LastIndex(path, "/")])
return path
}
func main() {
testFile := GetCurrentPath() + "/backup.sh"
log.Println(testFile)
file, inerr := os.Open(testFile)
if inerr == nil {
md5h := md5.New()
io.Copy(md5h, file)
fmt.Printf("%x", md5h.Sum([]byte(""))) //md5
}
}
~
可以看到程序运行的结果和linux md5sum是一致的.

转载自:
http://studygolang.com/topics/150
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29254281/viewspace-1518312/
,如需转载,请注明出处,否则将追究法律责任。