Quick question regarding "Switch statement"

I understand that if a Select-statement does not contain a default-case it blocks until a case becomes true; is that the same for the Switch-statement?

Select is restricted to communication statements. The switch might not execute anything without default. In this case, next statement is executed. See https://golang.org/ref/spec#Switch_statements

Yes, I had already read the link you provided. Unfortunately, the link only refers to what happens if there is a default-case; it fails to state what happens if there is no default-case. If it’s consistent with the Select-case it should block; if it’s more like a Visual Basic select-case it will fall through. But, again the description doesn’t provide that information; I was hoping someone who’s actually used the switch-case, without a default-case would know.

Switch doesn’t block. As wriiten, statement after switch is executed. It means after } of the last case. Specs doesn’t need to méntion it as it is the normal behaviour of every statement.

There is nothing to block on in a switch - it’s just a list of expressions, equivalent to a series of if, else if, else. I guess you can put channel operations in switch expressions, in which case it behaves just as it would in an if. It does not multiplex over the operations like a select would.

A (very) elementary case here https://play.golang.org/p/akd8euq0NK

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.