patterngoCritical
Which is the nicer way to initialize a map?
Viewed 0 times
nicerinitializethewaywhichmap
Problem
As
and
map is a reference type. What is difference between:?m := make(map[string]int32)and
m := map[string]int32{}Solution
One allows you to initialize capacity, one allows you to initialize values:
vs
For an empty map with capacity 0, they're the same and it's just preference.
// 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.
Code Snippets
// Initializes a map with space for 15 items before reallocation
m := make(map[string]int32, 15)// Initializes a map with an entry relating the name "bob" to the number 5
m := map[string]int{"bob": 5}Context
Stack Overflow Q#31064688, score: 309
Revisions (0)
No revisions yet.