How to display the hash value for keys on the map?

How to calculate a hash for keys on a map? Is it really unique and is it available for use in other structures?

I think this is just for primitive keys like int or immutable string , but for composite structures this seems nontrivial.

+5
source share
2 answers
  • The language specification does not say that means that it can change at any time or vary between implementations.

  • The hash algorithm is slightly different between types and platforms. At the moment: on x86 (32 or 64 bit), if the processor supports AES instructions, the runtime uses aeshash , a hash built on AES primitives, otherwise it uses a function inspired by xxHash and cityhash, but different from different options for 32-bit and 64-bit systems. Most types use a simple hash of their memory contents, but floating point types have a code that guarantees uniformity of 0 and -0 hash (since they are compared equally) and NaNs hacks (since two NaNs are never equal). Because complex types are built from floats, their hashes consist of hashes of their two parts with floating point. And the hash of the interface hash is the hash of the value stored in the interface, not the interface header itself.

  • All these things are in private functions, so no, you cannot access the Go internal hash for the value in your own code.
+8
source

The Go map implementation uses a hash called aeshash . This is not AES, but it uses the aesenc build instruction to calculate the hashes. This hash is not exported for use in the standard library.

The hash itself is written to the assembly and can be found in the source package.

+3
source

All Articles