Golang : Generate MD5 checksum of a file
This is an update to the previous tutorial on how to generate checksum for a file, except that this tutorial uses the io.Copy
function instead of breaking up the file into chunks. io.Copy
function reads a file content until it reaches EOF and it can be used to copy the file data into a MD5 hash.
package main
import (
"crypto/md5"
"fmt"
"io"
"os"
)
func main() {
file, err := os.Open("utf8.txt")
if err != nil {
panic(err)
}
defer file.Close()
hash := md5.New()
_, err = io.Copy(hash, file)
if err != nil {
panic(err)
}
fmt.Printf("%s MD5 checksum is %x \n", file.Name(), hash.Sum(nil))
}
Sample output :
utf8.txt MD5 checksum is 5af1ca1d92340d72a29c194d3f4096e0
See also : Generate checksum for a file in Go
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+25.7k Mac/Linux and Golang : Fix bind: address already in use error
+7.9k Golang : Metaprogramming example of wrapping a function
+7.6k Golang : Regular Expression find string example
+9.4k Javascript : Read/parse JSON data from HTTP response
+6k Golang : Test input string for unicode example
+5.7k Golang : Extract unicode string from another unicode string example
+40.7k Golang : How to check if a string contains another sub-string?
+11.7k Golang : Detect user location with HTML5 geo-location
+32.3k Golang : Regular Expression for alphanumeric and underscore
+29k Golang : JQuery AJAX post data to server and send data back to client example
+18k Golang : Get path name to current directory or folder
+9.7k Golang : Translate language with language package example