but if you have the K to compare to, why not just use map[K]V and let the standard map manage the hash and duplicates - since the OP referred to hash/maphash I assume they are using keys that are comparable
unless the OP is trying to write a complete custom map implementation - but I doubt that is the case if they didn’t understand that hashes can have duplicates > On Jun 25, 2025, at 4:53 PM, 'Axel Wagner' via golang-nuts > <golang-nuts@googlegroups.com> wrote: > > Yes, I meant map[uint64][]struct{K;V}, thanks for correcting me. > > On Wed, 25 Jun 2025 at 20:06, robert engels <reng...@ix.netcom.com > <mailto:reng...@ix.netcom.com>> wrote: >> That is not correct and will not work - what the op is talking about is if K >> collides - so this is only a single K - so map[K][]struct{K;V} will only >> ever have one element in the array… >> >>> On Jun 25, 2025, at 12:33 PM, 'Axel Wagner' via golang-nuts >>> <golang-nuts@googlegroups.com <mailto:golang-nuts@googlegroups.com>> wrote: >>> >>> 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 >>> <mailto: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 >>>> <mailto: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 >>> <mailto: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 >>> >>> <https://groups.google.com/d/msgid/golang-nuts/CAEkBMfH6WzHLzoz%3DMe30MamY6th8miTqLFd4_LT_kJ%2BLB2%2B9gg%40mail.gmail.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 > <mailto:golang-nuts+unsubscr...@googlegroups.com>. > To view this discussion visit > https://groups.google.com/d/msgid/golang-nuts/CAEkBMfHRHS_5V21MMC6oz5X75k-pXM8hCLN_tCTjfxnG5UEN7g%40mail.gmail.com > > <https://groups.google.com/d/msgid/golang-nuts/CAEkBMfHRHS_5V21MMC6oz5X75k-pXM8hCLN_tCTjfxnG5UEN7g%40mail.gmail.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/6FCDE641-892E-4BF5-B682-BB0FBDE8FE53%40ix.netcom.com.