如何从 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

我应该在这里使用什么来打破开关大小写,而不是让程序遍历每个选择并在每个选择上运行我的函数?

你应该利用 goquery 的 EachWithBreak 方法来停止迭代选择:

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

只要开关机箱中没有剩余代码,就不需要使用 break

最新更新