Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i increase a map's allocation size in GO language?

Tags:

go

I have a collection of objects that will sometime have new element added to it.

How do I increase the internal map size?

Do I need to reallocate the whole map every time the element count exceed allocated count?

like image 422
Nick Avatar asked Jun 05 '13 10:06

Nick


1 Answers

The Go specification says:

A new, empty map value is made using the built-in function make, which takes the map type and an optional capacity hint as arguments:

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

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

So, no, you don't have to make any allocations to a map once you've created it. This is handled internally by the Go runtime. The optional capacity used when making the map it's only a hint, not a limitation.

like image 90
ANisus Avatar answered Oct 19 '22 06:10

ANisus