一、流程控制语法
if condition_1{
}else if condition_2{
}else if condition_3{
}else{
}
二、switch分支
package main
import "fmt"
func main(){
var a int = 10
// go语言中不需写break防止case穿透问题
switch a {
case 0:
fmt.Println("a is equal 0")
case 10:
fmt.Println("a is equal 10")
default:
fmt.Println("a is equal default")
}
}
package main
import(
"fmt"
"math/rand"
)
func main(){
var n int
// 随机生成一个整数
n = rand.Intn(100)
for{
var input int
// 输入
fmt.Scanf("%d\n", &input)
flag := false
switch{
case input == n:
fmt.Println("you are right")
flag = true
case input > n:
fmt.Println("bigger")
case input < n:
fmt.Println("less")
}
if flag{
break
}
}
}