Creating map with/without make

The second form always creates an empty map.

The first form is a special case of a map literal. Map literals allow to create non empty maps:

m := map[bool]string{false: "FALSE", true: "TRUE"}

Now your (generalized) example:

m := map[T]U{}

is a map literal with no initial values (key/value pairs). It’s completely equivalent to:

m := make(map[T]U)

Additionally, make is the only way to specify an initial capacity to your map which is larger than the number of elements initially assigned. Example:

m := make(map[T]U, 50)

will create a map with enough space allocated to hold 50 items. This can be useful to reduce future allocations, if you know the map will grow.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)