현재 3권의 신간들인 Go Optimizations 101, Go Details & Tips 101Go Generics 101이 출간되어 있습니다. Leanpub 서점에서 번들을 모두 구입하시는 방법이 비용 대비 효율이 가장 좋습니다.

Go에 대한 많은 정보들과 Go 101 책들의 최신 소식을 얻으시려면 Go 101 트위터 계정인 @Go100and1을 팔로잉 해주세요.

Basic Control Flows

The control flow code blocks in Go are much like other popular programming languages, but there are also many differences. This article will show these similarities and differences.

An Introduction of Control Flows in Go

There are three kinds of basic control flow code blocks in Go: There are also some control flow code blocks which are related to some certain kinds of types in Go.

Like many other popular languages, Go also supports break, continue and goto code execution jump statements. Besides these, there is a special code jump statement in Go, fallthrough.

Among the six kinds of control flow blocks, except the if-else control flow, the other five are called breakable control flow blocks. We can use break statements to make executions jump out of breakable control flow blocks.

for and for-range loop blocks are called loop control flow blocks. We can use continue statements to end a loop step in advance in a loop control flow block, i.e. continue to the next iteration of the loop.

Please note, each of the above mentioned control flow blocks is a statement, and it may contain many other sub-statements.

Above mentioned control flow statements are all the ones in narrow sense. The mechanisms introduced in the next article, goroutines, deferred function calls and panic/recover, and the concurrency synchronization techniques introduced in the later article concurrency synchronization overview can be viewed as control flow statements in broad sense.

Only the basic control flow code blocks and code jump statements will be explained in the current article, other ones will be explained in many other Go 101 articles later.

if-else Control Flow Blocks

The full form of a if-else code block is like
if InitSimpleStatement; Condition {
	// do something
} else {
	// do something
}

if and else are keywords. Like many other programming languages, the else branch is optional.

The InitSimpleStatement portion is also optional. It must be a simple statement if it is present. If it is absent, we can view it as a blank statement (one kind of simple statements). In practice, InitSimpleStatement is often a short variable declaration or a pure assignment. A Condition must be an expression which results to a boolean value. The Condition portion can be enclosed in a pair of () or not, but it can't be enclosed together with the InitSimpleStatement portion.

If the InitSimpleStatement in a if-else block is present, it will be executed before executing other statements in the if-else block. If the InitSimpleStatement is absent, then the semicolon following it is optional.

Each if-else control flow forms one implicit code block, one if branch explicit code block and one optional else branch code block. The two branch code blocks are both nested in the implicit code block. Upon execution, if Condition expression results in true, then the if branch block will get executed, otherwise, the else branch block will get executed.

Example:
package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	rand.Seed(time.Now().UnixNano()) // needed before Go 1.20

	if n := rand.Int(); n%2 == 0 {
		fmt.Println(n, "is an even number.")
	} else {
		fmt.Println(n, "is an odd number.")
	}

	n := rand.Int() % 2 // this n is not the above n.
	if n % 2 == 0 {
		fmt.Println("An even number.")
	}

	if ; n % 2 != 0 {
		fmt.Println("An odd number.")
	}
}

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.

An else branch code block can be implicit if the corresponding else is followed by another if-else code block, otherwise, it must be explicit.

Example:
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 Loop Control Flow Blocks

The full form of a for loop block is:
for InitSimpleStatement; Condition; PostSimpleStatement {
	// do something
}

for is a keyword. The InitSimpleStatement and PostSimpleStatement portions must be both simple statements, and the PostSimpleStatement portion must not be a short variable declaration. Condition must be an expression which result is a boolean value. The three portions are all optional.

Unlike many other programming languages, the just mentioned three parts following the for keyword can't be enclosed in a pair of ().

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.

The InitSimpleStatement in a for loop block will be executed (only once) before executing other statements in the for loop block.

The Condition expression will be evaluated at each loop step. If the evaluation result is false, then the loop will end. Otherwise the body (a.k.a., the explicit code block) of the loop will get executed.

The PostSimpleStatement will be executed at the end of each loop step.

