Menu

Accepted proposal: grow an existing map with maps.Grow

#golang#redditgolang#grow#maps#issue
Reading 0:00
15s threshold

I was digging through the Go issue tracker and found out this proposal for maps.Grow. It works like slices.Grow and lets you grow an existing map.

You can already hint a map's capacity when you create it with make(map[K]V, n). But sometimes you need to grow a map you don't own. One example is copying HTTP headers from one map into another, where the destination map often comes from somewhere else and you have no control over how it was created.

Without a way to grow it up front, the map resizes itself several times as you insert entries. This causes extra allocations and garbage collection. maps.Grow lets you reserve the space once, which makes it a nice perf win.

Liz Fong-Jones from honeycomb.io proposed and implemented the CL. Hopefully, it's on track for the Go 1.28 release.

I read through the issue thread to collect more use cases and wrote about them here. This is a fun one. You might find it useful.

https://rednafi.com/shards/2026/11/go-maps-grow/

submitted by /u/sigmoia
[link] [comments]