0%

Go_Fundamental_Learning_Basic Control Flow

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 for container types;
  • type-switch: multi-way conditional execution block for interface type;
  • select-case:block for channel types;

Execution Jump Statements

Go support break, continue, goto, fallthrough as jump statements;

  • break :Except if-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, for for statement or for-range statement;

Basic Control Flow

If-else

Basic Format

1
2
3
4
5
if InitSimpleStatement; Condition {
// do something
} else {
// do something
}

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main

import (
"fmt"
"time"
)

func main() {
if h := time.Now().Hour(); h < 12 {
fmt.Println("Now is AM time.")
} else if h > 19 {
fmt.Println("Now is evening time.")
} else {
fmt.Println("Now is afternoon time.")
h := h // the right one is declared above
// The just new declared "h" variable
// shadows the above same-name one.
_ = h
}

// h is not visible here.
}

For

Basic Format

1
2
3
for InitSimpleStatement; Condition; PostSimpleStatement {
// do something
}

Feature

  • The three portions are all optional.
  • The InitSimpleStatement in a for loop block will be executed (only once) before executing other statements in the for loop block.
  • If the InitSimpleStatement and PostSimpleStatement 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 as true.

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
2
3
4
5
6
7
8
9
10
11
switch InitSimpleStatement; CompareOperand0 {
case CompareOperandList1:
// do something
case CompareOperandList2:
// do something
...
case CompareOperandListN:
// do something
default:
// do something
}

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 untyped nil.
  • Each of the CompareOperandListX (X may represent from 1 to N) portions must be a comma separated expression list. Each of these expressions shall be comparable with CompareOperand0;
  • There can be at most one default branch in a switch-case control flow block.

Implicit code block

  • Each case CompareOperandListX: or default: opens (and is followed by) an implicit code block. The implicit code block and that case CompareOperandListX: or default: 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).

….

For-range

Type-switch

Select-case