1. 条件控制
a := 3 if a > 5 { fmt.Println("big") } else { fmt.Println("small") }
2. 选择语句
a := 3 switch a { case 0: fmt.Println(0) case 3: fmt.Println(3) }
3.循环语句
package main import "fmt" func main() { sum := 1 for i := 1; i < 3; i++ { sum = sum * i fmt.Println(i) } fmt.Println(sum) }
package main func main() { // 无限循环 sum := 1 for { sum ++ if sum >= 100{ break } } }
循环条件多重赋值
package main func main() { arr := [5]int{1,2,3,4,5} for i, j := 0, len(arr)-1; i < j; i,j = i + 1, j - 1{ arr[i], arr[j] = arr[j], arr[i] } }
循环支持break和continue
package main import ( "fmt" ) func main() { loop1: for i:=0;i<10 ;i++ { for j:=0;j<10 ;j++ { if j > 5 { break loop1 } fmt.Println(i, j) } } }