OverView
There are three kinds of basic control flow code blocks in Go:
if-else: two-way conditional execution block.switch-case: multi-way conditional execution block;for: loop block
There are also some control flow block which use as certain kinds of types in Go:
for-range: loop block forcontainertypes;type-switch: multi-way conditional execution block forinterfacetype;select-case:block for channel types;
Execution Jump Statements
Go support break, continue, goto, fallthrough as jump statements;
break:Exceptif-elseother five control flow statements is breakable, we can use break to end the block in advance.continue: continue is use to stop current loop step and continue next step, forforstatement orfor-rangestatement;
Basic Control Flow
If-else
Basic Format
1 | if InitSimpleStatement; Condition { |
Feature
- The
elsestatement is optional; InitSimpleStatementis optional;Conditionmust be an expression which result a bool value;
Implicit code block
Each if-else control flow forms with three block :
- one implicit code block;
- one
ifbranch explicit code block; - one optional
elsebranch code block;
If the InitSimpleStatement in a if-else code block is a short variable declaration, then the declared variables will be viewed as being declared in the top nesting implicit code block of the if-else code block.
1 | package main |
For
Basic Format
1 | for InitSimpleStatement; Condition; PostSimpleStatement { |
Feature
- The three portions are all optional.
- The
InitSimpleStatementin aforloop block will be executed (only once) before executing other statements in theforloop block. - If the
InitSimpleStatementandPostSimpleStatementportions are both absent (just view them as blank statements), their nearby two semicolons can be omitted. - If the
Conditionportion is absent, compilers will view it astrue.
Implicit code block
Each for control flow forms at least two code blocks, one is implicit and one is explicit. The explicit one is nested in the implicit one.
Switch-case
Basic Format
1 | switch InitSimpleStatement; CompareOperand0 { |
Feature
- The
CompareOperand0portion is an expression which is viewed as a typed value (if it is an untyped value, then it is viewed as a type value of its default type), hence it can’t be an untypednil. - Each of the
CompareOperandListX(Xmay represent from1toN) portions must be a comma separated expression list. Each of these expressions shall be comparable withCompareOperand0; - There can be at most one
defaultbranch in aswitch-casecontrol flow block.
Implicit code block
- Each
case CompareOperandListX:ordefault:opens (and is followed by) an implicit code block. The implicit code block and thatcase CompareOperandListX:ordefault:forms a branch. Each such branch is optional to be present - Besides the branch code blocks, each
switch-casecontrol flow forms two code blocks, one is implicit and one is explicit. The explicit one is nested in the implicit one. All the branch code blocks are nested in the explicit one (and nested in the implicit one indirectly).
….