英文:
Why am I getting 'Unreachable Code' error in Go for loop
问题
我有一个三个组件的循环,它遍历一个名为Policies的结构体,该结构体包含一个Policy类型的列表,而Policy类型也是一个结构体。然而,在这个for循环中,"i++"被标记为不可达。我不确定为什么会出现这个错误?因为当我运行应用程序时,它实际上会执行c.sendResultDialog函数,所以它是可达的,但我不知道为什么会出现这个错误。
for i := 0; i < len(policies.Policies); i++ {
if strings.ToLower(c.name) == strings.ToLower(policies.Policies[i].PolicyName) || strings.ToLower(c.name) == strings.ToLower(policies.Policies[i].PolicyId) {
c.sendResultDialog(TestFoundPolicy, c.name, policies.Policies[i].PolicyId, policies.Policies[i].PolicyDescription)
return true
} else {
c.sendResultDialog(PolicyNotFound, c.name)
return false
}
}
英文:
I've got a three component for loop that iterates over a Policies struct, which holds a list of Policy types, which is also a struct. However, in the for loop, the 'i++' is highlighted and apparently unreachable. I'm not sure why? Because when I run the application, it does actually execute the c.sendResultDialog function, so it is reachable, but I don't know why this error is showing up.
for i := 0; i < len(policies.Policies); i++ {
if strings.ToLower(c.name) == strings.ToLower(policies.Policies[i].PolicyName) || strings.ToLower(c.name) == strings.ToLower(policies.Policies[i].PolicyId) {
c.sendResultDialog(TestFoundPolicy, c.name, policies.Policies[i].PolicyId, policies.Policies[i].PolicyDescription)
return true
} else {
c.sendResultDialog(PolicyNotFound, c.name)
return false
}
}
答案1
得分: 2
由于你在循环中使用了return语句,你没有遍历所有的切片值。
你可能会这样做:
policy_found := false
for i := 0; i < len(policies.Policies); i++ {
if strings.ToLower(c.name) == strings.ToLower(policies.Policies[i].PolicyName) || strings.ToLower(c.name) == strings.ToLower(policies.Policies[i].PolicyId) {
c.sendResultDialog(TestFoundPolicy, c.name, policies.Policies[i].PolicyId, policies.Policies[i].PolicyDescription)
policy_found = true
}
}
if !policy_found {
c.sendResultDialog(PolicyNotFound, c.name)
}
return policy_found
这应该修复了你的错误。
英文:
Since, you call return inside the for loop, you are not iterating over all the slice values.
You would probably do this
policy_found := false
for i := 0; i < len(policies.Policies); i++ {
if strings.ToLower(c.name) == strings.ToLower(policies.Policies[i].PolicyName) || strings.ToLower(c.name) == strings.ToLower(policies.Policies[i].PolicyId) {
c.sendResultDialog(TestFoundPolicy, c.name, policies.Policies[i].PolicyId, policies.Policies[i].PolicyDescription)
policy_found = true
}
}
if !policy_found {
c.sendResultDialog(PolicyNotFound, c.name)
}
return policy_found
this should fix your bug
答案2
得分: 2
这很难在没有完整示例的情况下确定(不知道sendResultDialog在做什么)。但是错误可能是因为你在循环内部返回,你可以尝试像这样修改:
policyFound := false
for i := 0; i < len(policies.Policies); i++ {
if strings.ToLower(c.name) == strings.ToLower(policies.Policies[i].PolicyName) || strings.ToLower(c.name) == strings.ToLower(policies.Policies[i].PolicyId) {
c.sendResultDialog(TestFoundPolicy, c.name, policies.Policies[i].PolicyId, policies.Policies[i].PolicyDescription)
policyFound = true
break // 如果你想对所有条目调用sendResultDialog而不仅仅是第一个条目,请删除此行
}
}
if !policyFound {
c.sendResultDialog(PolicyNotFound, c.name)
}
return policyFound
英文:
This is hard to tell without a full example (no idea what sendResultDialog is doing). But the error is likely due to the fact that you're returning from within a loop, you can try something like this instead:
policyFound := false
for i := 0; i < len(policies.Policies); i++ {
if strings.ToLower(c.name) == strings.ToLower(policies.Policies[i].PolicyName) || strings.ToLower(c.name) == strings.ToLower(policies.Policies[i].PolicyId) {
c.sendResultDialog(TestFoundPolicy, c.name, policies.Policies[i].PolicyId, policies.Policies[i].PolicyDescription)
policyFound = true
break // remove this if you want to call sendResultDialog for all entries and not just the first one
}
}
if !policyFound {
c.sendResultDialog(PolicyNotFound, c.name)
}
return policyFound
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论