分支语句
单分支语句
func main() {
s := "屌丝"
if s == "屌丝" {
fmt.Println("左手一个,右手一个")
}
// if支持1个初始化语句,初始化语句和判断条件以分号分隔
if a := 10; a == 10 {
fmt.Println("a = 10")
}
a := 8
if a == 10 {
fmt.Println("a==10")
} else if a > 10 {
fmt.Println("a>10")
} else if a < 10 {
fmt.Println("a<10")
} else {
fmt.Println("这是不可能的!!")
}
}
多分支语句
switch
func main() {
num := 12
switch num {
case 1:
fmt.Println("按下的是1楼")
// break //go保留了break关键字,跳出语句;默认就是break
fallthrough //继续无条件执行下一个条件语句
case 2:
fmt.Println("按下的2楼")
// break
case 3: // case后面支持多个条件
fmt.Println("按下的3楼")
// break
case 4:
fmt.Println("按下的4楼")
// break
default:
fmt.Println("按下的xxxx楼")
}
socre := 85
switch {
case socre > 90:
fmt.Println("优秀")
case socre > 80:
fmt.Println("良好")
case socre > 70:
fmt.Println("一般")
default:
fmt.Println("other....")
}
}
循环语句
for
func main() {
sum := 0
for i := 1; i <= 100; i++ {
sum += i
}
fmt.Println("sum = ", sum)
}
break
- 跳出循环
- 跳出到指定 tag
package main
import "fmt"
func main() {
outer:
for i:=0; i< 10; i++ {
for j := 0; j<10; j ++ {
if i == 5 {
break outer
}
fmt.Printf("i, j = %d, %d\t", i, j )
}
fmt.Println()
}
fmt.Println("main outer....")
}
输出
D:\projects\gocode\go100\breakdemo
λ go run main.go
i, j = 0, 0 i, j = 0, 1 i, j = 0, 2 i, j = 0, 3 i, j = 0, 4 i, j = 0, 5 i, j = 0, 6 i, j = 0, 7 i, j = 0, 8 i, j = 0, 9
i, j = 1, 0 i, j = 1, 1 i, j = 1, 2 i, j = 1, 3 i, j = 1, 4 i, j = 1, 5 i, j = 1, 6 i, j = 1, 7 i, j = 1, 8 i, j = 1, 9
i, j = 2, 0 i, j = 2, 1 i, j = 2, 2 i, j = 2, 3 i, j = 2, 4 i, j = 2, 5 i, j = 2, 6 i, j = 2, 7 i, j = 2, 8 i, j = 2, 9
i, j = 3, 0 i, j = 3, 1 i, j = 3, 2 i, j = 3, 3 i, j = 3, 4 i, j = 3, 5 i, j = 3, 6 i, j = 3, 7 i, j = 3, 8 i, j = 3, 9
i, j = 4, 0 i, j = 4, 1 i, j = 4, 2 i, j = 4, 3 i, j = 4, 4 i, j = 4, 5 i, j = 4, 6 i, j = 4, 7 i, j = 4, 8 i, j = 4, 9
main outer....
迭代语句
range
func main() {
str := "hello"
// 传统方式 for
// for i := 0; i < len(str); i++ {
// fmt.Printf("str[%d] = %c\n", i, str[i])
// }
// for + range 迭代, 类似 python enumerate
for idx, data := range str {
fmt.Printf("str[%d] is %c\n", idx, data)
}
for idx, _ := range str { // 支持匿名变量
fmt.Printf("str[%d] is %c\n", idx, str[idx])
}
}
练习: 打印99乘法表
func main() {
for i := 1; i <= 9; i++ {
for j := 1; j <= i; j++ {
fmt.Printf("%d * %d = %d ", j, i, j*i)
}
fmt.Println("")
}
}
/*
1 * 1 = 1
1 * 2 = 2 2 * 2 = 4
1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
1 * 9 = 9 2 * 9 = 18 3 * 9 = 27 4 * 9 = 36 5 * 9 = 45 6 * 9 = 54 7 * 9 = 63 8 * 9 = 72 9 * 9 = 81
*/
range 变量地址
package main
import (
"fmt"
"os"
)
func main() {
var s, sep string
for _, arg := range os.Args[1:] {
fmt.Printf("arg addr: %v, value: %v\n", &arg, arg)
s += sep + arg
sep = " "
}
fmt.Println(s)
}
输出
D:\projects\gocode\studylib\gopro01>go run main.go 11 aa bb
arg addr: 0xc0000881e0, value: 11
arg addr: 0xc0000881e0, value: aa
arg addr: 0xc0000881e0, value: bb
11 aa bb
跳出/过循环
- break 跳出本层循环;可用于
fro, switch, select
- continue 跳过本次循环,继续下一次循环;只能用于
for
goto语句
goto可以用在任何地方,但是不能跨函数使用
func main() {
fmt.Println("11111111111")
goto End
fmt.Println("2222222222222")
End:
fmt.Println("end end end end end")
}