In addition, it just so happens that the break at the end situation is the most common situation for a switch statement. So all Go did was flip the requirements: instead of needing an explicit break to not fall through, have an explicit fallthrough to not break.
The Go model of break by default except for explicit fallthrough also allows select statements to look identical to switch statements at a glance. (Of course, by their nature, a select statement cannot have a fallthrough.) Consistent grammars make things easier to learn and easier to use. Other languages took different approaches to closing the implicit-fallthrough trap. C#, for instance, **requires** a break statement at the end of every case block, or a "goto case" or "goto default" that allows jumping from case to case, or some other control flow mechanism like return, throw, or for(;;) that will never allow execution to continue into the next case label. The only type of fallthrough is consecutive case labels (case 5: case 6: case 7:). There's no shorthand for falling into the next case body; you have to spell out the case expression every time (goto case 4; case 4:). I'm sure there are other approaches in other languages, but I don't know them off the top of my head. > On Oct 19, 2016, at 2:36 PM, Konstantin Khomoutov > <flatw...@users.sourceforge.net> wrote: > > On Wed, 19 Oct 2016 18:47:03 +0300 > Konstantin Khomoutov <flatw...@users.sourceforge.net> wrote: > > [...] >>> Please, can you explain the follwing output: > [...] >> In addition to what Ian said, the way to understand how this works, >> is to compare it with how it works in C. In C, where the "classic" >> switch was implemented (and then copied to many different languages), >> branches of this statement felt through by default -- unless an >> explicit "break" statement was terminating a branch. >> >> The idea behind that approach supposedly was to implement a single >> "wall of code" with several entry points into it -- the branches. >> Once the control flow enters that wall of code, it executes to its >> end. > [...] > > For more fun regarding the C's switch statement, you can also consider > reading [1] which makes explicit use of the fallthrough-by-default > behaviour (plus more weirdness). > > 1. https://en.wikipedia.org/wiki/Duff%27s_device > > -- > You received this message because you are subscribed to the Google Groups > "golang-nuts" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-nuts+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.