How to iterate over an Array using for loop in Golang? Last Updated : 05 May, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report An array is a data structure of the collection of items of the similar type stored in contiguous locations. For performing operations on arrays, the need arises to iterate through it. A for loop is used to iterate over data structures in programming languages. It can be used here in the following ways: Example 1: C // Golang program to iterate over // an Array using for loop package main import "fmt" func main() { // taking an array arr := [5]int{1, 2, 3, 4, 5} fmt.Println("The elements of the array are: ") // using for loop for i := 0; i < len(arr); i++ { fmt.Println(arr[i]) } } Output: The elements of the array are: 1 2 3 4 5 Explanation: The variable i is initialized as 0 and is defined to increase at every iteration until it reaches the value of the length of the array. Then the print command is given to print the elements at each index of the array one by one. Example 2: The for loopcan use another keyword return to perform iterations. C // Golang program to iterate over // an Array using for loop package main import "fmt" func main() { // taking an array arr := [5]string{"Ronaldo", "Messi", "Kaka", "James", "Casillas"} fmt.Println("The elements of the array are:") // using for loop for index, element := range arr { fmt.Println("At index", index, "value is", element) } } Output: The elements of the array are: At index 0 value is Ronaldo At index 1 value is Messi At index 2 value is Kaka At index 3 value is James At index 4 value is Casillas Explanation: Keyword range sets the scope of iteration upto the length of arr. The variables index and element store then indexes and values of the array respectively. Example 3: When we don’t need the index, we can ignore it with the blank identifier _. C // Golang program to iterate over // an Array using for loop package main import "fmt" func main() { // taking an array arr := []int{1, 2, 3, 4, 5} fmt.Println("The elements of the array are:") // using for loop for _, value := range arr { fmt.Println(value) } } Output: The elements of the array are: 1 2 3 4 5 Comment More infoAdvertise with us Next Article How to use for and foreach loop in Golang? P prakhar7 Follow Improve Article Tags : Go Language Golang-Arrays Similar Reads How to use for and foreach loop in Golang? There is only one looping construct in Golang, and that is the for loop. The for loop in Golang has three components, which must be separated by semicolons (;), those are: The initialization statement: which is executed before the first iteration. e.g. i := 0The condition expression: which is execu 3 min read How to use Array Reverse Sort Functions for Integer and Strings in Golang? Go language provides inbuilt support implementation of basic constants and run-time reflection to operate sort package. Golang is the ability for functions to run independently of each other. With the help of this function we can easily sort integer and string by importing "sort" package. Basically, 1 min read How to pass an Array to a Function in Golang? In Go, arrays are used to store a fixed-length collection of data of the same type. To manage this data effectively, you may often need to pass arrays to functions. In this article we will learn "How to pass an Array to a Function in Golang".Example:Gopackage main import "fmt" // Function to calcula 2 min read How to Calculate the Average using Arrays in Golang? Given an array of n elements, your task is to find out the average of the array.Approach: Accept the size of the array.Accept the elements of the array.Store the sum of the elements using for loop.Calculate Average = (sum/size of array)Print the average. Example: Input: n = 4 array = 1, 2, 3, 4 Ou 2 min read How to Copy an Array into Another Array in Golang? In Go, an array is a fixed-length sequence that holds elements of a specific type. Unlike slices, arrays have a constant size, which is determined when the array is declared. Copying one array to another is straightforward but requires that both arrays have the same length and type.Examplepackage ma 3 min read How to copy a map to another map in Golang? Maps in Golang is a collection of unordered pairs of key-value. It is widely used because it provides fast lookups and values that can retrieve, update, or delete with the help of keys. In Map, you can copy a map to another map using the for loop provided by the Go language. In for loop, we fetch th 2 min read Like