go - GoLang, "hash.Write" , where did the "write()" function come from? -
func hash(s string) uint32 { h := fnv.new32a() h.write([]byte(s)) return h.sum32() }
for code piece. understand type h. hash. hash type, didn't see write() method. http://golang.org/pkg/hash/ write()?
thanks
the hash interface embeds writer interface. therefore, type wants implement hash interface, needs implement writer interface containing write method.
the reason write method can calculate hashes of can written. example, can calculate hash of formatted representation of object (by using fmt package), or can calculate hash of json representation (by using json package), etc.
h := fnv.new32a() fmt.fprint(h, myobject) // alternatively: // json.newencoder(h).encode(myobject) // etc. return h.sum32()
Comments
Post a Comment