Range Loop in Golang

Learn via video courses
Topics Covered

Overview

The range keyword in Golang is used with various data structures for iterating over an element. It is most commonly used in loops for iterating over elements of an array, map, slice, etc. In this article, we will learn all about a range keyword in Golang.

Introduction

A for-range loop in Golang is used for iterating over elements in various data structures like an array, slice, map, or even a string, etc. The range keyword is used for iterating over an expression and to evaluate data structures like slice, array, map, channel, etc.

arious-data-structure-where-we-use-for-range-loop

The Syntax of the For-Range Loop is as Follows:

Where, Index is the value that is been accessed. value is the actual value that we have in each of the iteration. datastructure is used for holding the values that is accessed by the loop.

Range Keyword

  • The range keyword is used for iterating over elements in any data structure. While iterating over an array or slice, it returns an index of an element in an int format. Similarly when it is iterated over a map then it returns the key and value. The range can however return multiple values as an output. To understand this, let’s look at how the range works differently for different data types.
  • Using range on an array or slice- in this, the first value that it returns at the output is an index, and the second value that it returns is the element.
  • Using range on the string- in the string, the first value that it returns is the index and the second value is rune int.
  • Using the range on the map- the first value that is returned by the map is the key and the second value returns the value from the key-value pair.
  • Using the range on the channel- the first value that it returns is an element and the second value returned is none.

Iterate on A String Using Range

Program to use the for-range loop on a string for accessing characters.

Output:

Explanation: the for-range loop starts from the starting character which is H to the last character which is o and prints each of the character.

Iterate on Array and Slice Using Range

Program to use range with an array

Output:

Explanation: In this example, the for range iterates over an array and prints each of the elements along with its index. The range keyword returns two values here, one is the index of the element, and the other is the corresponding element itself.

Iterate on The Map Using Range

Program to use for-range on a map to access individual key-pair

Output:

Explanation: In this example, the for-range is used on a map for printing the key-value pair of the map. The map contains the key-value of type string and int which is accessed by using for-range.

Iterate on Channel Using Range

Program to iterate over a channel until its close

Output:

Explanation: In this example, a function is made for a channel, and elements are inserted. The for loop with range is used for accessing the elements of that channel and printing its values as the output.

Conclusion

  • The range keyword in Golang is used with various data structures for iterating over an element. It is mostly used in loops for iterating over elements of an array, map, slice, etc.
  • The syntax of the for-range loop is as follows:
    • Index is the value that is been accessed.
    • value is the actual value that we have in each iteration.
    • data structure is used for holding the values that are accessed by the loop.
  • The range can however return multiple values as an output.