This is a creation in Article, where the information may have evolved or changed.
Depending on the incoming condition, the SELECT statement executes a different statement. The following example prints a different content based on the input integer variable i:
Switch I {case 0: fmt. Printf ("0") Case 1: FMT. Printf ("1") Case 2: fallthrough case 3: FMT. Printf ("3") Case 4, 5, 6: FMT. Printf ("4, 5, 6") default:
Running the above case, you will get the following results:
i = 0 o'clock, output 0;
i = 1 o'clock, output 1;
i = 2 o'clock, output 3;
i = 3 o'clock, output 3;
i = 4 o'clock, output 4, 5, 6;
i = 5 o'clock, output 4, 5, 6;
i = 6 o'clock, output 4, 5, 6;
i = Any other value, the output is default.
Interestingly, the expression behind switch is not even required, such as the following example:
Switch {case 0 <= num && num <= 3: FMT. Printf ("0-3") case 4 <= num && num <= 6: FMT. Printf ("4-6") Case 7 <= num && num <= 9: FMT. Printf ("7-9")//http://www.cnblogs.com/osfipin/}
When using the switch structure, we need to pay attention to the following points:
Left Curly brace {must be in the same row as switch;
A conditional expression is not limited to a constant or an integer;
Multiple result options can appear in a single case;
Contrary to the rules of C, the go language does not need to use a break to explicitly exit a case;
The next case will be executed only if the Fallthrough keyword is explicitly added to the case;
You can not set the conditional expression after switch, in which case the entire switch structure with multiple if...else ... The logical function of the same.