Skip to content
Tutorials Center
My learning

Topics

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.

Go
ages := map[string]int{"ana": 31, "bo": 28}
ages["cy"] = 40

Try it

Go Create, look up, delete

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.

Go nil maps, sets and shared references

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

Go
MapType = "map" "[" KeyType "]" ElementType .
Go
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, delete is a no-op, but assigning panics with "assignment to entry in nil map". Initialize with make or 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 = 1 is 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/RWMutex or use sync.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 clear to reuse the allocated space.
  • Helpers. The maps package offers Keys, Values, Clone, Equal and DeleteFunc.