{

Golang range loop examples | For each loop tutorials


In this blog post, We are going to learn for each loop with a range in go language with example

golang  for each loop in golang to iterate array,strings,unicode chars,maps using golan range

Golang range loop

the foreach loop is a for loop with a range keyword. It uses to iterate each element from Array, Slice, Map, and Strings. It is always useful to iterate the elements of a data structure without getting the length of data structure Syntax of the for with range loop is as follows.

for key, value := range collection{  
// body  
}

During an iteration of the collection, each iteration contains a key or index, value or element at index.
Few key points for loop with range keywords

  • This is used to iterate elements of collections types
  • Each iteration data contains two parts, One is key or index, the second is a value or an element at index
  • The iterated elements order is not ordered or preserved in a collections
  • use the key as _ for retrieving values during array iteration
  • for array and slice, the first element is an index, the second value is an element at the index position for each iteration
  • if the array or slice is empty, It returns nothing

For each array range iteration example

Below is an example for iterating the array of numbers

package main  
  
import "fmt"  
  
func main() {  
    // Iterate numeric array  
    numbers := [3]int{6, 9, 3}  
    for key, value := range numbers {  
        fmt.Printf("%d = %d\n", key, value)  
    }  
}  

Output is

0 = 6  
1 = 9  
2 = 3  

For array iteration, the First variable for iteration value can be passed _

func main() {  
    // Iterate numeric array  
    numbers := [3]int{6, 9, 3}  
    for _, value := range numbers {  
        fmt.Printf("%d ", value)  
    }  
}  

Output is

6 9 3  

another way is using the index parameter of iteration value to traverse the elements of the array

func main() {  
    // Iterate numeric array  
    numbers := [3]int{6, 9, 3}  
    for i := range numbers {  
        fmt.Printf("%d ", numbers[i])  
    }  
}  

Output is

6 9 3  

Iteration examples with range keyword

The below example covers the following things

  • Iterate elements in Numeric Array
  • Loop elements in String Array
  • Iterate elements in Unicode characters of a string
  • Iterate elements in Map
package main  
  
import "fmt"  
  
func main() {  
  
    // Create Numeric array  
    numbers := [3]int{6, 9, 3}  
    // Iterate numeric array  
    for key, value := range numbers {  
        fmt.Printf("%d = %d\n", key, value)  
    }  
    // Create String array  
    strs := [3]string{"one", "two", "three"}  
    // Iterate String array  
    for key, value := range strs {  
        fmt.Printf("%d = %s\n", key, value)  
    }  
  
    /* create a map*/  
    emps := map[string]string{"1": "John", "2": "Franc", "3": "Kiran"}  
  
    /* Iterate map using keys*/  
    for emp := range emps {  
        fmt.Println("Emp Id: ", emp, " Name: ", emps[emp])  
    }  
    /* Iterate Unicode charactes of a String*/  
    for index, character := range "本語日" {  
        fmt.Printf("%#U  position %d\n", character, index)  
    }  
  
}  

The output of the above program code is

0 = 6  
1 = 9  
2 = 3  
0 = one  
1 = two  
2 = three  
Emp Id:  1  Name:  John  
Emp Id:  2  Name:  Franc  
Emp Id:  3  Name:  Kiran  
U+672C '本'  position 0  
U+8A9E '語'  position 3  
U+65E5 '日'  position 6  
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.