Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从golang中开关盒内部定义的功能中突破开关盒?_Go_Switch Statement_Goquery - Fatal编程技术网

如何从golang中开关盒内部定义的功能中突破开关盒?

如何从golang中开关盒内部定义的功能中突破开关盒?,go,switch-statement,goquery,Go,Switch Statement,Goquery,这个问题听起来很奇怪,但我不知道还有什么更好的说法。我正在使用goquery,我在一个开关盒中: 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

这个问题听起来很奇怪,但我不知道还有什么更好的说法。我正在使用goquery,我在一个
开关盒中

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
会产生以下错误:
中断不在循环中


我应该在这里使用什么来打破切换情况,而不是让程序迭代每个选择并在每个选择上运行我的函数?

您应该使用goquery的方法停止迭代选择:

switch {
    case url == url1:
        doc.Find("xyz").EachWithBreak(func(i int,s *goquery.Selection) bool {
            a,_ := s.Attr("href")
            return a != b
        })
}

只要您的交换机外壳中没有剩余的代码,您就不需要使用
中断

转到是您的朋友