A for loop example. The example will print the integers from 0 to 9.
for i := 0; i < 10; i++ {
	fmt.Println(i)
}

If the InitSimpleStatement and PostSimpleStatement portions are both absent (just view them as blank statements), their nearby two semicolons can be omitted. The form is called as condition-only for loop form. It is the same as the while loop in other languages.
var i = 0
for ; i < 10; {
	fmt.Println(i)
	i++
}
for i < 20 {
	fmt.Println(i)
	i++
}

If the Condition portion is absent, compilers will view it as true.
for i := 0; ; i++ { // <=> for i := 0; true; i++ {
	if i >= 10 {
		// "break" statement will be explained below.
		break
	}
	fmt.Println(i)
}

// The following 4 endless loops are
// equivalent to each other.
for ; true; {
}
for true {
}
for ; ; {
}
for {
}

If the InitSimpleStatement in a for block is a short variable declaration statement, then the declared variables will be viewed as being declared in the top nesting implicit code block of the for block. For example, the following code snippet prints 012 instead of 0.
for i := 0; i < 3; i++ {
	fmt.Print(i)
	// The left i is a new declared variable,
	// and the right i is the loop variable.
	i := i
	// The new declared variable is modified, but
	// the old one (the loop variable) is not yet.
	i = 10
	_ = i
}

A break statement can be used to make execution jump out of the for loop control flow block in advance, if the for loop control flow block is the innermost breakable control flow block containing the break statement.
i := 0
for {
	if i >= 10 {
		break
	}
	fmt.Println(i)
	i++
}

A continue statement can be used to end the current loop step in advance (PostSimpleStatement will still get executed), if the for loop control flow block is the innermost loop control flow block containing the continue statement. For example, the following code snippet will print 13579.
for i := 0; i < 10; i++ {
	if i % 2 == 0 {
		continue
	}
	fmt.Print(i)
}

switch-case Control Flow Blocks

switch-case control flow block is one kind of conditional execution control flow blocks.

The full form a switch-case block is
switch InitSimpleStatement; CompareOperand0 {
case CompareOperandList1:
	// do something
case CompareOperandList2:
	// do something
...
case CompareOperandListN:
	// do something
default:
	// do something
}
In the full form,

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. We call an implicit code block in such a branch as a branch code block later.

There can be at most one default branch in a switch-case control flow block.

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).

switch-case control flow blocks are breakable, so break statements can also be used in any branch code block in a switch-case control flow block to make execution jump out of the switch-case control flow block in advance.

The InitSimpleStatement in a for loop block will be executed (only once) before executing other statements in the for loop block.

The InitSimpleStatement will get executed firstly when a switch-case control flow gets executed, then the switch CompareOperand0 expression will be evaluated and only evaluated once. The evaluation result is always a typed value. The evaluation result will be compared (by using the == operator) with the evaluation result of each case expression in the CompareOperandListX expression lists, from top to down and from left to right. If a case expression is found to be equal to CompareOperand0, the comparison process stops and the corresponding branch code block of the expression will be executed. If none case expressions are found to be equal to CompareOperand0, the default branch code block (if it is present) will get executed.

A switch-case control flow example:
package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {
	rand.Seed(time.Now().UnixNano()) // needed before Go 1.20
	switch n := rand.Intn(100); n%9 {
	case 0:
		fmt.Println(n, "is a multiple of 9.")

		// Different from many other languages,
		// in Go, the execution will automatically
		// jumps out of the switch-case block at
		// the end of each branch block.
		// No "break" statement is needed here.
	case 1, 2, 3:
		fmt.Println(n, "mod 9 is 1, 2 or 3.")
		// Here, this "break" statement is nonsense.
		break
	case 4, 5, 6:
		fmt.Println(n, "mod 9 is 4, 5 or 6.")
	// case 6, 7, 8:
		// The above case line might fail to compile,
		// for 6 is duplicate with the 6 in the last
		// case. The behavior is compiler dependent.
	default:
		fmt.Println(n, "mod 9 is 7 or 8.")
	}
}

The rand.Intn function returns a non-negative int random value which is smaller than the specified argument.

