Directory
- Grammar
- For loop
- If statement
- Switch statement
- Defer statements
- Defer stack
- Conclusion
Preface: The second chapter of Go language, mainly about the grammar of Go language, such as loop, if judgment, Swich statement, defer statement, defer stack, and so on, each syntax is compared with the syntax in Java, each provides a combination of concept + example, Examples can be run directly.
Ben wanted to write the grammar and data type of the go language in a chapter, after writing the grammar found that if the writing data type, the article length is too long, so the data type is placed in the next chapter to write.
Follow the series of articles to learn, all the examples are knocked over, the series of articles after learning, your go statement is also a primer
Syntax for loop
There is only one loop structure in the go language: The For loop, similar to the For loop in Java, except that the FOR keyword has no parentheses (), and curly braces {} must be added.
// 基本结构:for 初始化语句;判定条件;后置语句{ 方法体 }
Such as:
func main() { for i := 0; i < 10; i++ { fmt.Println("当前值", i) }}
The most important thing in the For loop is the decision condition, and once the conditional expression value is false, the loop iteration terminates;
With the FOR keyword modifier, there is a decision condition, you can form a for loop, so the above for loop can also be written like this (you can also post the sentence after the decision condition to try the effect):
func main() { i := 0 for i < 10 { i = i + 1 fmt.Println("当前值", i) }}
If statement
The IF statement for Go is similar to the For loop, with no parentheses (), and curly braces {} must be added.
Such as:
func main() { testIf(2)}func testIf(x int) { if x > 10 { fmt.Println("say hello") } else { fmt.Println("say no") }}
The FOR Loop statement can have variants, and of course if statements have:
func main() { testSimpleIf(2)}func testSimpleIf(x int) { if y := 9 + x; y > 10 { fmt.Println("say hello") } else { fmt.Println("say no") }}
If statement, you can put the initialization statement before the decision condition, except that the scope of the variable is only in the if condition, the for loop is the same, if the initialization statement in the For loop, the scope is also within the for loop.
Switch statement
The Swich statement in Go is shorthand for a series of if-else statements. The case statement is executed by matching the value on the conditional expression, otherwise default is executed.
Basically consistent with the Swich statements in Java, the difference is that each case statement does not have to write the break in the display, and go automatically provides a break after each one.
func main() { testSwich(3)}func testSwich(x int) { switch x { case 1: fmt.Println("1") case 2: fmt.Println("2") default: fmt.Println("default") }}
There is a difference: switch case does not have to be a constant, or it can be an expression, so a series of If-else can be written as such.
Such as:
func testTrueSwich() { switch { case 1 > 1: fmt.Println("1") case 2 < 3: fmt.Println("2") default: fmt.Println("default") }}
Defer statements
使用defer修饰的函数会推迟到外层函数返回之后执行;推迟调用的函数其参数会立即求值,但直到外层函数返回后该函数才会被调用。
func main() { defer fmt.Println("可以你先") fmt.Println("我先说")}
Post-execution output: I'll tell you first.
Defer stack
defer 栈 顾名思义:就是推迟执行的一系列的函数,将函数按顺序入栈。栈的数据就是先进后出,所以就是以为这先入栈的函数会最后在执行,执行下面的示例看下输出结果,有不明白的也明白了。
func testDefer() { fmt.Println("开始") for i := 0; i < 10; i++ { defer fmt.Println("当前值", i) } fmt.Println("结束")}
Conclusion
First knowledge of Go language series
- [x] First knowledge of the go language
- [x] First knowledge of Go language: Grammar
- [] first knowledge of Go language: Data type
- [] First knowledge of the go language: methods, interfaces and concurrency
Follow me on the public number
First knowledge of Go language: Grammar