Which is the nicer way to initialize a map?

Go

Go Problem Overview


As map is a reference type. What is difference between:?

m := make(map[string]int32)

and

m := map[string]int32{}

Go Solutions


Solution 1 - Go

One allows you to initialize capacity, one allows you to initialize values:

// Initializes a map with space for 15 items before reallocation
m := make(map[string]int32, 15)

vs

// Initializes a map with an entry relating the name "bob" to the number 5
m := map[string]int{"bob": 5} 

For an empty map with capacity 0, they're the same and it's just preference.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioniwatView Question on Stackoverflow
Solution 1 - GoLinearView Answer on Stackoverflow