为什么我得到无法实现的代码'循环的错误

发布于 2025-01-23 05:50:54 字数 663 浏览 0 评论 0原文

我有一个循环的三个组件,可以在策略结构上进行迭代,该策略结构保存了策略类型的列表,这也是一个结构。但是,在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心房敞 2025-01-30 05:50:54

没有一个完整的例子,这是很难说的(不知道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 // 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

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
长发绾君心 2025-01-30 05:50:54

由于您在for循环中调用返回,因此您并没有在所有切片值上迭代。
您可能会这样做

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文