每月一号例行技术复习 —— Golang 流程控制

Golang控制语句只有四种:if、for、switch、跳转语句,虽简尤强。

if

var a int = 3
if a == 3 {
	fmt.Println("a==3")
}

if b := 3; b == 3 && true || false {
	fmt.Println("b==3")
} else if b != 3 {
	fmt.Println("b!=3")
} else {
	fmt.Println("error")
}

if true && (true || false || false) {
	fmt.Println("输出")
}

for

var i, sum int

for i = 1; i <= 100; i++ {
	sum += i
}
fmt.Println("sum =", sum)

s := "abc"
for i := range s {
	fmt.Printf("%c\n", s[i])
}

for _, c := range s {
	fmt.Printf("%c\n", c)
}

for i, c := range s {
	fmt.Printf("%d, %c\n", i, c)
}

switch

var score int = 90
switch score {
case 90:
	fmt.Println("优秀")
case 80:
	fmt.Println("良好")
case 50, 60, 70:
	fmt.Println("一般")
default:
	fmt.Println("差")
}

switch s1 := 90; s1 {
case 90:
	fmt.Println("优秀")
case 80:
	fmt.Println("良好")
default:
	fmt.Println("一般")
}

var s2 int = 90
switch {
case s2 >= 90:
	fmt.Println("优秀")
case s2 >= 80:
	fmt.Println("良好")
default:
	fmt.Println("一般")
}

switch s3 := 90; {
case s3 >= 90:
	fmt.Println("优秀")
case s3 >= 80:
	fmt.Println("良好")
default:
	fmt.Println("一般")
}

跳转语句

break和continue
	for i := 0; i <= 9; i++ {
		if 2 == i {
			break
		}
		fmt.Println(i) // 0 1
	}

	for i := 10; i <= 19; i++ {
		if 12 == i {
			continue
		}
		fmt.Println(i) // 10 11 13 14 15 16 17 18 19
	}

OuterLoop:
	for i := 0; i <= 9; i++ {
		for j := 0; j <= 9; j++ {
			if i == 1 {
				break OuterLoop
			}
			fmt.Printf("[%d %d]", i, j) // [0 0][0 1][0 2][0 3][0 4][0 5][0 6][0 7][0 8][0 9]
		}
	}
goto
	for i := 0; i < 5; i++ {
		for {
			fmt.Println(i)
			goto LABEL
		}
	}

	fmt.Println("this is test")

LABEL:
	fmt.Println("it is over")

今天偷懒了,复习一点点,看阅兵去啦~~~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值