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
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents