Golang maps access key using value

can we access map key using value?

1 Like

Key from Value

Check this out . I guess this is what you want.


Like Rounak’s link shows: If this is an infrequent operation, you could loop over the map’s keys and values and return the key(s) that match the value, e.g.:

func keysByValue(m map[string]string, value string) []string {
    var keys []string
    for k, v := range myMap {
        if value == v {
            keys = append(keys, k)
    return keys

Like this answer says, if this is a common operation, you’ll want another map to make it more efficient.


Thanks, Rounak
Thanks, Sean

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.