Checks for equality.
Another example that showcases more features:
Go only runs the selected case, not all the cases that follow. So the break
keyword isn’t needed like in other languages.
fallthrough
keyword can be specified so that the case continue to the next one, but it’s generally a sign that you may should restructure your code.
break
keyword
break
keyword can be added to exit early from the switch, but it’s also a bad sign.
When you want to break a loop early with a switch
inside, with this code Go will break switch
statement instead:
To fix it use break
with label:
Blank switch
While regular switch
only allow to check for equality, when you omit the value you compare against, you can any boolean comparison.
Use blank switch to replace a series of if/else
statements when you have multiple related cases. FizzBuzz example:
q What does that mean?
Go’s switch cases need not be constants, and the values involved need not be integers.
https://go.dev/tour/flowcontrol/9
switch
can be used without a condition.
This construct can be a clean way to write long if-then-else chains.
q Why though?