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 forcontainer
types;type-switch
: multi-way conditional execution block forinterface
type;select-case
:block for channel types;
Execution Jump Statements
Go support break
, continue
, goto
, fallthrough
as jump statements;
break
:Exceptif-else
other 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, forfor
statement orfor-range
statement;
Basic Control Flow
If-else
Basic Format
1 | if InitSimpleStatement; Condition { |
Feature
- The
else
statement is optional; InitSimpleStatement
is optional;Condition
must 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
if
branch explicit code block; - one optional
else
branch 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
InitSimpleStatement
in afor
loop block will be executed (only once) before executing other statements in thefor
loop block. - If the
InitSimpleStatement
andPostSimpleStatement
portions are both absent (just view them as blank statements), their nearby two semicolons can be omitted. - If the
Condition
portion 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
CompareOperand0
portion 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
(X
may represent from1
toN
) portions must be a comma separated expression list. Each of these expressions shall be comparable withCompareOperand0
; - There can be at most one
default
branch in aswitch-case
control 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-case
control 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).
….