• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.github/workflows/H22-Nov-2020-

LICENSEH A D22-Nov-20201.1 KiB

README.mdH A D22-Nov-20201.8 KiB

go.modH A D22-Nov-202051

hashstructure.goH A D22-Nov-20209.6 KiB

hashstructure_examples_test.goH A D22-Nov-2020476

hashstructure_test.goH A D22-Nov-202012.9 KiB

include.goH A D22-Nov-2020857

README.md

1# hashstructure [![GoDoc](https://godoc.org/github.com/mitchellh/hashstructure?status.svg)](https://godoc.org/github.com/mitchellh/hashstructure)
2
3hashstructure is a Go library for creating a unique hash value
4for arbitrary values in Go.
5
6This can be used to key values in a hash (for use in a map, set, etc.)
7that are complex. The most common use case is comparing two values without
8sending data across the network, caching values locally (de-dup), and so on.
9
10## Features
11
12  * Hash any arbitrary Go value, including complex types.
13
14  * Tag a struct field to ignore it and not affect the hash value.
15
16  * Tag a slice type struct field to treat it as a set where ordering
17    doesn't affect the hash code but the field itself is still taken into
18    account to create the hash value.
19
20  * Optionally, specify a custom hash function to optimize for speed, collision
21    avoidance for your data set, etc.
22
23  * Optionally, hash the output of `.String()` on structs that implement fmt.Stringer,
24    allowing effective hashing of time.Time
25
26  * Optionally, override the hashing process by implementing `Hashable`.
27
28## Installation
29
30Standard `go get`:
31
32```
33$ go get github.com/mitchellh/hashstructure
34```
35
36## Usage & Example
37
38For usage and examples see the [Godoc](http://godoc.org/github.com/mitchellh/hashstructure).
39
40A quick code example is shown below:
41
42```go
43type ComplexStruct struct {
44    Name     string
45    Age      uint
46    Metadata map[string]interface{}
47}
48
49v := ComplexStruct{
50    Name: "mitchellh",
51    Age:  64,
52    Metadata: map[string]interface{}{
53        "car":      true,
54        "location": "California",
55        "siblings": []string{"Bob", "John"},
56    },
57}
58
59hash, err := hashstructure.Hash(v, nil)
60if err != nil {
61    panic(err)
62}
63
64fmt.Printf("%d", hash)
65// Output:
66// 2307517237273902113
67```
68