Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the nicer way to initialize a map? [duplicate]

Tags:

go

People also ask

How do you initialize a map?

The Static Initializer for a Static HashMap We can also initialize the map using the double-brace syntax: Map<String, String> doubleBraceMap = new HashMap<String, String>() {{ put("key1", "value1"); put("key2", "value2"); }};

How do you initialize a map in go?

Initializing map using map literals: Map literal is the easiest way to initialize a map with data just simply separate the key-value pair with a colon and the last trailing colon is necessary if you do not use, then the compiler will give an error.

How do you initialize a map with 0?

What exactly do you want to initialize to zero? map's default constructor creates empty map. You may increase map's size only by inserting elements (like m["str1"]=0 or m. insert(std::map<std::string,int>::value_type("str2",0)) ).


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.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!