Go: loops with for and for range

Go: loops with for and for range

In this article we will see how to manage loops in Go.

In this article we will see how to manage loops in Go.

for

The basic syntax of the for construct in Go is as follows:


for initialization; condition; increment {
     // block of code to execute
}

Initialization is performed only once before the start of the loop. It can be used to declare and initialize variables that will be used in the loop.

The condition is a boolean condition that is evaluated at the beginning of each iteration of the loop. If the condition is true, the code block inside the loop is executed. If the condition is false, the loop terminates and execution continues with the code following the loop.

The increment is performed at the end of each iteration of the loop. It is often used to update the value of the variables involved in the loop.

A simple example of using the for construct could be the following:


for i := 0; i < 5; i++ {
     fmt.Println(i)
}

In this case, the loop executes five times. At the beginning of each iteration, the variable i is initialized to 0. The condition i < 5 is evaluated, and if true, the fmt.Println(i) code block is executed. Next, the i variable is incremented by 1 with the i++ statement. This process repeats until the condition becomes false.

Another common form of the for construct in Go is to use a single Boolean expression as a condition:


i := 0
for i < 5 {
     fmt.Println(i)
     i++
}

In this case, the variable i is initialized to 0 before the loop, but the declaration and increment are moved within the loop's code block. The condition i < 5 is evaluated at the beginning of each iteration, and if true, the code block is executed. The increment is performed at the end of each iteration.

The for construct in Go also offers a way to create infinite loops using an omitted boolean condition:


for {
     // block of code to execute
}

In this case, the loop runs forever until it is manually stopped by a break statement within the code block.

for range

The basic syntax of the "for range" construct in Go is as follows:

for index, value := range collection {
     // ...
}

The for keyword is followed by two variables, index and value, separated by an assignment operator :=. The index variable is the index of the current element in the collection, while the value variable is the value of the current element. The range keyword indicates that the "for range" construct is used.

The range keyword can also be used without the index variable if it is not necessary to use the index of the current element. In this case, the syntax is as follows:

for _, value := range collection {
     // ...
}

The _ variable is an anonymous identifier that is used to ignore the index value.

Here are some examples that illustrate the use of the "for range" construct with different types of collections:

Iterating through an array

An array is a collection of elements of the same type. The size of an array is fixed and cannot be changed after it has been created. The syntax for creating an array in Go is as follows:

numbers := [5]int{1, 2, 3, 4, 5}
for index, value := range numbers {
     fmt.Printf("Index: %d, Value: %d\n", index, value)
}

The above code creates an array of 5 elements and iterates through it using the "for range" construct.

Iterating through a slice

A slice is a dynamic data structure that can be used to store a collection of elements of the same type. The size of a slice is dynamic and can be changed after its creation. The syntax for creating a slice in Go is as follows:

numbers := []int{1, 2, 3, 4, 5}
for index, value := range numbers {
     fmt.Printf("Index: %d, Value: %d\n", index, value)
}

The above code creates a slice of 5 elements and iterates through it using the "for range" construct.

Iterating through a map

A map is a dynamic data structure that can be useful designed to store a collection of key-value pairs. The size of a map is dynamic and can be changed after its creation. The syntax for creating a map in Go is as follows:

numbers := map[string]int{
     "one": 1,
     "two": 2,
     "three": 3,
     "four": 4,
     "five": 5,
}
for key, value := range numbers {
     fmt.Printf("Key: %s, Value: %d\n", key, value)
}

The above code creates a map of 5 key-value pairs and iterates through it using the "for range" construct.

Iterate through a string

A string is a sequence of characters. The size of a string is dynamic and can be changed after it is created. The syntax for iterating through a string in Go is as follows:

text := "Hello, World!"
for index, value := range text {
     fmt.Printf("Index: %d, Value: %c\n", index, value)
}