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)
    	}
    }
    
  • 相关阅读:
    IOS编程之通讯录
    和iPhone有关的视图控制器:UIViewController、UITabBarController、UINavigationController及其混合用法
    跟大家分享下今天所学到的PHP,虽然很基础,但是感觉也很重要
    直接拿来用!最火的iOS开源项目
    ios各种手势,很有意思
    IOS中Json解析的四种方法
    [iOS]深入浅出 iOS 之多线程 NSThread
    IOS应用程序升级
    ios学习笔记之block在ios开发中的应用
    【热门收藏】iOS开发人员必看的精品资料(100个)——下载目录
  • 原文地址:https://www.cnblogs.com/kikochz/p/13446558.html
Copyright © 2011-2022 走看看