Map length and capacity

May 15, 2023

Maps, like arrays and slices, have length.

Map types

The number of map elements is called its length. For a map m, it can be discovered using the built-in function len and may change during execution. …

This should be straight forward, but let’s consider a couple examples for good measure:

x := map[string]int{"a": 1, "b": 2, "c": 3}
fmt.Println(len(x)) // 3

var y map[string]float64
fmt.Println(len(y)) // 0

And somewhat deceptively, their capacity can also be set, or at least hinted at, but not read.

A new, empty map value is made using the built-in function make, which takes the map type and an optional capacity hint as arguments:

make(map[string]int)
make(map[string]int, 100)

Use a capacity hint when you know that a map you’re creating is likely or certain to grow to a certain size. Doing so gives the runtime the option to allocate enough memory for the specified capacity. Without this hint, the map will allocate new memory as it grows, contributing additional work to the garbage collector.

Notably, providing the capacity hint does not affect the length of the map.

x := make(map[string]int, 100)
fmt.Println(len(x)) // 0

The initial capacity does not bound its size: maps grow to accommodate the number of items stored in them, with the exception of nil maps.

x := make(map[string]int, 1)
x["a"] = 1
x["b"] = 2
fmt.Println(len(x)) // 2, notably greater than initial capacity of 1

Quotes from The Go Programming Language Specification Version of December 15, 2022


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Constant lengths and expressions

A few of the built-in functions are very special, in that they can evaluate to constant expressions. len and cap are two such functions. But they aren’t always evaluated to constant expressions, sometimes they’re more normal-ish runtime functions. Length and capacity … The expression len(s) is constant if s is a string constant. The expressions len(s) and cap(s) are constants if the type of s is an array or pointer to an array and the expression s does not contain channel receives or (non-constant) function calls; in this case s is not evaluated.


Capacity of slices

Length and capacity … The capacity of a slice is the number of elements for which there is space allocated in the underlying array. At any time the following relationship holds: 0 <= len(s) <= cap(s) Recall that a slice is backed by a fixed-length array, which may have more elements than the slice. When this is the case, the capacity of the slice is said to be the current length of the slice, plus any unused elements in the backing array that extend beyond the final element of the slice.


Length and capacity

Length and capacity The built-in functions len and cap take arguments of various types and return a result of type int. The implementation guarantees that the result always fits into an int. Recall that int is either a 32- or 64-bit integer. So this means that the theoretical maximum length or capacity of the various types that support len and cap depends on the CPU architecture. However, this should not matter in practice, since you’d quickly exceed the available memory, before you had a slice, array, map, or other item with 2^32 elements in it.

Get daily content like this in your inbox!

Subscribe