Welcome To Golang By Example

Length of a map in Go (Golang)

The golang builtin function len() can be used to get the length of the map which is number of key-value pairs present in the map. Below is the format for using this function on the map.

len(mapName)

Let’s see a program

package main

import "fmt"

func main() {
    //Declare
    employeeSalary := make(map[string]int)

    //Adding a key value
    employeeSalary["Tom"] = 2000
    employeeSalary["Sam"] = 1200

    lenOfMap := len(employeeSalary)
    fmt.Println(lenOfMap)
}

Output

2