Golang Tutorial Index


The Go programming language offers a variety of features, making it a versatile and efficient tool for developers. One such feature is the range keyword, which is used to loop through elements in arrays, slices, maps, and other data structures. This tutorial will explore the range keyword, how it works, and how it can be used to iterate over elements in Go efficiently.



What is the Range Keyword in Go?

The range keyword in Go is a language construct that allows developers to iterate over the elements of an array, slice, map, or other data structure. It is used in conjunction with the for loop, which makes it possible to loop through each element in the data structure.

When it iterates over the array elements or the slice elements, it returns the index of those elements in the integer format. And when the range iterates over the elements of a Golang map, it returns the key of the key-value pair. In addition, the range can return either one value or more than one value. For example, Let us see what the range returns while iterating over various data structures in the Go programming language.

An example of using the range keyword to iterate over the elements of an array that prints all odd numbers in the Go array:

Example:

package main
import "fmt"

func main() {
   odds := [10]int{1, 3, 5, 7, 9, 11, 13, 15, 17, 19}
   for i, v := range odds {
      fmt.Printf("odds[%d] = %d \n", i, v)
  }
}

Output:

odds[0] = 1
odds[1] = 3
odds[2] = 5
odds[3] = 7
odds[4] = 9
odds[5] = 11
odds[6] = 13
odds[7] = 15
odds[8] = 17
odds[9] = 19

Another example of using range with strings that print the int32 ASCII value of the given characters:

Example:

package main
import "fmt"

func main() {
    var str = "Hello Go"
   for i, v := range str {
      fmt.Printf("length_of_String[%d] = %d \n", i, v)
  }
}

Output:

length_of_String[0] = 72
length_of_String[1] = 101
length_of_String[2] = 108
length_of_String[3] = 108
length_of_String[4] = 111
length_of_String[5] = 32
length_of_String[6] = 71
length_of_String[7] = 111

How the Range Keyword Works

The range keyword works by taking the data structure to be iterated over as an argument and returning a pair of values for each iteration. The first value is the element's index, and the second is the element's value. These values can then be used in the for loop to process the elements of the data structure.

Syntax:

for i, v := range data_structure {
    // code to process element v at index i
}

Use Cases for the Range Keyword

The range keyword can be used in various situations where it is necessary to process data structure elements. For example, it can be used to calculate the sum of an array of numbers, to find the maximum or minimum value in a list, or to count the occurrences of a particular element.

Here is an example of using the range keyword to calculate the sum of an array of numbers:

Example:

package main
import "fmt"

func main() {
    numbers := []int{1, 2, 3, 4, 5}
    sum := 0
    for _, num := range numbers {
        sum += num
    }
    fmt.Printf("%d", sum)
}

In the above example, the range keyword iterates over the numbers array elements and calculates the elements' sum.

Conclusion

The Go programming language provides a powerful and flexible way to work with data structures such as slices, arrays, maps, etc., using the range keyword. The range keyword enables developers to quickly iterate over the elements in a data structure and perform operations on each element, such as printing the length of each character in a string, as demonstrated in this tutorial.

By understanding the basics of the range keyword, developers can write more efficient and effective Go code. The range keyword is essential to mastering Go programming and can be used in various contexts to simplify data processing tasks.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram