Golang length of map: len, nil maps, and counting nested values

Golang length of map and golang map len with builtin len; golang get length of map and go length of map for key count; nil map length zero; golang size of map vs counting nested slice values with range.

Published

Updated

Read time 2 min read

Reviewed byDeepak Prasad

Golang length of map: len, nil maps, and counting nested values

If you search for golang length of map, golang map length, length of map golang, golang map len, golang len of map, golang get length of map, go map length, golang size of map, len of map golang, or go length of map, the usual answer is the built-in len: len(m) counts how many keys are in the map. That is different from summing items stored inside each value (for example scores in a slice per student). This page shows len, nil behavior, and how to count nested elements when that is what you really need. For map basics, see maps in Go; for loops, see for and range.

Tested with Go 1.24 on Linux.


golang map len: number of keys

len applied to a map[K]V returns an int: the count of key-value pairs.

go
package main

import "fmt"

func main() {
	scoreRecord := map[string]float64{
		"Ariana": 8.6,
		"Bob":    9.3,
		"Clover": 7.8,
		"Daniel": 9,
	}
	fmt.Println(scoreRecord)
	fmt.Println("len:", len(scoreRecord))
}
Output

Running the program prints the four-entry map and len: 4.


Nil maps and golang len of map

A nil map has length 0. You still cannot assign to keys until the map is initialized with make or a literal:

go
package main

import "fmt"

func main() {
	var m map[string]int
	fmt.Println("nil len:", len(m))
	m = map[string]int{"a": 1}
	fmt.Println("after literal len:", len(m))
}
Output
text
nil len: 0
after literal len: 1

Counting values inside entries (not golang map length)

If each map value is itself a collection, len(m) still counts keys, not inner elements. Sum inner lengths only when that is the metric you want:

go
package main

import "fmt"

func main() {
	scoreRecord := map[string][]float64{
		"Ariana": {5.3, 7.3, 6.9},
		"Bob":    {8.3, 8.7, 9.2},
		"Clover": {7.6, 9.2},
		"Daniel": {8.8, 9.0, 8.9},
	}
	fmt.Println("keys:", len(scoreRecord))

	n := 0
	for _, scores := range scoreRecord {
		n += len(scores)
	}
	fmt.Println("total scores stored:", n)
}
Output
text
keys: 4
total scores stored: 11

Summary

Golang length of map and golang map len mean len(m) for the key count. Golang size of map in the informal sense is usually that same count, not bytes on the heap. len of a nil map is 0. If you need a total across nested slices or maps, loop and add lengths—do not confuse that total with len of the outer map.


References


Frequently Asked Questions

1. How do I golang get length of map or golang map length?

Use the built-in len(myMap). It returns the number of key-value pairs, not the sum of lengths inside values such as slices.

2. What is golang len of map for a nil map?

len on a nil map is defined and returns 0; reading or writing keys still requires a non-nil map (use make or a literal first).

3. Is golang size of map the same as memory size?

No. len only counts entries. Actual memory depends on growth, key sizes, and values; use profiling or runtime metrics if you need footprint.

4. Can I call len while another goroutine writes the map?

No. Maps are not safe for concurrent read and write; synchronize with a mutex or use sync.Map if you need concurrent access.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …