golang语句笔记

来源:互联网 发布:美瞳推荐知乎 编辑:程序博客网 时间:2024/06/03 19:28

GO语言语句

switch语句

1. var grade string = "B"    var marks int = 90    switch marks {    case 90:grade = "A"    case 80:grade = "B"    case 50,60,70:grade = "C"    default:        grade = "D"    }    switch {    case grade == "A":        fmt.Println("优秀")    case grade == "B",grade == "C":        fmt.Println("良好")    case grade == "D":        fmt.Println("及格")    case grade == "F":        fmt.Println("不及格")    default:        fmt.Println("差")    }    fmt.Println("你的等级是:",grade)2.     func main()  {       a := 1       switch a {       case a>=0:        fmt.Println("a=0")            fallthrough //满足这个条件之后继续向下寻找       case a>=1:        fmt.Println("a=1")       default:        fmt.Println("None")       }      }

for 语句

for init;condition;post{}  //init:一般为赋值表达式,给控制变量赋初值,condition:关系表达式或逻辑表达式,循环控制条件for condition{}  // 和C的while一样,for{}  //和C语言的(;;)一样for循环的range格式可以对slice,map,数组,字符串等进行迭代循环,格式如下:for key,value := range oldMap{    newMap[key] = value}var b int = 15    var a int    numbers := [6] int {1,2,3,5}    for a:=0;a<10;a++{        fmt.Printf("a的值为:%d\n",a)    }    fmt.Println("-----------------------------------------")    for a<b{        a++        fmt.Printf("a的值为:%d\n",a)    }    fmt.Println("-----------------------------------------")    for i,x := range numbers{        fmt.Printf("第%d位x的值是%d\n",i,x)    }
  1. for,goto,break

    func main(){    LABEL1:        for i:=0;i<10;i++{            if i>3{                goto LABEL1 //  跳到LABEL标签所在的位置                break LABEL1 //  跳出当前循环至LABEL那一级,            }        }}
原创粉丝点击