分支语句

单分支语句

  1. func main() {
  2. s := "屌丝"
  3. if s == "屌丝" {
  4. fmt.Println("左手一个,右手一个")
  5. }
  6. // if支持1个初始化语句,初始化语句和判断条件以分号分隔
  7. if a := 10; a == 10 {
  8. fmt.Println("a = 10")
  9. }
  10. a := 8
  11. if a == 10 {
  12. fmt.Println("a==10")
  13. } else if a > 10 {
  14. fmt.Println("a>10")
  15. } else if a < 10 {
  16. fmt.Println("a<10")
  17. } else {
  18. fmt.Println("这是不可能的!!")
  19. }
  20. }

多分支语句

switch

  1. func main() {
  2. num := 12
  3. switch num {
  4. case 1:
  5. fmt.Println("按下的是1楼")
  6. // break //go保留了break关键字,跳出语句;默认就是break
  7. fallthrough //继续无条件执行下一个条件语句
  8. case 2:
  9. fmt.Println("按下的2楼")
  10. // break
  11. case 3: // case后面支持多个条件
  12. fmt.Println("按下的3楼")
  13. // break
  14. case 4:
  15. fmt.Println("按下的4楼")
  16. // break
  17. default:
  18. fmt.Println("按下的xxxx楼")
  19. }
  20. socre := 85
  21. switch {
  22. case socre > 90:
  23. fmt.Println("优秀")
  24. case socre > 80:
  25. fmt.Println("良好")
  26. case socre > 70:
  27. fmt.Println("一般")
  28. default:
  29. fmt.Println("other....")
  30. }
  31. }

循环语句

for

  1. func main() {
  2. sum := 0
  3. for i := 1; i <= 100; i++ {
  4. sum += i
  5. }
  6. fmt.Println("sum = ", sum)
  7. }

break

  • 跳出循环
  • 跳出到指定 tag
  1. package main
  2. import "fmt"
  3. func main() {
  4. outer:
  5. for i:=0; i< 10; i++ {
  6. for j := 0; j<10; j ++ {
  7. if i == 5 {
  8. break outer
  9. }
  10. fmt.Printf("i, j = %d, %d\t", i, j )
  11. }
  12. fmt.Println()
  13. }
  14. fmt.Println("main outer....")
  15. }

输出

  1. D:\projects\gocode\go100\breakdemo
  2. λ go run main.go
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. main outer....

迭代语句

range

  1. func main() {
  2. str := "hello"
  3. // 传统方式 for
  4. // for i := 0; i < len(str); i++ {
  5. // fmt.Printf("str[%d] = %c\n", i, str[i])
  6. // }
  7. // for + range 迭代, 类似 python enumerate
  8. for idx, data := range str {
  9. fmt.Printf("str[%d] is %c\n", idx, data)
  10. }
  11. for idx, _ := range str { // 支持匿名变量
  12. fmt.Printf("str[%d] is %c\n", idx, str[idx])
  13. }
  14. }

练习: 打印99乘法表

  1. func main() {
  2. for i := 1; i <= 9; i++ {
  3. for j := 1; j <= i; j++ {
  4. fmt.Printf("%d * %d = %d ", j, i, j*i)
  5. }
  6. fmt.Println("")
  7. }
  8. }
  9. /*
  10. 1 * 1 = 1
  11. 1 * 2 = 2 2 * 2 = 4
  12. 1 * 3 = 3 2 * 3 = 6 3 * 3 = 9
  13. 1 * 4 = 4 2 * 4 = 8 3 * 4 = 12 4 * 4 = 16
  14. 1 * 5 = 5 2 * 5 = 10 3 * 5 = 15 4 * 5 = 20 5 * 5 = 25
  15. 1 * 6 = 6 2 * 6 = 12 3 * 6 = 18 4 * 6 = 24 5 * 6 = 30 6 * 6 = 36
  16. 1 * 7 = 7 2 * 7 = 14 3 * 7 = 21 4 * 7 = 28 5 * 7 = 35 6 * 7 = 42 7 * 7 = 49
  17. 1 * 8 = 8 2 * 8 = 16 3 * 8 = 24 4 * 8 = 32 5 * 8 = 40 6 * 8 = 48 7 * 8 = 56 8 * 8 = 64
  18. 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
  19. */

range 变量地址

  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. )
  6. func main() {
  7. var s, sep string
  8. for _, arg := range os.Args[1:] {
  9. fmt.Printf("arg addr: %v, value: %v\n", &arg, arg)
  10. s += sep + arg
  11. sep = " "
  12. }
  13. fmt.Println(s)
  14. }

输出

  1. D:\projects\gocode\studylib\gopro01>go run main.go 11 aa bb
  2. arg addr: 0xc0000881e0, value: 11
  3. arg addr: 0xc0000881e0, value: aa
  4. arg addr: 0xc0000881e0, value: bb
  5. 11 aa bb

跳出/过循环

  • break 跳出本层循环;可用于fro, switch, select
  • continue 跳过本次循环,继续下一次循环;只能用于for

goto语句

goto可以用在任何地方,但是不能跨函数使用

  1. func main() {
  2. fmt.Println("11111111111")
  3. goto End
  4. fmt.Println("2222222222222")
  5. End:
  6. fmt.Println("end end end end end")
  7. }