A simple switch statement:
https://codeeval.dev/gist/6397bb8b3b9ff36be35e93e3722b52f8
Notice case can handle multiple values (1 and 3 in above example).
Unlike most other languages like C++ or Java, you don't have to use break to stop one case into continuing execution in following case.
Such default behavior is a frequent source of bugs.
Instead in Go you can ask for such behavior with fallthrough:
https://codeeval.dev/gist/353e035917c1d94a11d070d3ee8cd259
Switch in Go is more flexible than in languages like C++ or Java.
We can switch on strings:
https://codeeval.dev/gist/8e328efa8fe0a97153b300603945c936
switch expression can be empty in which case case can be a boolean expression, not just a constant:
https://codeeval.dev/gist/91ce6f67a3a9b8171ce0d6d813bc331a
caseWhat happens if more than one case statement evaluates as true?
In the above example 6 matches both n > 0 && n%3 == 0 expression and n ≥ 4 expression.
As you can see, only one case is executed, the one defined first.