find the values of a map in go

To retrieve the values of a map in Go, you can use the range keyword to iterate over the map and retrieve each value.

main.go
m := map[string]int{"foo": 1, "bar": 2, "baz": 3}

// To retrieve the values of the map, use range
for _, value := range m {
    fmt.Println(value)
}
150 chars
7 lines

In this example, we define a map with string keys and integer values. We then use the range keyword to iterate over the map and retrieve the values. Within the loop, we print the value of each key-value pair.

Note that we use the blank identifier _ to ignore the key in this case, since we are only interested in the values of the map.

This will output:

main.go
1
2
3
6 chars
4 lines

gistlibby LogSnag