Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate checksum of a file in GO

Tags:

file

go

checksum

I need calculate checksum of a file to determine the integrity of data of existing file. I need it for large files to avoid the download. Can you give me any idea?

like image 878
Alvina Avatar asked Jan 31 '26 02:01

Alvina


1 Answers

You can do that by :

f, err := os.Open(path)
if err != nil {
    glog.Fatal(err)
}
defer f.Close()

hasher := sha256.New()
if _, err := io.Copy(hasher, f); err != nil {
    glog.Fatal(err)
}
value:= hex.EncodeToString(hasher.Sum(nil))
like image 55
Animesh Kumar Paul Avatar answered Feb 01 '26 17:02

Animesh Kumar Paul