Bugs on switch

108 views
Skip to first unread message

luthfigea...@gmail.com

unread,
Jun 17, 2019, 10:26:15 AM6/17/19
to golang-nuts
i write example code standard switch statement
https://play.golang.org/p/kOplHDBk8Df

program exited without print something.

Andrew Klager

unread,
Jun 17, 2019, 10:35:26 AM6/17/19
to luthfigea...@gmail.com, golang-nuts
I'm going to guess, based on the "break" statement in the final case, that you expect each of the case statements without a break to fall through to the nest case. Go does not work like that. By default, each case, whether there's any action taken or not, breaks. Take a look at this to accomplish what you're probably looking for: https://play.golang.org/p/0aXXjoshOk9

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/88c11740-e4a2-43af-8d4f-45c986cc55df%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tyler Compton

unread,
Jun 17, 2019, 12:45:57 PM6/17/19
to golang-nuts
Andrew is right, but I'll also add that if you really do want fallthrough semantics, you can use the "fallthrough" keyword: https://play.golang.com/p/x2gBb905Hj0

Go opts to make the somewhat uncommon case of fallthrough explicit, and the more common "break" case implicit. A bit surprising for new Go programmers but it does allow for most switch statements to be quite a bit less verbose.

Reply all
Reply to author
Forward
0 new messages