How to Count the Items in the Map in Go

Created
Modified

Using len Function

The built-in function len take arguments of various types and return a result of type int. The implementation guarantees that the result always fits into an int.

See the following example:

package main

import "fmt"

func main() {
  m := make(map[string]int, 30)
  var n map[string]int

  fmt.Println(len(m))
  fmt.Println(len(n))
}
0
0

len(s):

string type      string length in bytes
[n]T, *[n]T      array length (== n)
[]T              slice length
map[K]T          map length (number of defined keys)
chan T           number of elements queued in channel buffer
type parameter   see below

Related Tags

#count# #map#