go - How to create a custom hashcode to determine if data has mutated -
say have struct type has int64 , bools, along embedded types have more int64 , 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 structs fields/properties, want generate hashcode.
the aim of can determine if contents of instance has changed comparing before/after hashcode value.
so if t1 instance has changed i.e. of own properties value of hash should different.
you can use 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 using map key, it's better directly use struct key , let go handle it.
Comments
Post a Comment