Skip to content

Go refusing to make structs in maps mutable since 2012

2 pointsivoras1 comment
On HN

I've recently asked if it's time to revisit the decision to make structs in maps in Go mutable, and was politely declined, with a reference to this being a refusted proposal since 2012:

https://github.com/golang/go/issues/53857

Since arrays work fine, if a is a slice or an array:

a[10].field = 42

it seems bogus that maps don't; this doesn't compile in Go if m is a map:

m[10].field = 42

In both cases, the address of the struct is calculated at runtime, as it must be known to access the field.

Instead of leaving this as a rant, let's make it a question: what's your "what is this insanity?" peeve with your favourite language?

Comments

it seems bogus that maps don't; this doesn't compile in Go if m is a map:

It does not seem bogus to me. Values in a map are not addressable for a good reasons. It allows more freedom in implementing a map. Arrays, on the other hand, are specified to be continuous in memory, hence the implementation is set in stone.

Also, non-existent items of a map return the zero value as specified. Now assume the map items are addressable. What exactly should happen if you refer to a field of a map item that is not in the map? Is there a possibility that is not surprising?

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.