Go Language 3-Program control

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

The program is defined as a data structure + algorithm, and the implementation of the algorithm is usually done by program control, therefore, the program control is a very important part. Next, let's take a look at the program control that go provides for us in this article.

1. Conditional statements

 关键字:if、else、else if
 if  I: = 0 ; x = = i {//comment 1  return   0 } else  if  x == 1  {//comment 2  return   1 } else  {return  -1 }  
     注释1:这里可以自定义局部变量,作用域为这个if-else体结束。     注释2:if块不定义局部变量时与这种写法类似,如果这里也定义局部变量,则注释1处的           if语句块无法引用到这里定义的变量,else语句块可以引用。
 切记:不需要()来包围条件语句,{}为必须的,且必须与关键字同行

2. SELECT statement

 关键字:switch、case、fallthrough
 switch  I {     //Note 1  case   0 : //comment 2  fmt. Printf ( "0" ) case   1<     /span>: //comment 3  fallthrough  case   2 ,  3 : //comment 4< /span> FMT. Printf ( "2,3,4" ) default : //Note 5  fmt. Printf ( "Default" )}  
     注释1:{须与switch同行,这里可以有一个初始化表达式,右侧需要跟分号     注释2:不需要明确的break来退出,默认自动退出     注释3:当i=1时输出2,3,fallthrough关键字会继续执行紧跟的下一个case代码     注释4:可以一个case中写多个满足条件(i为2,3中的一个即可)     注释5:以上的都不匹配时执行
 switch后面的表达式不是必需的,这种结构与多个if...else if的逻辑作用等同
     switch {                   //注释1     case 0 == i:          fmt.Printf("0")     case 1 == i:          fallthrough     case 2 == i || 3 == i:          fmt.Printf("2,3,4")     default:          fmt.Printf("Default")     }
     注释1:{这里可以有一个初始化表达式,右侧需要跟分号,如本行可写为switch i := 0; {

3. Circular statements

 关键字:for、range、break、continue 1)for语句的多种格式 格式1:
     sum := 0     for i := 0; i < 10//注释1          sum += i     }
     注释1:不需要()来包围条件语句,{}为必须有的,且{必须与关键字同行
 格式2:
     i, sum := 0, 0     for {                    //注释1          if i == 10 {               break          }          sum += i          i++     }
     注释1:类似于Java中的while(true)写法
 格式3:
     i, sum := 0, 0     for i < 10 {          sum += i          i++     }
 2)数组或切片(后面讲解)的两种遍历方式 写法1:
     mySlice := []int{1, 2, 3, 4}              //注释1     sum := 0                                       for i, l := 0len//注释2          sum += mySlice[i]     }
     注释1:定义一个slice     注释2:赋值语句支持多重赋值(仅支持平行多重赋值)
 写法2:
     forrange mySlice {               //注释1          sum += v                             //注释2          //sum +=mySlice[i]     }
    注释1:range有两个返回值,i为索引,v为值。当对map(后面讲解)进行遍历时,range的返回值分别为key,value    注释2:这里要注意i没有使用,编译错误,请使用_代替

4. Jump Statement

 关键字:goto,其实break及continue也有跳转的功能 三个语法都可以配合标签使用,标签区分大小写 goto     标签名:调整程序执行位置,标签可以在语句之后定义 break    标签名:结束与标签同级的for循环,标签必须在语句之前定义 continue 标签名:结束与标签同级的for循环,标签必须在语句之前定义
     Label1:                             //定义名为"Label1"的标签          for {                          //无限循环               for i := 0; i < 10; i++ {                      if i > 3 {                         break Label1    //直接结束Label1同级的for                    }               }          }
 goto、continue的使用大体类似

All the way, we found that go is actually a very simple language, there are other programming language basic students read these several blog comprehend by analogy understand, just the difference in writing, do not worry, the following article will introduce some of the characteristics of go more unique.

No smell (Unknow) "Go Programming Basics"
Xu Xiwei's "Go Language Programming"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.