get the length of a map in go

To get the length of a map in Go, you can use the built-in len function. Here's an example:

main.go
myMap := map[string]int{
    "apple":  1,
    "banana": 2,
    "orange": 3,
}

fmt.Println(len(myMap)) // output: 3
116 chars
8 lines

In this example, we've defined a map myMap with 3 key-value pairs. To get the length of the map, we use the len function and pass in the map as its argument. The len function returns the number of key-value pairs in the map, which in this case is 3.

related categories

gistlibby LogSnag