The deriveHash function calculates a hash of an object.
Given the following input:
package hash
import "reflect"
type Entry struct {
Name string
Numbers []int
}
func hasDuplicates(es []*Entry) bool {
m := make(map[uint64][]*Entry)
for i, e := range es {
h := deriveHash(e)
for _, f := range m[h] {
if reflect.DeepEqual(e, f) {
return true
}
}
if _, ok := m[h]; !ok {
m[h] = []*Entry{es[i]}
}
}
return false
}
goderive will generate the following code:
// Code generated by goderive DO NOT EDIT.
package hash
// deriveHash returns the hash of the object.
func deriveHash(object *Entry) uint64 {
if object == nil {
return 0
}
h := uint64(17)
h = 31*h + deriveHash_(object.Name)
h = 31*h + deriveHash_1(object.Numbers)
return h
}
// deriveHash_ returns the hash of the object.
func deriveHash_(object string) uint64 {
var h uint64
for _, c := range object {
h = 31*h + uint64(c)
}
return h
}
// deriveHash_1 returns the hash of the object.
func deriveHash_1(object []int) uint64 {
if object == nil {
return 0
}
h := uint64(17)
for i := 0; i < len(object); i++ {
h = 31*h + uint64(object[i])
}
return h
}