go基础学习(5)流程控制

流程控制

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()
}
  1. 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
  1. 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()
}
发布了48 篇原创文章 · 获赞 0 · 访问量 762

猜你喜欢

转载自blog.csdn.net/qq_36710311/article/details/104550264
今日推荐