Get map size (number of map entries)

suggest change

The built-in function len returns the number of elements in a map:

m := map[string]int{}
size := len(m)
fmt.Printf("Size of empty map: %d\n\n", size)

m["foo"] = 1
size = len(m)
fmt.Printf("Size of map: %d\n\n", size)
Size of empty map: 0

Size of map: 1

A nil map has size 0:

var m map[string]int
size := len(m)
fmt.Printf("Size of nil map: %d\n\n", size)
Size of nil map: 0

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents