golang get file md5

To calculate the MD5 hash of a file in Go, you can follow these steps:

  1. Import the necessary packages: import ( "crypto/md5" "encoding/hex" "os" )

  2. Open the file: file, err := os.Open("filename.txt") if err != nil { // handle error } defer file.Close()

  3. Create an MD5 hash: hash := md5.New() if _, err := io.Copy(hash, file); err != nil { // handle error }

  4. Calculate the hash sum: hashInBytes := hash.Sum(nil)

  5. Convert the hash sum to a hexadecimal string: md5String := hex.EncodeToString(hashInBytes)

  6. Print or use the MD5 hash: fmt.Println(md5String)