Cycles in Golang

Loops allow you to perform certain actions multiple times, depending on a certain condition. There is only one loop in Go the for loop, which can take many forms. This cycle has the following formal definition:

for [counter initialization]; [condition]; [Counter Change] {
      actions to do
}

Define a Loop

For example, let's use a loop to display hello 10 times:

package main

import "fmt"

func main() {
    for msg: = 1;
    msg < 10;
    msg++{
        fmt.Println(Message = "hello")
    }
}

The for-loop declaration is broken down into three parts. First, the counter is initialized (msg:= 1):. It represents Declaring a variable to be used inside the loop. In this case, it's counter i with an initial value of 1.
The second part represents the condition (msg < 10):. As long as this condition is true, that is, it returns true, the loop will continue.

The third part represents the change (increase) of the counter by one.

In the body of the loop, the string hello is displayed on the console.

In this way, the loop will run 9 times until the value of i is 10. And each time this value will increase by 1. Each pass of the cycle is called an Iteration. That is, in this case, there will be 9 iterations. As a result, we get the following console output:

hello Number 1
hello Number 2
hello Number 3
hello Number 4
hello Number 5
hello Number 6
hello Number 7
hello Number 8
hello Number 9

We don't have to specify all the conditions when declaring a loop. For example, you can push the declaration of a variable outward:

var x = 1;

for ; x <10; x++ {
  fmt.Println(x)
}

You can remove the counter change to the loop body itself and leave only the following condition:

var x = 1;

for;
x < 10; {
    fmt.Println(x)
    x++
}

If the loop uses only a condition, you can shorten it as follows:

var x = 1;

for x < 10 {
    fmt.Println(x)
    x++
}

Nested loops

Loops can be nested, that is, they can be located inside other loops. For example, let's display the multiplication table on the console:

package main

import "fmt"

func main() {
    for x := 1; x < 10; x++ {
        for j := 1; j < 10; j++ {
             fmt.Print("x =", x, " j =", j, "\t")
        }
        fmt.Println()
    }
}

the output should be like this:

x =1 j =1	x =1 j =2	x =1 j =3	x =1 j =4	x =1 j =5	x =1 j =6	x =1 j =7	x =1 j =8	x =1 j =9	
x =2 j =1	x =2 j =2	x =2 j =3	x =2 j =4	x =2 j =5	x =2 j =6	x =2 j =7	x =2 j =8	x =2 j =9	
x =3 j =1	x =3 j =2	x =3 j =3	x =3 j =4	x =3 j =5	x =3 j =6	x =3 j =7	x =3 j =8	x =3 j =9	
x =4 j =1	x =4 j =2	x =4 j =3	x =4 j =4	x =4 j =5	x =4 j =6	x =4 j =7	x =4 j =8	x =4 j =9	
x =5 j =1	x =5 j =2	x =5 j =3	x =5 j =4	x =5 j =5	x =5 j =6	x =5 j =7	x =5 j =8	x =5 j =9	
x =6 j =1	x =6 j =2	x =6 j =3	x =6 j =4	x =6 j =5	x =6 j =6	x =6 j =7	x =6 j =8	x =6 j =9	
x =7 j =1	x =7 j =2	x =7 j =3	x =7 j =4	x =7 j =5	x =7 j =6	x =7 j =7	x =7 j =8	x =7 j =9	
x =8 j =1	x =8 j =2	x =8 j =3	x =8 j =4	x =8 j =5	x =8 j =6	x =8 j =7	x =8 j =8	x =8 j =9	
x =9 j =1	x =9 j =2	x =9 j =3	x =9 j =4	x =9 j =5	x =9 j =6	x =9 j =7	x =9 j =8	x =9 j =9	

Iterating over arrays

To iterate over arrays, you can use the following form of a for loop:

for index, value: = range arrayName {
    Actions
}

By iterating, we can separately get the index of the element in the array and the value of this element. For example, let's iterate over an array of strings:

var books = [4] string {
    "Docker", "Kubernetes", "Linux", "Git"
}

for index, value: = range books {
    fmt.Println(index, value)
}

The output will be this:

0 Docker
1 Kubernetes
2 Linux
3 Git

If we don't plan to use element values or indexes, we can specify a dash instead. For example, we don't need indexes:

for _, value: = range books {
    fmt.Println(value)
}