2016-11-26 18:49
浏览 12


What happens when you reach the end of a Go case, does it fall through to the next, or assume that most applications don't want to fall through?

图片转代码服务由CSDN问答提供 功能建议

当您到达Go案件的末尾,跌至下一个案件或假设大多数 应用程序不想失败?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • drouie2014 2016-11-26 20:20

    No, Go switch statements do not fall through by default. If you do want it to fallthrough, you must explicitly use a "fallthrough" statement. From the spec:

    In a case or default clause, the last non-empty statement may be a (possibly labeled) "fallthrough" statement to indicate that control should flow from the end of this clause to the first statement of the next clause. Otherwise control flows to the end of the "switch" statement. A "fallthrough" statement may appear as the last statement of all but the last clause of an expression switch.

    For example (sorry, I could not for the life of me think of a real example):

    switch 1 {
    case 1:
        fmt.Println("I will print")
    case 0:
        fmt.Println("I will also print")

    点赞 打赏 评论
  • dongyupen6269 2019-08-12 23:06

    Break is kept as a default but not fallthrough , if you want to get onto next case for a match then you should explicitly mention fallthrough.

    switch choice {
    case "optionone":
        // some instructions 
        fallthrough // control will not come out from this case but will go to next case.
    case "optiontwo":
       // some instructions 


    点赞 打赏 评论

相关推荐 更多相似问题