dpj997991 2014-05-04 22:41
浏览 179
已采纳

使用“ Continue Label”和使用“ break”跳出Go的内循环之间的区别

In order to jump out of inner loop, and continue with the outer loop, we could either use continue Label or use break.

Here's an example from http://www.goinggo.net/2013/11/label-breaks-in-go.html

The original post is using the continue Label pattern:

    guestList := []string{"bill", "jill", "joan"}
    arrived := []string{"sally", "jill", "joan"} 

CheckList:
    for _, guest := range guestList {
        for _, person := range arrived {
            fmt.Printf("Guest[%s] Person[%s]
", guest, person)

            if person == guest {
                fmt.Printf("Let %s In
", person)
                continue CheckList
            }
        }
    }

The same result could be achieved through using break, as shown here: http://play.golang.org/p/0YUjkdxxRE

guestList := []string{"bill", "jill", "joan"}
arrived := []string{"sally", "jill", "joan"}

for _, guest := range guestList {
    for _, person := range arrived {
        fmt.Printf("Guest[%s] Person[%s]
", guest, person)

        if person == guest {
            fmt.Printf("Let %s In
", person)
            break
        }
    }
}

Which way is more idiomatic in Go?

  • 写回答

1条回答 默认 最新

  • doushui3061 2014-05-04 22:59
    关注

    I don't know if there's any widely accepted standard for which to use. Note that these two are not equivalent, however.

    Take:

    for _,guest := range guestList {
        for _,person := range arrived {
            fmt.Printf("Guest[%s] Person[%s]
    ", guest, person)
    
            if person == guest {
                fmt.Printf("Let %s In
    ", person)
                break
            }
        }
        fmt.Println("Superfluous line!")
    }
    

    vs

    OuterLoop:
    for _,guest := range guestList {
        for _,person := range arrived {
            fmt.Printf("Guest[%s] Person[%s]
    ", guest, person)
    
            if person == guest {
                fmt.Printf("Let %s In
    ", person)
                continue OuterLoop
            }
        }
        fmt.Println("Superfluous line!")
    }
    

    If the break or continue is hit, the former will print "Superfluous Line!", while the latter will not. Which you use largely depends on whether you want code in the outer loop to execute after your inner loop terminates. If no such code exists, then it largely depends on whether you think you'll ever add code there.

    Keep in mind what you're saying semantically in the code. One is saying "I would like the outer loop to go to its next iteration (or exit if none exists)". The other is saying "I would like this loop to exit". Overall, I think most people find that code that affects and considers, as much as possible, the most local scope is generally the clearest. For this reason I'd avoid the labelled continue unless it's necessary, but I don't think it's a great holy affront either way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记