为什么我的 Go 循环中出现了“Unreachable Code”错误?

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

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 &lt; 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 &lt; 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 &lt; 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

huangapple
  • 本文由 发表于 2022年4月21日 01:14:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/71943701.html
匿名

发表评论

匿名网友

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

确定