Golang如何美化搜索唯一ID逻辑

huangapple go评论68阅读模式
英文:

Golang how to beautify the search unique id logic

问题

我已经编写了以下代码来检测结果中是否有多个具有值的SomeStruct,如果只有一个,则返回AnotherStruct.ID。通常情况下,结果只有一个具有值的SomeStruct,其余都为空,然后我将获取AnotherStruct的ID。您可以阅读下面的逻辑,逻辑是正确的,但我觉得它看起来很丑陋,有没有更好的编写方式?

var tmp []string
for _, id := range result {
    if len(id.SomeStruct) > 0 {
        tmp = append(tmp, id.AnotherStruct.ID)
    }
}

if len(tmp) > 1 {
    return "Failure", fmt.Errorf("More than 1 id that has unique code")
} else {
    return tmp[0], nil
}

以下是翻译好的代码部分。

英文:

I have written the below code to detect if the result has more than 1 SomeStruct that is having value, if only one then return the AnotherStruct.ID. Normally the result only have one SomeStruct that is having value, and the rest are empty, then I will get the id of AnotherStruct. You may read my logic below, the logic is correct, but it looks ugly to me, is there a better way to write this?

var tmp []string
for _, id := range result {
	if len(id.SomeStruct) > 0 {
		tmp = append(tmp, id.AnotherStruct.ID)
	}
}

if len(tmp) > 1 {
	return "Failure, ", fmt.Errorf("More than 1 id that has unique code")
} else {
	return tmp[0], nil
}

答案1

得分: 2

你不需要将ID附加到tmp切片中,可以使用计数器并在for循环内部进行检查,这样可以获得更好的性能。也许这会对你有所帮助:

    c := 0
    tmp := ""
	for _, id := range result {
		if len(id.SomeStruct) > 0 {
			c++
			if c > 1 {
				return "", fmt.Errorf("More than 1 id that has unique code")
			}
            tmp = id.AnotherStruct.ID
		}
	}
	return tmp, nil

我忘记了tmp的返回值,谢谢@stefan-zhelyazkov的提醒。

英文:

You don't need to append ID to tmp slice, use a counter and check it inside the for, by this you have better performance. maybe this will help you:

    c := 0
    tmp := ""
	for _, id := range result {
		if len(id.SomeStruct) > 0 {
			c++
			if c > 1 {
				return "", fmt.Errorf("More than 1 id that has unique code")
			}
            tmp = id.AnotherStruct.ID
		}
	}
	return tmp, nil

I missed the tmp return value, thanks @stefan-zhelyazkov

答案2

得分: 2

我不完全理解你的逻辑和用例,但最后的else是多余的,也不符合惯用法。

var tmp []string
for _, id := range result {
    if len(id.SomeStruct) > 0 {
        tmp = append(tmp, id.AnotherStruct.ID)
    }
}

if len(tmp) > 1 {
    return "Failure, ", fmt.Errorf("More than 1 id that has unique code")
}
// else是多余的

return tmp[0], nil
英文:

I don't fully understand your logic & use case, but the last else is redundant and not idiomatic.

var tmp []string
for _, id := range result {
    if len(id.SomeStruct) > 0 {
        tmp = append(tmp, id.AnotherStruct.ID)
    }
}

if len(tmp) > 1 {
    return "Failure, ", fmt.Errorf("More than 1 id that has unique code")
}
// else was redundant

return tmp[0], nil

答案3

得分: 2

你所需要做的就是存储来自另一个结构体的ID,并确保你没有超过1个。

这是对@S4eed3sm答案的扩展:

var tmp string
for _, o := range result {
    if len(o.SomeStruct) > 0 {
        if len(tmp) > 0 {
            return "Failure, ", fmt.Errorf("More than 1 id that has unique code")
        }
        tmp = o.AnotherStruct.ID
    }
}
return tmp, nil
英文:

All you have to do is store the ID from the other struct and make sure that you don't have more than 1.

This is expanding on @S4eed3sm answer:

var tmp string
for _, o := range result {
	if len(o.SomeStruct) > 0 {
		if len(tmp) > 0 {
			return "Failure, ", fmt.Errorf("More than 1 id that has unique code")
		}
		tmp = o.AnotherStruct.ID
	}
}
return tmp, nil

huangapple
  • 本文由 发表于 2021年12月29日 14:36:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/70515928.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定