如何从 golang 的开关盒内定义的函数内部跳出开关盒?
How to break out of switch case from inside a function defined inside the switch case in golang?
这个问题听起来很奇怪,但我不知道有什么更好的表达方式。我正在使用 goquery,我在 switch-case
:
switch{
case url == url1:
doc.Find("xyz").Each(func(i int,s *goquery.Selection){
a,_ := s.Attr("href")
if a== b{
//I want to break out of the switch case right now. I dont want to iterate through all the selections. This is the value.
break
}
})
}
使用break
会出现以下错误:
break is not in a loop
我应该在这里使用什么来打破 switch-case,而不是让程序遍历每个选择并 运行 我的函数遍历每个选择?
您应该使用 goquery 的 EachWithBreak
方法来停止迭代选择:
switch {
case url == url1:
doc.Find("xyz").EachWithBreak(func(i int,s *goquery.Selection) bool {
a,_ := s.Attr("href")
return a != b
})
}
只要您的 switch case 中没有剩余代码,您就不需要使用 break
。
这个问题听起来很奇怪,但我不知道有什么更好的表达方式。我正在使用 goquery,我在 switch-case
:
switch{
case url == url1:
doc.Find("xyz").Each(func(i int,s *goquery.Selection){
a,_ := s.Attr("href")
if a== b{
//I want to break out of the switch case right now. I dont want to iterate through all the selections. This is the value.
break
}
})
}
使用break
会出现以下错误:
break is not in a loop
我应该在这里使用什么来打破 switch-case,而不是让程序遍历每个选择并 运行 我的函数遍历每个选择?
您应该使用 goquery 的 EachWithBreak
方法来停止迭代选择:
switch {
case url == url1:
doc.Find("xyz").EachWithBreak(func(i int,s *goquery.Selection) bool {
a,_ := s.Attr("href")
return a != b
})
}
只要您的 switch case 中没有剩余代码,您就不需要使用 break
。