Control statements for the Go Basics
Supplemental knowledge
- Pointer
- Unlike other languages, pointer arithmetic is not supported in Go
->
, and a selector is used .
to manipulate the members of the pointer target object directly.
- Operator
&
takes the address of the variable, using *
indirect access to the target object through the pointer
- Default value bit nil, not NULL
The instance code is as follows
//指针的使用package mainimport "fmt"func main() { a := 1 //定义一个变量,类型位int a++ //必须单独放一行 var p *int = &a // 指针p指向a的内存地址 fmt.Println(p) //访问p fmt.Println(*p) //取p内存地址指向的值}
Run results
shell 0xc04200e0b8 //内存地址 2 //地址指向的值
Judging if control statements
conditional expression without parentheses
Supports an initialization expression (which can be in parallel mode)
The left curly brace and conditional statement or else on the same line
Supports when row mode
The variable in the initialization statement is the block level, and the variable with the same name outside the bin
EG1:
//if语句:初始化语句a :1package mainimport ( "fmt")func main() { a := 10 if a := 1; a > 0 { //此时a是局部变量,作用域只在if语句内 fmt.Println(a) } fmt.Println(a)}
EG2:
//if..else语句:同时判断两种情况package mainimport "fmt"func main() { a := 10 if a, b := 1, 2; a > 1 { fmt.Println(a) } else { fmt.Println(b) } fmt.Println(a)} /*output210*/
For Loop statement
- Go is only for a loop statement keyword, but it supports 3 different forms
- Initialization and progression expressions can be multiple values
- The conditional statement is re-examined each time, so it is not recommended to use a function in a conditional statement, try to calculate the condition in advance and replace it with a variable or constant
-
The left brace must be on the same line as the conditional statement
EG1: Infinite loop
//For statement: Normal cycle Package main import ("FMT") func Main () {A: = 1 for {a++ if a >} fmt. Println (A)} FMT. Println ("Over")}
eg2:for conditional expression
//for conditional expression Package Mainimport ("FMT") func Main () {A: = 1 for a < 3 {//conditional expression a++ FMT. Println (A)} FMT. Println ("Over")}
Eg3: most common
//for: Step expression, most common package mainimport ("FMT") func Main () {A: = 1 for I: = 0; i < 3; i++ {//Step setting: When I<3, control a a++ fmt. Println (A)} FMT. Println ("Over")}
Note: The variable declared in the for loop disappears after each loop, and the scope is only valid in this loop
package Mainimport "FMT" Func Main () {for i: = 0; i < 3; i++ {A: = 1 fmt. PRINTLN (&a)//output A memory address}}/* output 0xc04204a0800xc04204a0880xc04204a0b0*/
SELECT statement Switch
- You can use any type or expression as a conditional statement
- No write break is required, once the condition meets automatic termination
- If you want to continue with the next case, you need to use
fallthrough
keywords
- Supports an initialization expression (which can be used in parallel mode), with a semicolon on the right
The left brace must be on the same line as the conditional statement
EG1: The most classic usage
/*检查a:如果a=0;输出a =0;如果a=1;输出a=1;...如果条件都不满足:默认输出None*/package mainimport ( "fmt")func main() { a := 1 switch a { case 0: fmt.Println("a=0") case 1: fmt.Println("a=1") default: fmt.Println("None") }}
EG2: Conditions satisfy multiple conditions at the same time, using keywordsfallthrough
/*同时满足多种情况,使用关键词fallthrough*/package mainimport "fmt"func main() { a := 1 switch { case a >= 0: fmt.Println("a=0") fallthrough //fallthrough用法,类似于elif case a >= 1: fmt.Println("a=1") default: fmt.Println("None") }}
Eg3:swith followed by initialization statement
/*swtich后面跟初始化语句: 初始化语句中的变量作用域在switch语句中*/package mainimport "fmt"func main() { switch a := 1; { case a >= 0: fmt.Println("a=0") fallthrough //fallthrough用法,类似于elif case a >= 1: fmt.Println("a=1") default: fmt.Println("None") }}
Jump Statement Goto,break,continue
- Three syntax can be used in conjunction with a label
- Label names are case-sensitive and can cause compilation errors if not used
- Break and continue mate tags can be used to jump out of multi-layer loops
Goto is the adjustment of the execution position, which is not the same as the result of the other 2 statement mates label
Eg1:break statements
//break跳转语句,可以跳转出多存循环package mainimport "fmt"func main() {LABEL1: //定义标签 for { //这是一个无线循环 for i := 0; i < 10; i++ { //有限循环 print(i) //循环四遍跳出循环 if i > 3 { fmt.Println("OK") break LABEL1 //直接跳出N层循环 } } } fmt.Println("OK")}
Eg2:goto statement, transfer to a loop, continue execution
package mainimport "fmt"func main() { for { //这是一个无线循环 for i := 0; i < 10; i++ { //有限循环 if i > 3 { goto LABEL1 //返回到LABEL1层继续执行 } } }LABEL1: //使用goto跳转的话,尽量将标签放goto语句之后 fmt.Println("OK")}
Eg3:continue statement, internal for Each loop after encountering continue, returns LABEL1
// continue语句package mainimport "fmt"func main() {LABEL1: for i := 0; i < 10; i++ { //有限循环 for { //无限循环 fmt.Println(i) continue LABEL1 // 遇到LABEL1后,不会继续执行,而是跳到同级别 fmt.Println("here") //永远不会执行这项操作 } } fmt.Println("OK") //循环十次,最后执行}