Switch
A switch
statement allows the selective execution of one of several blocks of code based on the value of an expression. It can be thought of as an expanded if ... else
statement, with many potential if
clauses.
Syntax 1
There are many syntaxes that can be used in the switch
statement. The following is probably the most common:
switch <expression> {
case <value 1>:
<statements>
case <value 2>:
<statements>
.
.
.
case <value N>:
<statements>
default:
<statements>
}
When <expression>
is equal to the value of one of the case statements, the corresponding <statements>
block is executed. If the <expression>
matches none of the case
values, then the optional default
statements block is executed. If the default
block is omitted, and none of the values are matched, none of the statements in the switch block are executed.
In Go, the switch
statement differs from other languages like C++ and Java in that the values used by the case
statements are not limited to constants. They can also be variables or full expressions. Additionally, Go only executes the selected case <expressions>
block, or the default
block, so no “break” statement is needed. Code execution automatically resumes after the switch
statement.
Syntax 2
The <expression>
after the switch
can be omitted, making the case
s behave more like expressions in if... else
statements:
switch {
case <expression 1>:
<statements>
case <expression 2>:
<statements>
.
.
.
case <expression N>
<statements>
default:
<statements>
}
The first case <expression>
that evaluates to true will execute its corresponding <statements>
. Again, the default
block is optional, and will execute its <statements>
if none of the case
statements are executed.
This is equivalent to:
switch true { ... }
Syntax 3
In each case, a given case
statement can have more than one matching value (or expression), separated by commas:
switch <expression> {
case <value 1>, <value 2>, ... <value N>:
<statements>
.
.
.
default:
<statements>
}
Values are compared left-to-right and top-to-bottom. The first case
block to match the switch
expression will be executed.
Example
In the example below, a variable i
is declared and used in a switch
expression to test whether the result will be even or odd:
i := 7switch i % 2 {case 0:fmt.Printf("%d is even.\n",i)case 1:fmt.Printf("%d is odd.\n",i)}// Output: 7 is odd.
Codebyte Example
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Go on Codecademy
- Career path
Back-End Engineer
Back-end developers deal with the hidden processes that run behind the scenes, building APIs and databases that power the front-end.Includes 41 CoursesWith Professional CertificationBeginner Friendly105 hours - Free course
Learn Go
Learn how to use Go (Golang), an open-source programming language supported by Google!Beginner Friendly6 hours