There are ∞ many possible hashable Go values (in particular, all strings) and 2^64 uint64, so yes, there is a possibility of collision. How big that risk is depends on how many values you have and what the damage is, if a collision happens. Wikipedia has a nifty table: https://en.wikipedia.org/wiki/Birthday_attack#Mathematics There, you can see that if you have a 64 bit hash and have, say 190K elements, the probability of a random collision is roughly 10^-9 (so pretty small, but not completely impossible). If a collision is truly problematic, a simple fix would be to use a map[K][]struct{K;V}. That way, if there *is* a collision on the hash, you store all the colliding values and can walk the slice to find the right one. As collisions should be rare, that should be an uncommon cost to pay.
On Wed, 25 Jun 2025 at 17:26, Pierre Durand <pierredur...@gmail.com> wrote: > Is it safe to use the result of a hash/maphash (uint64) as a map key. > Is there a risk of collision ? (different inputs generate the same hash) > > Thank you > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > To view this discussion visit > https://groups.google.com/d/msgid/golang-nuts/c8076635-5311-4e17-81c7-4907857df145n%40googlegroups.com > <https://groups.google.com/d/msgid/golang-nuts/c8076635-5311-4e17-81c7-4907857df145n%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion visit https://groups.google.com/d/msgid/golang-nuts/CAEkBMfH6WzHLzoz%3DMe30MamY6th8miTqLFd4_LT_kJ%2BLB2%2B9gg%40mail.gmail.com.