Skip to content
Home » How to count the items of a map in Golang

How to count the items of a map in Golang

Learn about How to count the items of a map in Golang in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.

How to count the items of a map in Golang

// Golang program to count the items of a map

package main

import "fmt"

func main() {
	CountryCode := make(map[string]int)

	CountryCode["ind"] = 101
	CountryCode["aus"] = 102
	CountryCode["eng"] = 103
	CountryCode["pak"] = 104
	CountryCode["usa"] = 105

	fmt.Println("Length of CountryCode: ", len(CountryCode))
	delete(CountryCode, "pak")
	fmt.Println("Length of CountryCode: ", len(CountryCode))
}

In the main() function, we created a CountryCode map using make() function to store country code of specified country. Map store items in KEY/VALUE pair. Then we count the total number of items of the CountryCode map and then print the result

Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.

Similar Codes :
How to delete an item from a map in Golang
How to create a simple map in Golang