GoLang - Range

3 minute read

In this tutorial, you will learn about “Range in Go Programming” with the help of example.

In Go programming, the “range” keyword is a powerful tool that allows you to iterate over elements in a collection, such as an array, slice, string, or map. It simplifies the process of accessing each element without worrying about indices or manual iteration. In this tutorial, we will explore the basics of using “range” in Go programming with examples and explanations in simple and easy-to-understand language. So let’s dive in and learn about “range” in Go!

Range with Arrays and Slices

The “range” keyword can be used to iterate over elements in an array or slice, providing access to both the index and the value of each element.

Example with Array:

package main

import "fmt"

func main() {
    numbers := [4]int{1, 2, 3, 4}

    for index, value := range numbers {
        fmt.Printf("Index: %d, Value: %d\n", index, value)
    }
}

In this example, we have an array called 'numbers' with four elements. Using the “range” keyword in the “for” loop, we iterate over each element of the array. In each iteration, we assign the current 'index' to the variable index and the corresponding value to the variable 'value'. We then print the index and value using the 'fmt.Printf' statement.

Example with Slice:

package main

import "fmt"

func main() {
    names := []string{"Alice", "Bob", "Charlie"}

    for index, value := range names {
        fmt.Printf("Index: %d, Value: %s\n", index, value)
    }
}

In this example, we have a slice called names containing three strings. Similar to the array example, the “range” keyword in the “for” loop allows us to iterate over each element of the slice. We assign the current index to the variable index and the corresponding value to the variable value. We then print the index and value using the fmt.Printf statement.

Range with Strings

The “range” keyword can also be used to iterate over characters in a string.

Example:

package main

import "fmt"

func main() {
    message := "Hello, World!"

    for index, character := range message {
        fmt.Printf("Index: %d, Character: %c\n", index, character)
    }
}

In this example, we have a string called message containing the text “Hello, World!”. By using the “range” keyword in the “for” loop, we iterate over each character of the string. In each iteration, we assign the current index to the variable index and the corresponding character to the variable character. We then print the index and character using the fmt.Printf statement.

Range with Maps

The “range” keyword can also be used to iterate over key-value pairs in a map.

Example:

package main

import "fmt"

func main() {
    studentGrades := map[string]int{
        "Alice":  90,
        "Bob":    85,
        "Charlie": 95,
    }

    for key, value := range studentGrades {
        fmt.Printf("Name: %s, Grade: %d\n", key, value)
    }
}

In this example, we have a map called studentGrades with student names as 'keys' and their corresponding grades as 'values'. Using the “range” keyword in the “for” loop, we iterate over each key-value pair in the map. In each iteration, we assign the current key to the variable key and the corresponding value to the variable value. We then print the name and grade using the 'fmt.Printf' statement.

Ignoring Index or Value

Sometimes, you may only be interested in the index or value while iterating using “range”. If you don’t need one of them, you can use the underscore (_) to discard the unused variable.

Example:

package main

import "fmt"

func main() {
    numbers := []int{1, 2, 3, 4}

    for _, value := range numbers {
        fmt.Println(value)
    }
}

In this example, we have a slice of numbers. We use the underscore ('_') to discard the index since we are not using it. The loop only prints the value of each element.

By mastering the “range” keyword, you can efficiently iterate over collections, perform operations on each element, and process data in your Go programs.

The “range” keyword in Go programming provides a convenient way to iterate over elements in collections such as arrays, slices, strings, and maps. It simplifies the process of accessing both the index and value of each element, making it easier to work with data structures.

In this tutorial, we explored how to use “range” with arrays, slices, strings, and maps. We learned how to access the index and value of each element in a loop and use them in our code. Additionally, we saw how to discard the index or value if it’s not needed using the underscore (_) symbol.

Congratulations on completing this beginner’s guide to “range” in Go programming! With this knowledge, you’re now equipped to work with collections and iterate over elements effortlessly. So go ahead, practice using “range” in your programs, and unlock the full potential of working with data structures in Go!

Updated: