流程控制
1.if
package main
import (
"fmt"
)
func testIf(){
ok := true
if ok {
fmt.Println("ok is true")
}
day := "Friday"
if day == "Friday" {
fmt.Println("明天不上班")
} else if day =="Sunday"{
fmt.Println("周末好快")
} else {
fmt.Println("干活啦")
}
m := make(map[string]string)
m["张飞"] = "吃豆芽"
if v,ok := m["张飞"];ok {
fmt.Println(v)
}
}
func main(){
testIf()
}
- switch
默认情况下 case 最后自带 break 语句,匹配成功后就不会执行其他 case,如果我们需要执行后面的 case,可以使用 fallthrough 。
package main
import "fmt"
func testSwitch(){
switch day := 3; day{
case 0,6:
fmt.Println("周末")
case 1,2,3,4,5:
fmt.Println("工作日")
default:
fmt.Println("不合法")
}
a, b :=1,2
switch{
case a<b:
fmt.Println(a<b)
case a>b:
fmt.Println(a>b)
}
}
func main(){
testSwitch()
}
结果:
工作日
true
- for
package main
import (
"fmt"
"time"
)
func testFor(){
//for i :=0; i<10; i++ {
// fmt.Println(i)
//}
for {
time.Sleep(time.Second)
fmt.Println("sleep")
}
}
func main() {
testFor()
}