Go程序主要由以下几部分组成:(具体可以参考2选择结构中的实例)
- *包声明
- *导入包
- *函数
- *变量
- *语句和表达式
- *注释
流程控制
1.顺序结构
       
2.选择结构 
(1)if else
| if和else分支结构在 Go 中当然是直接了当的了。
 |  | 
| 包声明 | package main
 | 
| 导入fmt包 | import "fmt"
 | 
| main函数 | func main() {
 | 
| 这里是一个基本的例子。 |     if 7%2 == 0 {
        fmt.Println("7 is even")
    } else {
        fmt.Println("7 is odd")
    }
 | 
| 你可以不要 else只用if语句。 |     if 8%4 == 0 {
        fmt.Println("8 is divisible by 4")
    }
 | 
| 在条件语句之前可以有一个语句;任何在这里声明的变量都可以在所有的条件分支中使用。 |     if num := 9; num < 0 {
        fmt.Println(num, "is negative")
    } else if num < 10 {
        fmt.Println(num, "has 1 digit")
    } else {
        fmt.Println(num, "has multiple digits")
    }
}
 | 
| 注意,在 Go 中,你可以不使用圆括号,但是花括号是需要的。 |  | 
|  | $ go run if-else.go 
7 is odd
8 is divisible by 4
9 has 1 digit
 | 
| Go 里没有三目运算符(?:),所以即使你只需要基本的条件判断,你仍需要使用完整的 if语句。 | 
(2)switch 
| switch ,方便的条件分支语句。 |  | 
|  | package main
 | 
|  | import "fmt"
import "time"
 | 
|  | func main() {
 | 
| 一个基本的 switch。 |     i := 2
    fmt.Print("write ", i, " as ")
    switch i {
    case 1:
        fmt.Println("one")
    case 2:
        fmt.Println("two")
    case 3:
        fmt.Println("three")
    }
 | 
| 在一个 case语句中,你可以使用逗号来分隔多个表达式。在这个例子中,我们很好的使用了可选的default分支。 |     switch time.Now().Weekday() {
    case time.Saturday, time.Sunday:
        fmt.Println("it's the weekend")
    default:
        fmt.Println("it's a weekday")
    }
 | 
| 不带表达式的 switch是实现 if/else 逻辑的另一种方式。这里展示了case表达式是如何使用非常量的。 |     t := time.Now()
    switch {
    case t.Hour() < 12:
        fmt.Println("it's before noon")
    default:
        fmt.Println("it's after noon")
    }
}
 | 
|  | $ go run switch.go 
write 2 as two
it's the weekend
it's before noon
 | 
3.循环结构
  (1)for
| for是 Go 中唯一的循环结构。这里有for循环的三个基本使用方式。
 |  | 
|  | package main
 | 
|  | import "fmt"
 | 
|  | func main() {
 | 
| 最常用的方式,带单个循环条件。 |     i := 1
    for i <= 3 {
        fmt.Println(i)
        i = i + 1
    }
 | 
| 经典的初始化/条件/后续形式 for循环。 |     for j := 7; j <= 9; j++ {
        fmt.Println(j)
    }
 | 
| 不带条件的 for循环将一直执行,直到在循环体内使用了break或者return来跳出循环。 |     for {
        fmt.Println("loop")
        break
    }
}
 | 
|  | $ go run for.go
1
2
3
7
8
9
loop
 | 
| 在教程后面,当我们学到 rang语句,channels,以及其他数据结构时,将会看到一些for的其它使用形式, |