Note, if any two case expressions in a switch-case control flow can be detected to be equal at compile time, then a compiler may reject the latter one. For example, the standard Go compiler thinks the case 6, 7, 8 line in the above example is invalid if that line is not commented out. But other compilers may think that line is okay. In fact, the current standard Go compiler (version 1.20) allows duplicate boolean case expressions, and gccgo (v8.2) allows both duplicate boolean and string case expressions.

As the comments in the above example describes, unlike many other languages, in Go, at the end of each branch code block, the execution will automatically break out of the corresponding switch-case control block. Then how to let the execution slip into the next branch code block? Go provides a fallthrough keyword to do this task. For example, in the following example, every branch code block will get executed, by their orders, from top to down.
rand.Seed(time.Now().UnixNano()) // needed before Go 1.20
switch n := rand.Intn(100) % 5; n {
case 0, 1, 2, 3, 4:
	fmt.Println("n =", n)
	// The "fallthrough" statement makes the
	// execution slip into the next branch.
	fallthrough
case 5, 6, 7, 8:
	// A new declared variable also called "n",
	// it is only visible in the current
	// branch code block.
	n := 99
	fmt.Println("n =", n) // 99
	fallthrough
default:
	// This "n" is the switch expression "n".
	fmt.Println("n =", n)
}

Please note, For example, the following fallthrough uses are all illegal.
switch n := rand.Intn(100) % 5; n {
case 0, 1, 2, 3, 4:
	fmt.Println("n =", n)
	// The if-block, not the fallthrough statement,
	// is the final statement in this branch.
	if true {
		fallthrough // error: not the final statement
	}
case 5, 6, 7, 8:
	n := 99
	fallthrough // error: not the final statement
	_ = n
default:
	fmt.Println(n)
	fallthrough // error: show up in the final branch
}

The InitSimpleStatement and CompareOperand0 portions in a switch-case control flow are both optional. If the CompareOperand0 portion is absent, it will be viewed as true, a typed value of the built-in type bool. If the InitSimpleStatement portion is absent, the semicolon following it can be omitted.

And as above has mentioned, all branches are optional. So the following code blocks are all legal, all of them can be viewed as no-ops.
switch n := 5; n {
}

switch 5 {
}

switch _ = 5; {
}

switch {
}

For the latter two switch-case control flow blocks in the last example, as above has mentioned, each of the absent CompareOperand0 portions is viewed as a typed value true of the built-in type bool. So the following code snippet will print hello.
switch { // <=> switch true {
case true: fmt.Println("hello")
default: fmt.Println("bye")
}

Another obvious difference from many other languages is the order of the default branch in a switch-case control flow block can be arbitrary. For example, the following three switch-case control flow blocks are equivalent to each other.
switch n := rand.Intn(3); n {
case 0: fmt.Println("n == 0")
case 1: fmt.Println("n == 1")
default: fmt.Println("n == 2")
}

switch n := rand.Intn(3); n {
default: fmt.Println("n == 2")
case 0: fmt.Println("n == 0")
case 1: fmt.Println("n == 1")
}

switch n := rand.Intn(3); n {
case 0: fmt.Println("n == 0")
default: fmt.Println("n == 2")
case 1: fmt.Println("n == 1")
}

goto Statement and Label Declaration

Like many other languages, Go also supports goto statement. A goto keyword must be followed by a label to form a statement. A label is declared with the form LabelName:, where LabelName must be an identifier. A label which name is not the blank identifier must be used at least once.

A goto statement will make the execution jump to the next statement following the declaration of the label used in the goto statement. So a label declaration must be followed by one statement.

