golang get file md5
To calculate the MD5 hash of a file in Go, you can follow these steps:
Import the necessary packages: import ( "crypto/md5" "encoding/hex" "os" )
Open the file: file, err := os.Open("filename.txt") if err != nil { // handle error } defer file.Close()
Create an MD5 hash: hash := md5.New() if _, err := io.Copy(hash, file); err != nil { // handle error }
Calculate the hash sum: hashInBytes := hash.Sum(nil)
Convert the hash sum to a hexadecimal string: md5String := hex.EncodeToString(hashInBytes)
Print or use the MD5 hash: fmt.Println(md5String)