Go Reference ยท Keywords and Statements
The map type
Reference for Go map types: key requirements, make and literals, the comma-ok lookup, nil maps, deletion, iteration order and concurrency rules.
A map[K]V is an unordered collection of key-value pairs implemented as a hash table. Lookups, inserts and deletes take constant time on average. Keys must be of a comparable type (anything that supports ==), and values can be any type.
A map is a reference to a runtime structure: assigning a map to another variable or passing it to a function shares the same data. The zero value is nil, which can be read but not written to.
ages := map[string]int{"ana": 31, "bo": 28} ages["cy"] = 40
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.
Syntax
MapType = "map" "[" KeyType "]" ElementType .
m := make(map[K]V) // empty map m := make(map[K]V, hint) // with a size hint m := map[K]V{k1: v1} // literal v := m[k] // zero value if k is absent v, ok := m[k] // ok reports presence m[k] = v // insert or replace delete(m, k) // remove (no-op if absent)
| Part | Description |
|---|---|
K |
A comparable type: booleans, numbers, strings, pointers, channels, arrays and structs of comparable types, and interfaces. Not slices, maps or functions. |
V |
Any type. |
len(m) |
Number of entries. |
clear(m) |
Removes all entries (Go 1.21+). |
Usage notes
- Nil map. Reading from a nil map yields zero values, ranging over it does nothing,
deleteis a no-op, but assigning panics with "assignment to entry in nil map". Initialize withmakeor a literal. - Iteration order is unspecified and randomized. Never rely on it; collect and sort the keys when you need a stable order, or use
slices.Sorted(maps.Keys(m)). - Modifying while ranging. Deleting entries during a range is safe. Entries added during iteration may or may not be visited.
- Not addressable. You cannot take
&m[k], and you cannot assign to a field of a struct stored directly as a value (m[k].f = 1is an error). Store pointers, or copy, modify and store back. - Float keys.
NaN != NaN, so NaN keys can be inserted but never found again. Avoid float keys. - Concurrency. Maps are not safe for simultaneous use. Concurrent writes (or a write with reads) can crash the program with "fatal error: concurrent map writes". Protect with a
sync.Mutex/RWMutexor usesync.Map. - Value presence. Use the comma-ok form when the zero value is a legitimate stored value.
- Memory. Maps do not shrink after deletes; create a new map or use
clearto reuse the allocated space. - Helpers. The
mapspackage offersKeys,Values,Clone,EqualandDeleteFunc.