Skip to content

Instantly share code, notes, and snippets.

@mping-exo
Created November 30, 2022 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mping-exo/ab6a65b54e0629907af85189987a6863 to your computer and use it in GitHub Desktop.
Save mping-exo/ab6a65b54e0629907af85189987a6863 to your computer and use it in GitHub Desktop.
xxhash golang
package main
import (
"fmt"
"io"
"os"
"reflect"
"github.com/cespare/xxhash/v2"
)
func xxh64Sum(filePath string) (uint64, error) {
file, err := os.Open(filePath)
if err != nil {
return 0, err
}
defer file.Close()
h := xxhash.New()
if _, err = io.Copy(h, file); err != nil {
return 0, err
}
return h.Sum64(), nil
}
func main() {
path := os.Args[1]
hash, err := xxh64Sum(path)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("file: %s, type: %s, hash: %d", path, reflect.TypeOf(hash), hash)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment