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.