Welcome To Golang By Example

Menu
  • Home
  • Blog
Menu

Length of a map in Go (Golang)

Posted on June 8, 2023June 9, 2023 by admin

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
  • go
  • golang
  • length
  • map
  • Popular Articles

    Golang Comprehensive Tutorial Series

    All Design Patterns in Go (Golang)

    Slice in golang

    Variables in Go (Golang) – Complete Guide

    OOP: Inheritance in GOLANG complete guide

    Using Context Package in GO (Golang) – Complete Guide

    All data types in Golang with examples

    Understanding time and date in Go (Golang) – Complete Guide

    ©2023 Welcome To Golang By Example | Design: Web XP