A label must be declared within a function body. A use of a label can appear before or after the declaration of the label. But a label is not visible (and can't appear) outside the innermost code block the label is declared in.

The following example uses a goto statement and a label to implement a loop control flow.
package main

import "fmt"

func main() {
	i := 0

Next: // here, a label is declared.
	fmt.Println(i)
	i++
	if i < 5 {
		goto Next // execution jumps
	}
}

As mentioned above, a label is not visible (and can't appear) outside the innermost code block the label is declared in. So the following example fails to compile.
package main

func main() {
goto Label1 // error
	{
		Label1:
		goto Label2 // error
	}
	{
		Label2:
	}
}

Note that, if a label is declared within the scope of a variable, then the uses of the label can't appear before the declaration of the variable. Identifier scopes will be explained in the article blocks and scopes in Go later.

The following example also fails to compile.
package main

import "fmt"

func main() {
	i := 0
Next:
	if i >= 5 {
		// error: jumps over declaration of k
		goto Exit
	}

	k := i + i
	fmt.Println(k)
	i++
	goto Next

// This label is declared in the scope of k,
// but its use is outside of the scope of k.
Exit:
}

The just mentioned rule may change later. Currently, to make the above code compile okay, we must adjust the scope of the variable k. There are two ways to fix the problem in the last example.

One way is to shrink the scope of the variable k.
func main() {
	i := 0
Next:
	if i >= 5 {
		goto Exit
	}
	// Create an explicit code block to
	// shrink the scope of k.
	{
		k := i + i
		fmt.Println(k)
	}
	i++
	goto Next
Exit:
}
The other way is to enlarge the scope of the variable k.
func main() {
	var k int // move the declaration of k here.
	i := 0
Next:
	if i >= 5 {
		goto Exit
	}

	k = i + i
	fmt.Println(k)
	i++
	goto Next
Exit:
}

break and continue Statements With Labels

A goto statement must contain a label. A break or continue statement can also contain a label, but the label is optional. Generally, break containing labels are used in nested breakable control flow blocks and continue statements containing labels are used in nested loop control flow blocks.

If a break statement contains a label, the label must be declared just before a breakable control flow block which contains the break statement. We can view the label name as the name of the breakable control flow block. The break statement will make execution jump out of the breakable control flow block, even if the breakable control flow block is not the innermost breakable control flow block containing break statement.

If a continue statement contains a label, the label must be declared just before a loop control flow block which contains the continue statement. We can view the label name as the name of the loop control flow block. The continue statement will end the current loop step of the loop control flow block in advance, even if the loop control flow block is not the innermost loop control flow block containing the continue statement.

The following is an example of using break and continue statements with labels.
package main

import "fmt"

func FindSmallestPrimeLargerThan(n int) int {
Outer:
	for n++; ; n++{
		for i := 2; ; i++ {
			switch {
			case i * i > n:
				break Outer
			case n % i == 0:
				continue Outer
			}
		}
	}
	return n
}

func main() {
	for i := 90; i < 100; i++ {
		n := FindSmallestPrimeLargerThan(i)
		fmt.Print("The smallest prime number larger than ")
		fmt.Println(i, "is", n)
	}
}


Index↡

The Go 101 프로젝트는 Github 에서 호스팅됩니다. 오타, 문법 오류, 부정확한 표현, 설명 결함, 코드 버그, 끊어진 링크와 같은 모든 종류의 실수에 대한 수정 사항을 제출하여 Go 101을 개선을 돕는 것은 언제나 환영합니다.

주기적으로 Go에 대한 깊이 있는 정보를 얻고 싶다면 Go 101의 공식 트위터 계정인 @go100and1을 팔로우하거나 Go 101 슬랙 채널에j가입해주세요.

이 책의 디지털 버전은 아래와 같은 곳을 통해서 구매할 수 있습니다.
Go 101의 저자인 Tapir는 2016년 7월부터 Go 101 시리즈 책들을 집필하고 go101.org 웹사이트를 유지 관리하고 있습니다. 새로운 콘텐츠는 책과 웹사이트에 수시로 추가될 예정입니다. Tapir는 인디 게임 개발자이기도 합니다. Tapir의 게임을 플레이하여 Go 101을 지원할 수도 있습니다. (안드로이드와 아이폰/아이패드용):
  • Color Infection (★★★★★), 140개 이상의 단계로 이루어진 물리 기반의 캐주얼 퍼즐 게임
  • Rectangle Pushers (★★★★★), 2가지 모드와 104개 이상의 단계로 이루어진 캐주얼 퍼즐 게임
  • Let's Play With Particles, 세가지 미니 게임이 있는 캐주얼 액션 게임
페이팔을 통한 개인 기부도 환영합니다.

색인: