]>
Commit | Line | Data |
---|---|---|
107c1cdb | 1 | # hashstructure [![GoDoc](https://godoc.org/github.com/mitchellh/hashstructure?status.svg)](https://godoc.org/github.com/mitchellh/hashstructure) |
bae9f6d2 JC |
2 | |
3 | hashstructure is a Go library for creating a unique hash value | |
4 | for arbitrary values in Go. | |
5 | ||
6 | This can be used to key values in a hash (for use in a map, set, etc.) | |
7 | that are complex. The most common use case is comparing two values without | |
8 | sending 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. | |
107c1cdb ND |
22 | |
23 | * Optionally hash the output of `.String()` on structs that implement fmt.Stringer, | |
24 | allowing effective hashing of time.Time | |
bae9f6d2 JC |
25 | |
26 | ## Installation | |
27 | ||
28 | Standard `go get`: | |
29 | ||
30 | ``` | |
31 | $ go get github.com/mitchellh/hashstructure | |
32 | ``` | |
33 | ||
34 | ## Usage & Example | |
35 | ||
36 | For usage and examples see the [Godoc](http://godoc.org/github.com/mitchellh/hashstructure). | |
37 | ||
38 | A quick code example is shown below: | |
39 | ||
107c1cdb ND |
40 | ```go |
41 | type ComplexStruct struct { | |
42 | Name string | |
43 | Age uint | |
44 | Metadata map[string]interface{} | |
45 | } | |
46 | ||
47 | v := ComplexStruct{ | |
48 | Name: "mitchellh", | |
49 | Age: 64, | |
50 | Metadata: map[string]interface{}{ | |
51 | "car": true, | |
52 | "location": "California", | |
53 | "siblings": []string{"Bob", "John"}, | |
54 | }, | |
55 | } | |
56 | ||
57 | hash, err := hashstructure.Hash(v, nil) | |
58 | if err != nil { | |
59 | panic(err) | |
60 | } | |
61 | ||
62 | fmt.Printf("%d", hash) | |
63 | // Output: | |
64 | // 2307517237273902113 | |
65 | ``` |