hashstructure

Get hash values for arbitrary values in Go (golang).

  • Owner: mitchellh/hashstructure
  • Platform:
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

hashstructure GoDoc

hashstructure is a Go library for creating a unique hash value
for arbitrary values in Go.

This can be used to key values in a hash (for use in a map, set, etc.)
that are complex. The most common use case is comparing two values without
sending data across the network, caching values locally (de-dup), and so on.

Features

  • Hash any arbitrary Go value, including complex types.

  • Tag a struct field to ignore it and not affect the hash value.

  • Tag a slice type struct field to treat it as a set where ordering
    doesn't affect the hash code but the field itself is still taken into
    account to create the hash value.

  • Optionally specify a custom hash function to optimize for speed, collision
    avoidance for your data set, etc.

  • Optionally hash the output of .String() on structs that implement fmt.Stringer,
    allowing effective hashing of time.Time

Installation

Standard go get:

$ go get github.com/mitchellh/hashstructure

Usage & Example

For usage and examples see the Godoc.

A quick code example is shown below:

type ComplexStruct struct {
    Name     string
    Age      uint
    Metadata map[string]interface{}
}

v := ComplexStruct{
    Name: "mitchellh",
    Age:  64,
    Metadata: map[string]interface{}{
        "car":      true,
        "location": "California",
        "siblings": []string{"Bob", "John"},
    },
}

hash, err := hashstructure.Hash(v, nil)
if err != nil {
    panic(err)
}

fmt.Printf("%d", hash)
// Output:
// 2307517237273902113

Main metrics

Overview
Name With Ownermitchellh/hashstructure
Primary LanguageGo
Program languageGo (Language Count: 1)
Platform
License:MIT License
所有者活动
Created At2016-01-07 18:30:08
Pushed At2023-01-03 15:13:06
Last Commit At2021-05-27 16:24:52
Release Count5
Last Release Namev2.0.2 (Posted on )
First Release Namev1.0.0 (Posted on )
用户参与
Stargazers Count759
Watchers Count9
Fork Count90
Commits Count73
Has Issues Enabled
Issues Count21
Issue Open Count8
Pull Requests Count14
Pull Requests Open Count6
Pull Requests Close Count6
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private