On 30 January 2018 at 23:19, <matthewju...@gmail.com> wrote: >> - When slices can be compared, they can be used as map keys. What happens >> if the contents of a slice are changed after it has been added to a map? > > > I’m not too familiar with Go map internals, but my thought is the key hash > would depend on the backing array values. Go maps also allow reading the > keys back using iteration so the slice backing array (up to length) would > have to be copied. If the slice contents are changed then that would be a > different key and the original key would be intact.
Note that copying the slice contents to make a map key implies that using a slice as a map key might imply copying a whole tree, which seems rather expensive to me (especially as it might end up using more memory than the original if some elements are duplicated, unless an alias-aware copy algorithm is used which would be more expensive still) BTW you can already do something like this: https://play.golang.org/p/q4bz8-AckN3 You can even do it without reflect, which I'll leave as an exercise for the reader :) -- 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. For more options, visit https://groups.google.com/d/optout.