Go switch语句

// switch 是一个条件语句,用于将表达式的值与可能匹配的选项列表进行比较,并根据匹配情况执行相应的代码块。它可以被认为是替代多个 if else 子句的常用方式


package main

func main() {
    //1 基本使用
    //num:=4
    //switch num {
    //case 1:
    //    fmt.Println("1")
    //case 2:
    //    fmt.Println("2")
    //case 3:
    //    fmt.Println("3")
    //case 4:
    //    fmt.Println("4")
    //}

    //2 默认情况
    //num:=40
    //switch num {
    //case 1:
    //    fmt.Println("1")
    //case 2:
    //    fmt.Println("2")
    //case 3:
    //    fmt.Println("3")
    //case 4:
    //    fmt.Println("4")
    //default:
    //    fmt.Println("我没有匹配")
    //}

    //3 多表达式判断
    //num:=40
    //switch num {
    //case 1,2,3,4,5,6,7,8:
    //    fmt.Println("1")
    //case 10,11,16:
    //    fmt.Println("2")
    //case 30:
    //    fmt.Println("3")
    //case 40,44,45:
    //    fmt.Println("4")
    //default:
    //    fmt.Println("我没有匹配")
    //}

    //4 无表达式的 switch
    //num:=80
    //switch  {
    //case num==12,num==13:
    //    fmt.Println("12,13")
    //case num==40,num==41:
    //    fmt.Println("40,41")
    //default:
    //    fmt.Println("我没有匹配")
    //}


    //5 Fallthrough
    //num:=12
    //switch  {
    //case num==12,num==13:
    //    fmt.Println("12,13")
    //    fallthrough
    //case num==40,num==41:
    //    fmt.Println("40,41")
    //    //fallthrough  //穿透,只要看到fallthrough,无条件执行下一个case或者default
    //default:
    //    fmt.Println("我没有匹配")
    //}
}


goto:饱受诟病  java 保留字,没有实际作用
原文地址:https://www.cnblogs.com/ZhZhang12138/p/14886715.html