A map literal such as map[string]int{"a": 1} creates a map and fills it with entries in one expression, while make(map[string]int) creates an empty map that you fill later. Both produce an ordinary, fully usable map value of the same type. The real difference is when you know the data and whether you want to give the runtime a size hint.
The old answer to this question described a literal as something that "cannot be modified", which is wrong. A map created from a literal is mutable like any other map. The only thing you must avoid is a nil map, which you get from var m map[string]int and which panics on writes.
A composite literal lists the key-value pairs inside braces. The compiler creates the map and inserts the entries before the variable is first used.
The trailing comma after the last pair is required when the closing brace is on its own line. fmt.Println prints maps with keys sorted, which is why the output is stable even though map iteration order is not.
A literal can also be empty. map[string]int{} is a non-nil, empty map, equivalent to make(map[string]int) for every practical purpose.
When the value type is a struct, slice or pointer to a struct, you can leave out the type in each element.
make(map[K]V) allocates a map with no entries. You then add entries at runtime, typically from a loop, a file or a request.
Assigning to an existing key replaces its value. Assigning to a new key inserts it. Reading a missing key returns the zero value of the value type, so use the two-value form when you need to tell the difference.
make accepts an optional second argument, an initial capacity hint. If you know roughly how many entries you will insert, the hint avoids repeated growth.
The hint is not a limit. The map still grows past it, and len reports the number of entries, not the capacity. A literal has no way to express a hint, which is one reason to prefer make for large, dynamically built maps.
| Aspect | Map literal | make(map[K]V) |
|---|---|---|
| Initial contents | Entries listed in the source | Empty |
| Result | Non-nil, mutable map | Non-nil, mutable map |
| Size hint | Not available | Optional second argument |
| Best for | Fixed lookup tables, config defaults, test data | Maps built from runtime data |
| Duplicate constant keys | Compile-time error | Not applicable |
A literal with two identical constant keys does not compile, which catches copy-paste mistakes in lookup tables.
Declaring a map variable without initialising it gives a nil map. Reading from it works and returns zero values, but writing panics.
Fix it with either a literal or make. A nil map is useful as a read-only "empty" value, for example as the zero value of a struct field, but you must initialise it before the first write.
A package-level literal is a good fit for data known at compile time.
Treat such a variable as read-only after start-up. Maps are not safe for concurrent writes, and a concurrent read and write is a data race that the runtime may detect and abort on.
Start from a literal for defaults, then copy and extend it. Since Go 1.21 the maps package provides maps.Clone and maps.Copy, and the clear built-in empties a map.
Cloning matters because maps are reference-like: assigning b := a makes both variables refer to the same underlying map.
Guard a shared map with a sync.RWMutex, or use sync.Map for the specific cases it was designed for, such as keys written once and read many times. Choosing a literal or make has no effect on thread safety.
Yes. A literal only decides the initial contents. You can add, change and delete entries afterwards.
Neither is meant as a performance switch. For small maps the cost is similar. For large maps built in a loop, make with a capacity hint usually reduces reallocation, so measure with a benchmark before optimising.
map[string]int{} give me?A non-nil, empty map that you can write to immediately. It behaves the same as make(map[string]int).
The variable is almost certainly a nil map from var m map[K]V. Initialise it with a literal or make before assigning to it.
Map literals and make both create real, mutable maps. Use a literal when the initial entries are known when you write the code, and use make, optionally with a capacity hint, when the data arrives at runtime. Never write to a nil map, clone maps with maps.Clone when you need independent copies, and protect shared maps with a mutex.