Iterating Over Data Structures in Go

List Topics
February 17, 2025
No Comments
4 min read

In Go, you can iterate over various data structures like arrays, slices, maps, and channels using loops. The most common iteration method involves using the for loop. Let’s look at how to iterate over these data structures step by step.

1️⃣ Iterating Over Arrays and Slices

You can use a for loop with an index to iterate over arrays and slices. Go also provides a convenient range keyword to iterate over these structures.

🔹 Example (Using for loop):

Go
package main

import "fmt"

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

    // Iterating over array using index
    for i := 0; i < len(arr); i++ {
        fmt.Println(arr[i])
    }
}

🔹 Output:

Go
1
2
3

🔹 Example (Using range):

Go
package main

import "fmt"

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

    // Iterating over slice using range
    for index, value := range arr {
        fmt.Println("Index:", index, "Value:", value)
    }
}

🔹 Output:

Bash
Index: 0 Value: 1
Index: 1 Value: 2
Index: 2 Value: 3

👉 The range keyword is a more idiomatic way to iterate over arrays and slices. It provides both the index and the value of each element.

2️⃣ Iterating Over Maps

Maps in Go can be iterated using the range keyword as well. This gives you access to both the key and the value of each key-value pair.

🔹 Example:

Go
package main

import "fmt"

func main() {
    m := map[string]int{
        "Alice": 25,
        "Bob":   30,
    }

    // Iterating over map using range
    for key, value := range m {
        fmt.Println("Key:", key, "Value:", value)
    }
}

🔹 Output:

Bash
Key: Alice Value: 25
Key: Bob Value: 30

👉 The range syntax here iterates through each key-value pair, giving you access to both the key and the value.

3️⃣ Iterating Over Channels

Channels can be iterated in Go, but it’s done a little differently. You can use the range keyword to iterate over the values received from a channel.

🔹 Example:

Go
package main

import "fmt"

func main() {
    ch := make(chan int, 3)
    ch <- 10
    ch <- 20
    ch <- 30
    close(ch)

    // Iterating over channel using range
    for value := range ch {
        fmt.Println(value)
    }
}

🔹 Output:

Bash
10
20
30

👉 The range over a channel works by continuously receiving values from the channel until it’s closed.

4️⃣ Iterating Over Strings (Runes)

When iterating over a string in Go, you get the individual runes (Unicode characters) and their index.

🔹 Example:

Go
package main

import "fmt"

func main() {
    str := "Hello"

    // Iterating over string using range
    for index, runeValue := range str {
        fmt.Printf("Index: %d, Rune: %c\n", index, runeValue)
    }
}

🔹 Output:

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

👉 Here, the range loop iterates over each rune (character) in the string, providing both the index and the character itself.

Summary:

  • Arrays and Slices: Use a for loop or range to iterate over elements.
  • Maps: Use range to iterate over key-value pairs.
  • Channels: Use range to receive values from a channel until it’s closed.
  • Strings: Use range to iterate over runes (characters) in the string.

These iteration techniques help you effectively process elements within different data structures in Go.


ডাটা স্ট্রাকচারগুলোর উপর ইটারেটিং (Iterating) in Go

Go তে, বিভিন্ন ডাটা স্ট্রাকচার যেমন এরে, স্লাইস, ম্যাপ এবং চ্যানেল এ ইটারেট করার জন্য for লুপ ব্যবহার করা হয়। এখানে range কিওয়ার্ডটি একটি সহজ এবং প্রচলিত উপায় ডাটা স্ট্রাকচারগুলোতে ইটারেট করার জন্য।

1️⃣ এরে এবং স্লাইসের উপর ইটারেটিং

এরে এবং স্লাইসে ইটারেট করতে for লুপ বা range কিওয়ার্ড ব্যবহার করা হয়।

🔹 উদাহরণ (Using for loop):

Go
package main

import "fmt"

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

    // Array এর উপর index ব্যবহার করে ইটারেটিং
    for i := 0; i < len(arr); i++ {
        fmt.Println(arr[i])
    }
}

🔹 আউটপুট:

Bash
1
2
3

🔹 উদাহরণ (Using range):

Go
package main

