为什么我得到无法实现的代码'循环的错误
我有一个循环的三个组件,可以在策略结构上进行迭代,该策略结构保存了策略类型的列表,这也是一个结构。但是,在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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
没有一个完整的例子,这是很难说的(不知道SendResultDialog在做什么)。但是错误可能是由于您从循环中返回的事实,您可以尝试这样的事情:
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:
由于您在for循环中调用返回,因此您并没有在所有切片值上迭代。
您可能会这样做
应该解决您的错误
Since, you call return inside the for loop, you are not iterating over all the slice values.
You would probably do this
this should fix your bug