For Range Loop in Go

List Topics
February 15, 2025
No Comments
4 min read

The for range loop in Go is a powerful construct used to iterate over elements in arrays, slices, maps, channels, and strings. It simplifies looping by automatically handling indexing and retrieving values.

1. Syntax of for range Loop

Go
for key, value := range collection {
    // Code to execute
}

🔹 Key Points:
✅ key: Represents the index (for slices/arrays) or key (for maps).
✅ value: The actual value at the index or key.
✅ If the key is not needed, use _.

2. Using for range with Slices/Arrays

Go
package main

import "fmt"

func main() {
    nums := []int{10, 20, 30, 40, 50}

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

🔹 Output:

Bash
Index: 0, Value: 10
Index: 1, Value: 20
Index: 2, Value: 30
Index: 3, Value: 40
Index: 4, Value: 50

👉 Explanation:

  • The index represents the position in the slice.
  • The value represents the actual element at that position.

3. Ignoring the Index

If the index is not needed, use _ to ignore it.

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

🔹 Output:

Bash
10
20
30
40
50

4. Using for range with Maps

In maps, for range iterates over keys and values.

Go
package main

import "fmt"

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

    for name, score := range studentScores {
        fmt.Printf("Name: %s, Score: %d\n", name, score)
    }
}

🔹 Output:

Bash
Name: Alice, Score: 85
Name: Bob, Score: 90
Name: Charlie, Score: 78

👉 Explanation:

  • The name represents the key in the map.
  • The score represents the value associated with the key.

✅ Ignoring values and only iterating over keys:

Go
for name := range studentScores {
    fmt.Println("Student:", name)
}

5. Using for range with Strings

The for range loop can iterate over Unicode characters in a string.

Go
package main

import "fmt"

func main() {
    text := "Hello"

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

🔹 Output:

Bash
Index: 0, Character: H
Index: 1, Character: e
Index: 2, Character: l
Index: 3, Character: l
Index: 4, Character: o

👉 Explanation:

  • The index is the position of the character in the string.
  • The char is the actual Unicode character.

✅ Ignoring the index and printing only characters:

Go
for _, char := range text {
    fmt.Println(string(char))
}

6. Using for range with Channels

A for range loop can be used to receive values from a channel until it is closed.

Go
package main

import "fmt"

func main() {
    ch := make(chan int, 3)
    ch <- 1
    ch <- 2
    ch <- 3
    close(ch) // Closing the channel

    for value := range ch {
        fmt.Println(value)
    }
}

🔹 Output:

Bash
1
2
3

👉 Explanation:

  • The loop reads values from ch until it is closed.

7. Using for range with Structs (Custom Data Types)

You can also iterate over struct slices using for range.

Go
package main

import "fmt"

type Employee struct {
    Name  string
    Salary int
}

func main() {
    employees := []Employee{
        {"Alice", 5000},
        {"Bob", 6000},
        {"Charlie", 7000},
    }

    for _, emp := range employees {
        fmt.Printf("Name: %s, Salary: %d\n", emp.Name, emp.Salary)
    }
}

🔹 Output:

Bash
Name: Alice, Salary: 5000
Name: Bob, Salary: 6000
Name: Charlie, Salary: 7000

👉 Explanation:

  • The loop iterates over the slice of Employee structs and prints their details.

Conclusion

Data StructureIteration TypeExample
Slice/ArrayIndex & Valuefor i, v := range slice {}
MapKey & Valuefor k, v := range map {}
StringIndex & Characterfor i, ch := range "Hello" {}
ChannelValues from Channelfor v := range ch {}

🚀 Go’s for range loop is a powerful way to iterate over different data structures with clean and readable syntax.


Go-āϤ⧇ for range āϞ⧁āĻĒ āϕ⧀?

🔹 for range āϞ⧁āĻĒ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇ āϏāĻŋāĻŽā§āĻĒāϞ āωāĻĒāĻžāϝāĻŧ⧇ āĻ…ā§āϝāĻžāϰ⧇, āĻ¸ā§āϞāĻžāχāϏ, āĻŽā§āϝāĻžāĻĒ, āĻšā§āϝāĻžāύ⧇āϞ āĻ“ āĻ¸ā§āĻŸā§āϰāĻŋāĻ‚-āĻāϰ āωāĻĒāϰ āϞ⧁āĻĒ āϚāĻžāϞāĻžāύ⧋ āϝāĻžāϝāĻŧāĨ¤

1. āĻ…ā§āϝāĻžāϰ⧇/āĻ¸ā§āϞāĻžāχāϏ-āĻāϰ āωāĻĒāϰ for range āϞ⧁āĻĒ

Go
nums := []int{10, 20, 30}
for index, value := range nums {
    fmt.Println(index, value)
}

✅ āĻļ⧁āϧ⧁ āĻŽāĻžāύ āύāĻŋāϤ⧇ āϚāĻžāχāϞ⧇:

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

2. āĻŽā§āϝāĻžāĻĒ⧇āϰ āωāĻĒāϰ for range āϞ⧁āĻĒ

Go
students := map[string]int{"Alice": 85, "Bob": 90}
for name, score := range students {
    fmt.Println(name, score)
}

✅ āĻļ⧁āϧ⧁ āϕ⧀ āύāĻŋāϤ⧇ āϚāĻžāχāϞ⧇:

Go
for name := range students {
    fmt.Println(name)
}

3. āĻ¸ā§āĻŸā§āϰāĻŋāĻ‚-āĻāϰ āωāĻĒāϰ for range āϞ⧁āĻĒ

Go
text := "Hello"
for _, char := range text {
    fmt.Println(string(char))
}

✅ āĻ…āĻ•ā§āώāϰ āĻĒā§āϰāĻŋāĻ¨ā§āϟ āĻ•āϰāĻŦ⧇, UTF-8 āϏāĻžāĻĒā§‹āĻ°ā§āĻŸā§‡āĻĄāĨ¤

4. āĻšā§āϝāĻžāύ⧇āϞ-āĻāϰ āωāĻĒāϰ for range āϞ⧁āĻĒ

Go
ch := make(chan int, 3)
ch <- 1
ch <- 2
ch <- 3
close(ch)

for value := range ch {
    fmt.Println(value)
}

✅ āĻšā§āϝāĻžāύ⧇āϞ āĻŦāĻ¨ā§āϧ āύāĻž āĻ•āϰāϞ⧇ āϞ⧁āĻĒ āϚāϞāϤ⧇ āĻĨāĻžāĻ•āĻŦ⧇!

5. āĻ¸ā§āĻŸā§āϰāĻžāĻ•āϚāĻžāϰ (Struct) āĻāϰ āωāĻĒāϰ for range āϞ⧁āĻĒ

Go
type Employee struct {
    Name  string
    Salary int
}

employees := []Employee{
    {"Alice", 5000},
    {"Bob", 6000},
}

for _, emp := range employees {
    fmt.Println(emp.Name, emp.Salary)
}

āωāĻĒāϏāĻ‚āĻšāĻžāϰ

✅ for range āϞ⧁āĻĒ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇ āϏāĻšāĻœā§‡ āĻ…ā§āϝāĻžāϰ⧇, āĻŽā§āϝāĻžāĻĒ, āĻšā§āϝāĻžāύ⧇āϞ, āĻ“ āĻ¸ā§āĻŸā§āϰāĻŋāĻ‚-āĻāϰ āωāĻĒāϰ āϞ⧁āĻĒ āϚāĻžāϞāĻžāύ⧋ āϝāĻžāϝāĻŧāĨ¤ 🚀

Š2025 Linux Bangla | Developed & Maintaind by Linux Bangla.