import "fmt"

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

    // Slice এর উপর range ব্যবহার করে ইটারেটিং
    for index, value := range arr {
        fmt.Println("Index:", index, "Value:", value)
    }
}

🔹 আউটপুট:

Bash
Index: 0 Value: 1
Index: 1 Value: 2
Index: 2 Value: 3

👉 range কিওয়ার্ডটি একটি সহজ এবং পছন্দনীয় উপায় স্লাইস বা এরে ইটারেট করার জন্য। এটি ইন্ডেক্স এবং ভ্যালু দুটি দেয়।

2️⃣ ম্যাপের উপর ইটারেটিং

Go তে, ম্যাপের উপর ইটারেট করার জন্যও range কিওয়ার্ড ব্যবহার করা হয়। এতে আপনি কী এবং ভ্যালু দুটি পেতে পারেন।

🔹 উদাহরণ:

Go
package main

import "fmt"

func main() {
    m := map[string]int{
        "Alice": 25,
        "Bob":   30,
    }

    // Map এর উপর range ব্যবহার করে ইটারেটিং
    for key, value := range m {
        fmt.Println("Key:", key, "Value:", value)
    }
}

🔹 আউটপুট:

Bash
Key: Alice Value: 25
Key: Bob Value: 30

👉 এখানে, range কিওয়ার্ডটি প্রতিটি কী-ভ্যালু পেয়ার ইটারেট করে, এবং আপনাকে কী এবং ভ্যালু উভয়টি দেয়।

3️⃣ চ্যানেলের উপর ইটারেটিং

চ্যানেলের উপরও range কিওয়ার্ড ব্যবহার করে ইটারেট করা যায়, তবে এটি একটু আলাদা। আপনি চ্যানেল থেকে মান (value) পাওয়ার জন্য range ব্যবহার করবেন।

🔹 উদাহরণ:

Go
package main

import "fmt"

func main() {
    ch := make(chan int, 3)
    ch <- 10
    ch <- 20
    ch <- 30
    close(ch)

    // Channel এর উপর range ব্যবহার করে ইটারেটিং
    for value := range ch {
        fmt.Println(value)
    }
}

🔹 আউটপুট:

Bash
10
20
30

👉 চ্যানেল ইটারেট করার জন্য, range কিওয়ার্ড চ্যানেল থেকে মান গ্রহণ করে যতক্ষণ না চ্যানেল বন্ধ হয়।

4️⃣ স্ট্রিং (Rune) এর উপর ইটারেটিং

Go তে, স্ট্রিং এর উপর ইটারেট করলে আপনি পৃথক রুন (Unicode চরিত্র) এবং তাদের ইনডেক্স পেয়ে থাকেন।

🔹 উদাহরণ:

Go
package main

import "fmt"

func main() {
    str := "Hello"

    // String এর উপর range ব্যবহার করে ইটারেটিং
    for index, runeValue := range str {
        fmt.Printf("Index: %d, Rune: %c\n", index, runeValue)
    }
}

🔹 আউটপুট:

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

👉 এখানে, range কিওয়ার্ড স্ট্রিং এর প্রতিটি রুন (অথবা চরিত্র) ইটারেট করে এবং ইনডেক্স এবং চরিত্র দুটো দেয়।

সারাংশ:

  • এরে এবং স্লাইস: for লুপ বা range কিওয়ার্ড ব্যবহার করে ইটারেট করা হয়।
  • ম্যাপ: range কিওয়ার্ড ব্যবহার করে কী-ভ্যালু পেয়ার ইটারেট করা হয়।
  • চ্যানেল: range কিওয়ার্ড ব্যবহার করে চ্যানেল থেকে মান গ্রহণ করা হয় যতক্ষণ না চ্যানেল বন্ধ হয়।
  • স্ট্রিং: range কিওয়ার্ড ব্যবহার করে স্ট্রিংয়ের প্রতিটি রুন (চরিত্র) ইটারেট করা হয়।

এই ইটারেশন টেকনিকগুলো আপনাকে Go তে বিভিন্ন ডাটা স্ট্রাকচারগুলোতে কার্যকরভাবে কাজ করার সুযোগ দেয়।

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