The most commonly used flow control in Go language is if and for, while switch and GOTO are mainly structures created to simplify code and reduce repetitive code, which belong to extended class flow control.

if else

if 1<2 {
   fmt.Println(1)}else{
  fmt.Println(2)}// If else can be preceded by an execution statement
if num := 3; num < 3 {
   fmt.Println(1)}else{
  fmt.Println(2)}Copy the code

The for loop

for i:=0; i<10; i++{ fmt.Println(i) }// Infinite loop For loop can be forced to exit by break, goto, return, panic statements.
for{
    fmt.Println(1)}//for range(key-value loop)
// In Go, you can use for range to iterate over groups, slices, strings, maps and channels. The return value traversed through for range has the following pattern:
// Arrays, slicing, strings return indexes and values.
//map returns keys and values.
// Channel returns only the values in the channel

list := []int{0.1.2.3}
for index,value := range list{
  fmt.Println(index,value)
}

Copy the code