Do Go switch/cases fallthrough or not?

前端 未结 2 1344
南方客
南方客 2020-12-31 00:50

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?

2条回答
  •  Happy的楠姐
    2020-12-31 01:33

    Break is kept as a default but not fallthrough. If you want to get onto the next case for a match, 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 
    default: 
       return 
    }
    

提交回复
热议问题