Go – Map Length

To get length of map in Go programming, call len() function and pass map as argument to it. The function returns an integer representing the number of key:value pairs in the map.

In this tutorial, we will learn how to use len() function to get the length of a given map.

Syntax

The syntax to find the length of Map x is

</>
Copy
len(x)

Return Value

An integer.

Example

In the following program, we take a map from string to string, and find its length.

example.go

</>
Copy
package main

import "fmt"

func main() {
	map1 := map[string]string{
		"a": "apple",
		"b": "banana",
	}

	var result int = len(map1)
	fmt.Print("Length of map : ", result)
}

Output

Length of map : 2

Conclusion

In this Golang Tutorial, we learned how to find the length of a Map, with the help of example programs.