gpt4 book ai didi

go - 如何从 golang 的开关盒内定义的函数内部跳出开关盒?

转载 作者:IT王子 更新时间:2023-10-29 02:03:22 25 4
gpt4 key购买 nike

这个问题听起来很奇怪,但我不知道有什么更好的表达方式。我正在使用 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 不在循环中

我应该在这里使用什么来打破 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

关于go - 如何从 golang 的开关盒内定义的函数内部跳出开关盒?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43156455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com