zoukankan      html  css  js  c++  java
  • Go if-else语句、for循环、switch语句

    Go if-else语句

    package main
    
    import "fmt"
    
    func main() {
        a := 10
    
        //1 基本使用
        //if a>10{
        //    fmt.Println("大于10")
        //}else {
        //    fmt.Println("小于等于10")
        //}
    
        //2 if -else
        //if a > 10 {
        //    fmt.Println("大于10")
        //} else if a == 10 {
        //    fmt.Println("等于10")
        //} else {
        //    fmt.Println("小于10")
        //}
    
    
        //3 注意:不能换行(go语言每一行结尾,需要加一个;  ,每当换行,会自动加;)以下是错误示范
        //if a>10{
        //    fmt.Println("大于10")
        //}else if a==10
        //{
        //    fmt.Println("等于10")
        //}else {
        //    fmt.Println("小于10")
        //}
    
    
        //4 条件里可以进行初始化操作(有作用域范围的区别)
        if a:=10;a<10{
            fmt.Println("xxx")
        }else{
            fmt.Println("yyy")
        }
    
        fmt.Println(a)
    }

    Go for循环

    // 循环(Go中只有for循环,没有while,do while)
    package main
    
    import "fmt"
    
    func main() {
        // 1 基本语法
        // for初始化;条件判断;自增/自减{ 循环体代码 }  三部分都可以省略
        //打印 0--9
        for i:=0;i<10;i++ {
            fmt.Println(i)
        }
    
        // 2 省略第一部分(初始化),作用域范围不一样
        //i :=0
        //for ;i<10;i++ {
        //    fmt.Println(i)
        //}
    
        // 3 省略第三部分
        //for i:=0;i<10; {
        //    i++
        //    fmt.Println(i)
        //}
    
        // 4 省略第一部分和第三部分
        //i := 0
        //for ; i<10; {
        //    i++
        //    fmt.Println(i)
        //}
    
        // 5 for 条件 {}
        //i :=0
        //for i <10 {
        //    i++
        //    fmt.Println(i)
        //}
    
        // 6 死循环
        //for {
        //    fmt.Println("我是死循环")
        //}
    
        // 7 breakcontinue
        //for {
        //    fmt.Println("我是死循环")
        //    break
        //    //continue
        //}
    }

    Go switch语句

    package main
    
    func main() {
        // 1 switch 基本使用
        //a := 10
        //switch a {
        //case 1:
        //    fmt.Println("1")
        //case 2:
        //    fmt.Println("2")
        //case 9:
        //    fmt.Println("9")
        //case 10:
        //    fmt.Println("10")
        //}
    
        // 2 default
        //a := 15
        //switch a {
        //case 1:
        //    fmt.Println("1")
        //case 2:
        //    fmt.Println("2")
        //case 9:
        //    fmt.Println("9")
        //case 10:
        //    fmt.Println("10")
        //default: // 以上条件都不符合
        //    fmt.Println("不知道")
        //}
    
        // 3 多条件
        //a := 16
        //switch a {
        //case 1,2,3:
        //    fmt.Println("1")
        //case 4,5,6:
        //    fmt.Println("2")
        //case 7,9:
        //    fmt.Println("9")
        //case 10,16:
        //    fmt.Println("10")
        //default:
        //    fmt.Println("不知道")
        //}
    
        // 4 无表达式
        //a := 3
        //switch  {
        //case a==1 || a==3:    // ||代表or
        //    fmt.Println("1")
        //case a==4 || a==5:
        //    fmt.Println("2")
        //default:
        //    fmt.Println("不知道")
        //}
    
        // 5 fallthrough,无条件执行下一个case
        //a := 3
        //switch  {
        //case a==1 || a==3:
        //    fmt.Println("1")
        //    fallthrough // fallthrough 会无条件执行下一个case
        //case a==4 || a==5:
        //    fmt.Println("2")
        //default:
        //    fmt.Println("不知道")
        //}
    }
  • 相关阅读:
    闪回flashback
    Oracle数据文件在open状态被删除的恢复记录
    从浅到深掌握Oracle的锁
    Oracle 11g 11201_RHEL5.5_RAC_VBOX 详细搭建步骤
    AWR Report 关键参数详细分析
    16、Xtrabackup备份与恢复
    17、percona-toolkit
    插入排序
    选择排序
    冒泡排序
  • 原文地址:https://www.cnblogs.com/baohanblog/p/12747651.html
Copyright © 2011-2022 走看看