How to increase the size of a map in GO?

I have a set of objects that someday will add a new element.

How to increase the size of the internal card?

Do I need to redistribute the entire card every time the item counter exceeds the allocated account?

+6
source share
1 answer

The Go spec states:

The new value of the empty map is performed using the built-in function make, which takes the type of map and optional hint as arguments:

make(map[string]int) make(map[string]int, 100) 

The initial capacity does not limit its size: cards grow to accommodate the number of items stored in them

So no, you don’t need to make any selections on the map once you have created it. This is done internally using the Go runtime. The extra capacity used to create the map is just a hint, not a limitation.

+16
source

Source: https://habr.com/ru/post/946579/


All Articles