My assumption is that OP wants to de-facto store keys in a map that are not
`comparable` (e.g. use a `[]byte` as a key), without having to fully
implement their own hash map. Of course, at some point we'll hopefully get
#69559 <https://github.com/golang/go/issues/69559> to do this easily. In
the meantime, using hash/maphash to calculate a hash and then using that as
a key for the built-in map seems like a fair compromise.


On Thu, 26 Jun 2025 at 00:12, robert engels <reng...@ix.netcom.com> wrote:

> 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> 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> 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>
>> 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
>> <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.
> 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/CAEkBMfFoQrrbp6TH2UVTzSP6t1sRdHkB1KO%3DietSb9CoYgLxOQ%40mail.gmail.com.

Reply via email to