How to use for and foreach loop in Golang?


Golang is a powerful programming language that offers various loop structures for iteration, including the traditional for loop and the foreach loop. These loops are essential tools in any programmer's toolbox, and they help to simplify repetitive tasks. In this article, we will discuss how to use for and foreach loops in Golang and provide some practical examples.

Understanding the For Loop in Golang

The for loop is one of the most commonly used loop structures in Golang. It is used for iterating over a range of values, such as an array or a slice. The basic syntax of the for loop is as follows −

for initialization; condition; increment {
   // statement(s)
}

In this syntax, initialization is the initial value of the loop variable, condition is the expression that is evaluated before each iteration of the loop, and increment is the statement that is executed at the end of each iteration. The following example demonstrates how to use the for loop to iterate over an array of integers −

Example

package main

import "fmt"

func main() {
   numbers := [5]int{1, 2, 3, 4, 5}

   for i := 0; i < len(numbers); i++ {
      fmt.Println(numbers[i])
   }
}

Output

1
2
3
4
5

Understanding the Foreach Loop in Golang

The foreach loop, also known as the range loop, is another loop structure available in Golang. It is used for iterating over a range of values, such as an array, slice, or map. The basic syntax of the foreach loop is as follows −

for index, value := range array {
   // statement(s)
}

In this syntax, index is the index of the current element, value is the value of the current element, and array is the array or slice being iterated over. The following example demonstrates how to use the foreach loop to iterate over an array of strings −

Example

package main

import "fmt"

func main() {
   fruits := [3]string{"apple", "banana", "cherry"}
   
   for _, fruit := range fruits {
      fmt.Println(fruit)
   }
}

Output

apple
banana
cherry

Choosing Between For and Foreach Loops

When choosing between the for loop and the foreach loop in Golang, it is important to consider the type of data structure being iterated over. The for loop is typically used for iterating over arrays, whereas the foreach loop is used for iterating over slices and maps. The foreach loop is also preferred when the index of the current element is not needed. In general, the foreach loop is more concise and easier to read than the for loop.

Conclusion

In conclusion, the for loop and the foreach loop are essential tools in Golang that simplify repetitive tasks. The for loop is used for iterating over a range of values, such as an array, whereas the foreach loop is used for iterating over slices and maps. By understanding how to use these loop structures in Golang, you can write more efficient and concise code.

Updated on: 25-Apr-2023

309 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements