英文:
How to check for three different struct keys in the golang map?
问题
我有一个方法,在这个方法中,我创建了三个不同的结构体对象,将它们作为键来检查我的name
映射。如果我能找到值,我就返回它,否则返回空字符串和状态。
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
}
有没有办法改进上面的代码,不需要为每个US和GB的区域信息保留单独的行。我的if块看起来也很嵌套,我认为这方面也可以有很大的改进。
英文:
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.
答案1
得分: 1
特殊的“comma-ok”形式只能在赋值语句中使用,所以无法使其更紧凑。
你可以编写一个实用函数,使用for循环来检查任意数量的键。
使用Go 1.18泛型,你可以使其适用于任何映射类型:
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
}
测试一下:
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))
输出结果为(在Go Playground上尝试一下):
four true
one true
false
注意:如果你使用的是Go 1.18之前的版本,只需使用非泛型版本的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
}
你可以像这样使用findKeys()
:
value, ok := findKeys(name, localeInfo, localeInfoUS, localeInfoGB)
if !ok {
errs.Newf("Unable to find locale for ProductId: %d", id)
}
return value, ok
还要注意,如果你使用的是非泛型版本,由于它将被限制为你的映射类型,你可以将更多的代码移到其中:它可以处理键的创建,因此你只需要传递string
类型的区域设置值(以及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
}
调用它的方式如下:
value, ok := findKeys(names, int(clientId), code, "US", "GB")
参考问题:https://stackoverflow.com/questions/41978101/check-if-key-exists-in-multiple-maps-in-one-condition/41978277#41978277
英文:
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: https://stackoverflow.com/questions/41978101/check-if-key-exists-in-multiple-maps-in-one-condition/41978277#41978277
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论