find the length of a map in go

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

main.go
package main

import "fmt"

func main() {
    // Declare and initialize a new map.
    m := map[string]int{"foo": 42, "bar": 69, "baz": 99}

    // Get the length of the map using the len function.
    length := len(m)

    fmt.Printf("The length of the map is %d.\n", length)
}
279 chars
14 lines

In this example, we declare and initialize a new map with three key-value pairs. Then, we use the len function to get the length of the map and assign it to the length variable. Finally, we print out the length of the map using fmt.Printf. The output of this program will be:

main.go
The length of the map is 3.
28 chars
2 lines

gistlibby LogSnag