Skip to content
Related Articles

Related Articles

Range Keyword in Golang
  • Last Updated : 26 Mar, 2020

In Golang Range keyword is used in different kinds of data structures in order to iterates over elements. The range keyword is mainly used in for loops in order to iterate over all the elements of a map, slice, channel, or an array. When it iterates over the elements of an array and slices then it returns the index of the element in an integer form. And when it iterates over the elements of a map then it returns the key of the subsequent key-value pair. Moreover, range can either returns one value or two values. Lets see what range returns while iterating over different kind of collections in Golang.

  • Array or slice: The first value returned in case of array or slice is index and the second value is element.
  • String: The first value returned in string is index and the second value is rune int.
  • Map: The first value returned in map is key and the second value is the value of the key-value pair in map.
  • Channel: The first value returned in channel is element and the second value is none.

Now, let’s see some examples to illustrate the usage of range keyword in Golang.

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang Program to illustrate the usage
// of range keyword over items of an
// array in Golang
  
package main
  
import "fmt"
  
// main function
func main() {
  
    // Array of odd numbers
    odd := [7]int{1, 3, 5, 7, 9, 11, 13}
  
    // using range keyword with for loop to
    // iterate over the array elements
    for i, item := range odd {
  
        // Prints index and the elements
        fmt.Printf("odd[%d] = %d \n", i, item)
    }
}

chevron_right


Output:

odd[0] = 1
odd[1] = 3
odd[2] = 5
odd[3] = 7
odd[4] = 9
odd[5] = 11
odd[6] = 13

Here, all the elements are printed with their respective index.



Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang Program to illustrate the usage of
// range keyword over string in Golang
  
package main
  
import "fmt"
  
// Constructing main function
func main() {
  
    // taking a string
    var strng = "GeeksforGeeks"
  
    // using range keyword with for loop to
    // iterate over the string
    for i, item := range strng {
  
        // Prints index of all the
        // characters in the string
        fmt.Printf("strng[%d] = %d \n", i, item)
    }
}

chevron_right


Output:

strng[0] = 71 
strng[1] = 101 
strng[2] = 101 
strng[3] = 107 
strng[4] = 115 
strng[5] = 102 
strng[6] = 111 
strng[7] = 114 
strng[8] = 71 
strng[9] = 101 
strng[10] = 101 
strng[11] = 107 
strng[12] = 115

Here, the items printed is the rune, that is int32 ASCII value of the stated characters that forms string.

Example 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang Program to illustrate the usage of
// range keyword over maps in Golang
  
package main
  
import "fmt"
  
// main function
func main() {
  
    // Creating map of student ranks
    student_rank_map := map[string]int{"Nidhi": 3,
                           "Nisha": 2, "Rohit": 1}
  
    // Printing map using keys only
    for student := range student_rank_map {
        fmt.Println("Rank of", student, "is: ",
                     student_rank_map[student])
    }
  
    // Printing maps using key-value pair
    for student, rank := range student_rank_map {
        fmt.Println("Rank of", student, "is: ", rank)
    }
}

chevron_right


Output:

Rank of Nidhi is:  3
Rank of Nisha is:  2
Rank of Rohit is:  1
Rank of Nidhi is:  3
Rank of Nisha is:  2
Rank of Rohit is:  1

So, here at first the output is printed only using key then again output is printed using both key and value.

My Personal Notes arrow_drop_up
Recommended Articles
Page :