Quick Overview
cespare/xxhash is a Go implementation of the xxHash algorithm, specifically the XXH64 variant. It provides a fast, non-cryptographic hash function that is suitable for tasks such as checksumming or fingerprinting data.
Pros
- High performance: xxHash is known for its speed, often outperforming other non-cryptographic hash functions
- Simple API: Easy to use with a straightforward interface
- No external dependencies: Pure Go implementation
- Well-maintained: Regular updates and bug fixes
Cons
- Limited to XXH64: Does not implement other xxHash variants like XXH32 or XXH3
- Non-cryptographic: Not suitable for security-sensitive applications
- Single-purpose: Focused solely on xxHash, not a general-purpose hashing library
Code Examples
Hashing a string:
import "github.com/cespare/xxhash"
sum := xxhash.Sum64String("hello world")
fmt.Printf("Hash: %x\n", sum)
Hashing a byte slice:
data := []byte("example data")
sum := xxhash.Sum64(data)
fmt.Printf("Hash: %x\n", sum)
Using a streaming hash:
h := xxhash.New()
h.Write([]byte("part one"))
h.Write([]byte("part two"))
sum := h.Sum64()
fmt.Printf("Hash: %x\n", sum)
Getting Started
To use cespare/xxhash in your Go project, first install it:
go get github.com/cespare/xxhash
Then import it in your Go code:
import "github.com/cespare/xxhash"
Now you can use the xxhash functions in your code as shown in the examples above.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
xxhash
xxhash is a Go implementation of the 64-bit xxHash algorithm, XXH64. This is a high-quality hashing algorithm that is much faster than anything in the Go standard library.
This package provides a straightforward API:
func Sum64(b []byte) uint64
func Sum64String(s string) uint64
type Digest struct{ ... }
func New() *Digest
The Digest
type implements hash.Hash64. Its key methods are:
func (*Digest) Write([]byte) (int, error)
func (*Digest) WriteString(string) (int, error)
func (*Digest) Sum64() uint64
The package is written with optimized pure Go and also contains even faster
assembly implementations for amd64 and arm64. If desired, the purego
build tag
opts into using the Go code even on those architectures.
Compatibility
This package is in a module and the latest code is in version 2 of the module. You need a version of Go with at least "minimal module compatibility" to use github.com/cespare/xxhash/v2:
- 1.9.7+ for Go 1.9
- 1.10.3+ for Go 1.10
- Go 1.11 or later
I recommend using the latest release of Go.
Benchmarks
Here are some quick benchmarks comparing the pure-Go and assembly implementations of Sum64.
input size | purego | asm |
---|---|---|
4 B | 1.3 GB/s | 1.2 GB/s |
16 B | 2.9 GB/s | 3.5 GB/s |
100 B | 6.9 GB/s | 8.1 GB/s |
4 KB | 11.7 GB/s | 16.7 GB/s |
10 MB | 12.0 GB/s | 17.3 GB/s |
These numbers were generated on Ubuntu 20.04 with an Intel Xeon Platinum 8252C CPU using the following commands under Go 1.19.2:
benchstat <(go test -tags purego -benchtime 500ms -count 15 -bench 'Sum64$')
benchstat <(go test -benchtime 500ms -count 15 -bench 'Sum64$')
Projects using this package
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot