get the length of a map with a key of type int in go

To get the length of a map with a key of type int in Go, you can use the built-in len() function along with the map type.

Here's an example:

main.go
package main

import "fmt"

func main() {
    myMap := make(map[int]string)
    myMap[1] = "one"
    myMap[2] = "two"
    myMap[3] = "three"

    // get the length of the map
    length := len(myMap)

    fmt.Println("Length of myMap:", length)
}
247 chars
16 lines

In this example, we first create a map with a key of type int and a value of type string. We then add some key-value pairs to the map.

To get the length of the map, we simply call len(myMap) and store the result in a variable called length.

When we run this program, the output will be:

main.go
Length of myMap: 3
19 chars
2 lines

indicating that the map has three key-value pairs.

related categories

gistlibby LogSnag