我编写了下面的代码来检测结果是否有超过 1 个具有价值的 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
}
GCT1015
倚天杖
子衿沉夜
相关分类