The flow control statements of Go language are the basis of its programming. Like other languages, they control the program execution flow and implement decision-making, looping and resource management. This article takes an in-depth look at Go’s flow control statements, including for
, if
, switch
, and defer
, and explains how to use them effectively in Go programs.
This article is part of the Go language tutorial series, designed to help developers understand the Go language more deeply. Whether you are a beginner or an experienced developer, this guide will give you the knowledge you need to write more efficient and readable Go code.
After reading this article, you will master:
Let’s get started!
for
Loopfor
Loop is the only loop structure in Go language, but it is very flexible and can be used in a variety of scenarios:
for
Loop<code class="language-go">for i := 0; i < 10; i++ { fmt.Println(i) }</code>
This is a traditional `for` loop, initializing variables, setting conditions and incrementing variables.
for
Continuous execution of loop (similar to while
loop)
Go does not have the `while` keyword, but you can use a `for` loop to achieve the same effect:
<code class="language-go">sum := 1 for sum < 100 { sum += sum }</code>
This loop continues to execute until the condition `sum < 100` is not true. < 100`不成立。
Infinite loop
If the condition is omitted, the `for` loop will execute infinitely:
<code class="language-go">for { fmt.Println("无限循环") }</code>
This is useful in tasks that need to run continuously (such as servers).
if
Statement`if` statement is used for conditional execution.
Basicif
Statements
<code class="language-go">if x > 10 { fmt.Println("x大于10") }</code>
if
statement with short statement
A short statement can be executed before the condition:
<code class="language-go">if x := 5; x < 10 { fmt.Println("x小于10") }</code>
if
and else
You can also use `else` and `else if`:
<code class="language-go">if x > 10 { fmt.Println("x大于10") } else if x == 10 { fmt.Println("x等于10") } else { fmt.Println("x小于10") }</code>
switch
StatementThe `switch` statement is a powerful way to handle multiple conditions.
Basicswitch
Statements
<code class="language-go">switch os := runtime.GOOS; os { case "darwin": fmt.Println("OS X") case "linux": fmt.Println("Linux") default: fmt.Printf("%s.\n", os) }</code>
switch
Execution order of statements
Go evaluates the cases of the `switch` statement from top to bottom, stopping once a match is successful.
Unconditionalswitch
Statement
The unconditional `switch` statement is equivalent to `switch true`:
<code class="language-go">t := time.Now() switch { case t.Hour() < 12: fmt.Println("上午") case t.Hour() < 18: fmt.Println("下午") default: fmt.Println("晚上") }</code>
defer
StatementThe `defer` statement defers the execution of a function until its surrounding function returns.
Basicdefer
Statements
<code class="language-go">func main() { defer fmt.Println("world") fmt.Println("hello") }</code>
Output:
<code>hello world</code>
Stackeddefer
Statements
Delay functions are executed in last-in-first-out (LIFO) order:
<code class="language-go">func main() { defer fmt.Println("first") defer fmt.Println("second") defer fmt.Println("third") }</code>
Output:
<code>third second first</code>
Practical example
Let’s look at a practical example demonstrating the use of these flow control statements. We will create a simple program that processes a task list and prints its status.
<code class="language-go">for i := 0; i < 10; i++ { fmt.Println(i) }</code>
Detailed explanation of steps
Best Practices
Conclusion
Flow control statements are essential tools in the Go language. They allow you to control the execution flow of your program. By mastering `for`, `if`, `switch`, and `defer`, you can write more efficient, readable, and maintainable Go code.
I encourage you to try the examples provided in this article and experiment with the concepts on your own.
Call to Action
This article is part of the Go language tutorial series, designed to help you become a more proficient Go developer. If you found this article helpful, be sure to check out previous and upcoming tutorials in this series. Check it out on my blog or Dev.to.
Happy programming! ?
The above is the detailed content of Flow Control Statements in Go. For more information, please follow other related articles on the PHP Chinese website!