简体   繁体   中英

How to check for three different struct keys in the golang map?

I have a below method where I create three different struct object to check as a key in my name map. If I can find the value, I return back otherwise I return empty string and status.

func (r *clientRepo) GetName(id int64, name map[models.LocaleInfo]string, clientId int32, code models.Locale) (string, bool) {
    localeInfo := models.LocaleInfo{
        Locale:  code,
        GroupID: int(clientId),
    }

    localeInfoUS := models.LocaleInfo{
        Locale:  "US",
        GroupID: int(clientId),
    }

    localeInfoGB := models.LocaleInfo{
        Locale:  "GB",
        GroupID: int(clientId),
    }

    value := ""
    ok := false
    if value, ok = name[localeInfo]; !ok {
        if value, ok = name[localeInfoUS]; !ok {
            if value, ok := name[localeInfoGB]; !ok {
                errs.Newf("Unable to find locale for ProductId: %d", id)
                return value, ok
            }
        }
    }
    
    return value, ok
}

Is there any way to improve above code where I don't have to keep separate line for each US and GB locale info. My if block also looks very nested and I think that can be improved a lot as well.

The special "comma-ok" form can only be used in an assignment, so you can't make it more compact.

What you may do is write a utility function which uses a for loop to check any number of keys.

Using Go 1.18 generics, you can make it work with any map type:

func findKeys[K comparable, V any](m map[K]V, keys ...K) (v V, ok bool) {
    for _, key := range keys {
        if v, ok = m[key]; ok {
            return
        }
    }
    return
}

Testing it:

m := map[int]string{
    1: "one",
    2: "two",
    3: "three",
    4: "four",
}

fmt.Println(findKeys(m, 5, 6, 4))
fmt.Println(findKeys(m, 1, 2, 6))
fmt.Println(findKeys(m, 6, 8))

Which outputs (try it on the Go Playground ):

four true
one true
 false

Note: if you're using a Go prior to Go 1.18, simply use a non-generic version of findKeys() :

func findKeys(m map[models.LocaleInfo]string, keys ...models.LocaleInfo) (v string, ok bool) {
    for _, key := range keys {
        if v, ok = m[key]; ok {
            return
        }
    }
    return
}

You could use this findKeys() like this:

value, ok := findKeys(name, localeInfo, localeInfoUS, localeInfoGB)
if !ok {
    errs.Newf("Unable to find locale for ProductId: %d", id)
}
return value, ok

Also note that if you're using a non-generic version, since it will be restricted to your map type anyway, you can move more code into it: it can handle the key creation, so you only have to pass the string locale values (and the clientId ):

func findKeys(names map[models.LocaleInfo]string, clientId int, locales ...string) (v string, ok bool) {
    key := models.LocaleInfo{GroupID: clientId}
    for _, locale := range locales {
        key.Locale = locale
        if v, ok = names[key]; ok {
            return
        }
    }
    return
}

Calling it is like:

value, ok := findKeys(names, int(clientId), code, "US", "GB")

See related question: Check if key exists in multiple maps in one condition

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM