Say I have a struct type that has int64 and bools, along with embedded types that have more int64 and bool type fields.
type T1 struct {
f1 int64
f2 int64
f3 bool
T2 T2
}
type T2 struct {
f4 int64
f5 int64
f6 bool
}
Now using all the structs fields/properties, I want to generate a hashcode.
The aim of this is so I can determine if the contents of the instance has changed but comparing the before/after hashcode value.
So if T1 instance has changed i.e. any of its own properties Then the value of the hash should be different.
You can use something like:
func (t *T1) Hash() uint64 {
hb := make([]byte, 8+8+1+8+8+1)
binary.BigEndian.PutUint64(hb, uint64(t.f1))
binary.BigEndian.PutUint64(hb[8:], uint64(t.f2))
if t.f3 {
hb[16] = 1
}
binary.BigEndian.PutUint64(hb[17:], uint64(t.T2.f4))
binary.BigEndian.PutUint64(hb[25:], uint64(t.T2.f5))
if t.T2.f6 {
hb[33] = 1
}
f := fnv.New64a()
f.Write(hb)
return f.Sum64()
}
Although if you are using it as a map key, it's better to just directly use the struct as the key and let go handle it.