Documentation
¶
Overview ¶
Package datahash computes 64-bit hashes for arbitrary Go values using reflection.
It recursively traverses Go values using reflection to produce deterministic 64-bit hashes based on their content and structure. Supported types include primitives, arrays, slices, maps, structs, pointers, interfaces, and types that support sequence iteration.
Features:
- Detects and handles cyclic data structures safely (via pointer tracking).
- Supports ordered or unordered hashing of collections and structs via the "Unordered" option.
- Integrates with encoding.BinaryMarshaler, encoding.TextMarshaler, fmt.Stringer, and custom HashWriter interfaces.
- High performance through reflection caching and hasher pooling.
Usage:
package main
import (
"fmt" "math/big" "github.com/cespare/xxhash/v2" "github.com/go-sqlt/datahash"
)
type MyStruct struct {
Name string `datahash:"-"`
Age int
Float *big.Float
}
func main() {
hasher := datahash.New(xxhash.New, datahash.Options{
UnorderedStruct: false,
UnorderedSlice: false,
UnorderedArray: false,
UnorderedSeq: false,
UnorderedSeq2: false,
Text: true, // big.Float implements encoding.TextMarshaler
JSON: false,
String: false,
ZeroNil: false,
IgnoreZero: false,
})
alice, _ := hasher.Hash(MyStruct{Name: "Alice", Age: 30, Float: big.NewFloat(1.23)})
bob, _ := hasher.Hash(MyStruct{Name: "Bob", Age: 30, Float: big.NewFloat(1.23)})
fmt.Println(alice, alice == bob) // Output: 13125691809697640472 true
}
Notes:
- For custom hashing behavior, implement the HashWriter or encoing.BinaryMarshaler interface.
- Text/JSON/String Option: use marshaling interfaces if available.
- Unordered Option: treat structs, slices, iter.Seq and iter.Seq2 as unordered sets.
- Use `datahash:"-"` to exclude a field from hashing.
- Struct fields are hashed in declared order unless Unordered is enabled, in which case order is ignored.
- Maps are always hashed as a unordered set.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type HashWriter ¶ added in v0.0.2
HashWriter can be implemented by types that want to define custom hashing behavior.
The WriteHash method is responsible for writing a representation of the value directly into the provided hash.Hash64.
type Hasher ¶
type Hasher struct {
// contains filtered or unexported fields
}
Hasher hashes arbitrary Go values consistently according to configurable Options.
It caches reflection logic internally for performance, is safe for concurrent use, and supports integration with marshaling interfaces (BinaryMarshaler, TextMarshaler, etc.).
func New ¶
New creates a new Hasher that uses the given hash.Hash64 constructor and Options.
The init function (e.g., fnv.New64a, xxhash.New) must return a new hash.Hash64 instance on each call.
Example:
fnvHasher := datahash.New(fnv.New64a, datahash.Options{})
xxhHasher := datahash.New(xxhash.New, datahash.Options{})
func (*Hasher) Hash ¶
Hash computes a 64-bit hash of the given value.
It recursively traverses the value's structure using reflection, respecting the configured Options. Custom behavior is supported via standard marshaling interfaces (BinaryMarshaler, TextMarshaler, JSONMarshaler, fmt.Stringer) or the custom HashWriter interface.
Returns the computed hash or an error if hashing fails.
type Options ¶
type Options struct {
UnorderedStruct, UnorderedArray, UnorderedSlice, UnorderedSeq, UnorderedSeq2 bool
Text, JSON, String bool
ZeroNil bool
IgnoreZero bool
}
Options configures how values are hashed, including support for unordered collections, interface marshaling, and zero value handling.