One, conditional statement
1.1、if
Execute the code in the statement block when the result of the if expression is true
package main
import "fmt"
func main() {
var flag bool = true
if flag {
fmt.Println("true")
}
}
1.2、if-else
When the result of the if expression is true , execute the code in the if statement block, otherwise execute the code in the else statement block
package main
import"fmt"
funcmain() {
var flag bool = false
if flag {
fmt.Println("true")
} else {
fmt.Println("false")
}
}
1.3、if-else if-else
When the result of the if expression is true, the code in the if statement block is executed, otherwise the result of the else if expression is judged from top to bottom, if the result is true, the code in the corresponding statement block is executed and the if-else if-else statement is exited. If the if and else if expressions are both false, the code in the else statement block is executed
package main
import "fmt"
func main() {
var ret int
fmt. Print ( " Please enter the score: " )
fmt.Scan(&ret)
if ret >= 90 {
fmt.Println("A")
} else if ret >= 80 {
fmt.Println("B")
} else if ret >= 60 {
fmt.Println("C")
} else {
fmt.Println("D")
}
}
1.4、初始化子语句
可以在 if语句中初始化语句块内的局部变量,多个语句之间使用逗号(;)分隔
package main
import "fmt"
func main() {
if flag := true; flag {
fmt.Println("flag",flag)
}
}
二、选择语句
2.1、switch-case 值
swtich语句后面跟接变量,此时选择与变量相同的 case语句块执行并退出,若所有 case均不相同则执行 default语句块,case语句中可包含多个不同的值进行匹配
单值
input := "yes"
switch input {
case "yes":
fmt.Println("确认")
case "no":
fmt.Println("取消")
default:
fmt.Println("输入错误")
}
多值
input := "yes"
switch input {
case "yes","y":
fmt.Println("确认")
case "no","n":
fmt.Println("取消")
default:
fmt.Println("输入错误")
}
2.2、switch-case 表达式
switch后不跟接变量,此时自上到下选择第一个表达式为 true的 case语句块执行并退出,若所有 case表达式均为 false,则执行 default语句块
var ret int
fmt.Print("请输入分数:")
fmt.Scan(&ret)
switch {
case ret >= 90:
fmt.Println("A")
case ret >= 80:
fmt.Println("B")
case ret >= 60:
fmt.Println("C")
default:
fmt.Println("D")
}
2.3、初始化子语句
可以在 switch语句中初始化语句块内的局部变量,多个语句之间使用逗号(;)分隔,注意初
始化表达式时后面的逗号(;)不能省略
初始化表达式
switch ret := 80; {
case ret >= 90:
fmt.Println("A")
case ret >= 80:
fmt.Println("B")
case ret >= 60:
fmt.Println("C")
default:
fmt.Println("D")
}
初始化值
switch input := "yes"; input {
case "y","yes":
fmt.Println("yes")
case "n","no":
fmt.Println("no")
default:
fmt.Println("输入错误")
}
2.4、fallthrough
switch-case默认执行 case语句后退出,若需要继续执行下一个 case语句块,可以在 case
语句块中使用 fullthrough进行声明
var ret int
fmt.Print("请输入分数:")
fmt.Scan(&ret)
switch ret := 80; {
case ret >= 90:
fmt.Println("A")
fallthrough
case ret >= 80:
fmt.Println("B")
case ret >= 60:
fmt.Println("C")
default:
fmt.Println("D")
}
三、循环语句
3.1、for
for语句后有三个子语句分别为:初始化子语句,条件子语句和后置子语句
执行顺序为:
a) 初始化子语句
b) 条件子语句
c) 语句块
d) 后置子语句
e) b->c->d
f) …
g) 直到条件子语句为 false结束循环
sum := 0
for i := 0; i <= 100; i++ {
sum += i
}
fmt.Println(sum)
3.2、break和continue 语句
break用于跳出循环,当条件满足则结束循环
continue用于跳过循环,当条件满足这跳过本次循环进行后置或条件子语句执行
for i := 0; i <= 100; i++ {
if i == 60 {
break
}
if i >= 50 {
fmt.Println(i)
continue
}
}
3.3、只有条件子语句的for
sum := 0
i := 1
for i <= 100 {
sum += i
i++
}
fmt.Println(sum)
3.4、死循环
sum := 0
i := 1
for {
if i > 100 {
break
}
sum += i
i++
}
fmt.Println(sum)
3.5、for range 遍历
用于遍历可迭代对象中的每个元素,例如字符串,数组,切片,映射,通道等
针对包含 Unicode字符的字符串遍历是需要使用 for-range,range返回两个元素分别为字
节索引和 rune字符,可通过空白标识符忽略需要接收的变量
//多维数组for range 遍历
for i, k := range a1 {
for j, v := range k {
fmt.Printf("一维:%d 二维:%d 值:%d\n", i, j, v)
}
}
四、label与goto
4.1、goto跳转label
可以通过 goto语句任意跳转到当前函数指定的 label位置
sum := 0
i := 1
START: //定义START标签
if i > 100 {
goto STOP //跳转到STOP标签
} else {
sum += i
i++
goto START
}
STOP:
fmt.Println(sum)
4.2、break和continue跳转label
break和 continue后也可以指定 label用于指定跳出或跳过指定 label同层级的循环
RESTART:
for i := 1; i < 10; i++ {
for j :=1; j < 10; j++ {
if j == 3 {
continue RESTART //跳过外层循环
}
if i == 5 {
break RESTART //跳出外层循环
}
fmt.Println(i,j)
}
}
来自于吴科老师《手撕go语言》 马哥go运维开发第四期