zoukankan      html  css  js  c++  java
  • Golang 分支控制和循环

    Golang 分支控制和循环

    if -- else

    go可以在条件中定一个一个变量,然后使用,大括号不能省略

    func main() {
    	if a := 10; a < 10 {
    		fmt.Println(a)
    	}else if a > 10 {
    		fmt.Println(a)
    	}else{
    		fmt.Println(a)
    	}
    }
    
    func main() {
    	var b bool = false
    	if b= true;b { //正确
    		fmt.Println(b)
    	}
    }
    

    switch

    • 匹配项后面不需要添加break, 默认会添加
    • case后的表达式可以有多个,使用逗号间隔
    func main() {
    	 a := 2
    	switch a {
    	case 1,2:
    		fmt.Println(10,20)
    	case 3:
    		fmt.Println(30)
    	}
    }
    
    • case后面的表达式如果是常量值(字面量),则要求不能重复
    • default不是必须的
    func main() {
    	 a := 1
    	switch a {
    	case 1:
    		fmt.Println(10)
    	case 2,3,4: //可以匹配多个表达式
    		fmt.Println(20)
    	default:
    		fmt.Println(30)
    	}
    }
    
    • switch 可以当做if -- else分支来使用
    func main() {
    	a := 1
    	switch {
    	case a > 0:
    		fmt.Println(10)
    	case a < 10:
    		fmt.Println(20)
    	}
    }
    
    • switch后可以直接声明/定义一个变量,分号结束
    func main() {
    	
    	switch a := 1; {
    	case a > 0:
    		fmt.Println(10)
    	case a < 10:
    		fmt.Println(20)
    	}
    }
    
    • switch穿透
    // 1 2
    func main() {
    	a := 1
    	switch a{
    	case 0:
    		fmt.Println(0)
    		fallthrough //默认穿透一层
    	case 1:
    		fmt.Println(1)
    		fallthrough
    	case 2:
    		fmt.Println(2)
    	}
    }
    
    • switch支持type-switch, 判断接口的数据类型
    func main() {
    	var x interface{}
    	var y = 10.0
    	x = y
    	//当作if--else使用没有fallthrough
    	switch i := x.(type) {
    	case nil:
    		fmt.Printf("%T", i)
    	case int:
    		fmt.Printf("%T", i)
    	case float64:
    		fmt.Printf("%T", i)
    	case int64:
    		fmt.Printf("%T", i)
    	}
    }
    

    for

    go中没有whiledo...while, 都使用for替代

    func main() {
    	for i := 0; i < 10; i++ {
    		fmt.Println("hello world")
    	}
    }
    //等价
    func main() {
    	var i int
    	for i = 0; i < 10; i++ {
    		fmt.Println("hello world")
    	}
    }
    //等价
    func main() {
    	i := 0
    	for i < 10 {
    		fmt.Println("hello world")
    		i++
    	}
    }
    ---------------------------
    func main() {
    	//死循环等价于 for ;; {} , while
    	for {
            if(i := 20; i > 10){
                break;
            }
            print("ok")
            i++
    	}
    }
    
    func main() {
    	//等价于do...while
    	for {
            print("ok")
            i++
            if(i := 20; i > 10){
                break;
            }
    	}
    }
    
    

    遍历字符串

    //按照字节遍历,和c#中的字符相同占用一个字节,所以不能遍历带有中文的字符串
    func main() {
    	str := "hello" //和C#相同,底层还是数组
    	for i := 0; i < len(str); i++ {
    		fmt.Printf("%c",str[i])
    	}
    }
    
    //按照字符个数遍历,所以可以遍历带有中文的字串
    func main() {
    	str := "hello"
    	for index,val:=range str{
    		//使用反引号, 转义符不会生效
    		//fmt.Printf(`index = %d,val = %c 
    `,index,val)
    		fmt.Printf("index = %d,val = %c 
    ",index,val)
    	}
    }
    

    goto

    需要配合标签使用

    func main() {
    	label: fmt.Println("hello world")
    	//无限循环
    	for i := 0; i < 10; i++ {
    		if i == 3 {
    			goto label
    		}
    		fmt.Println(i)
    	}
    }
    
  • 相关阅读:
    Codeforces.1051G.Distinctification(线段树合并 并查集)
    BZOJ.4818.[SDOI2017]序列计数(DP 快速幂)
    BZOJ.2159.Crash的文明世界(斯特林数 树形DP)
    Codeforces.1110F.Nearest Leaf(线段树)
    Codeforces.1110E.Magic Stones(思路 差分)
    Yahoo Programming Contest 2019.D.Ears(DP)
    BZOJ.5251.[八省联考2018]劈配mentor(最大流)
    Codeforces Round #538 (Div. 2)
    BZOJ.5249.[九省联考2018]iiidx(贪心 线段树)
    Hello 2019 (D~G)
  • 原文地址:https://www.cnblogs.com/kikochz/p/13446558.html
Copyright © 2011-2022 走看看