
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.
for range Loopfor 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 _.
for range with Slices/Arrayspackage 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:
Index: 0, Value: 10
Index: 1, Value: 20
Index: 2, Value: 30
Index: 3, Value: 40
Index: 4, Value: 50
đ Explanation:
index represents the position in the slice.value represents the actual element at that position.If the index is not needed, use _ to ignore it.
for _, value := range nums {
fmt.Println(value)
}
đš Output:
10
20
30
40
50
for range with MapsIn maps, for range iterates over keys and values.
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:
Name: Alice, Score: 85
Name: Bob, Score: 90
Name: Charlie, Score: 78
đ Explanation:
name represents the key in the map.score represents the value associated with the key.â Ignoring values and only iterating over keys:
for name := range studentScores {
fmt.Println("Student:", name)
}
for range with StringsThe for range loop can iterate over Unicode characters in a string.
package main
import "fmt"
func main() {
text := "Hello"
for index, char := range text {
fmt.Printf("Index: %d, Character: %c\n", index, char)
}
}
đš Output:
Index: 0, Character: H
Index: 1, Character: e
Index: 2, Character: l
Index: 3, Character: l
Index: 4, Character: o
đ Explanation:
index is the position of the character in the string.char is the actual Unicode character.â Ignoring the index and printing only characters:
for _, char := range text {
fmt.Println(string(char))
}
for range with ChannelsA for range loop can be used to receive values from a channel until it is closed.
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:
1
2
3
đ Explanation:
ch until it is closed.for range with Structs (Custom Data Types)You can also iterate over struct slices using for range.
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:
Name: Alice, Salary: 5000
Name: Bob, Salary: 6000
Name: Charlie, Salary: 7000
đ Explanation:
Employee structs and prints their details.| Data Structure | Iteration Type | Example |
|---|---|---|
| Slice/Array | Index & Value | for i, v := range slice {} |
| Map | Key & Value | for k, v := range map {} |
| String | Index & Character | for i, ch := range "Hello" {} |
| Channel | Values from Channel | for v := range ch {} |
đ Goâs for range loop is a powerful way to iterate over different data structures with clean and readable syntax.
for range āϞā§āĻĒ āĻā§?đš for range āϞā§āĻĒ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰ⧠āϏāĻŋāĻŽā§āĻĒāϞ āĻāĻĒāĻžāϝāĻŧā§ āĻ
ā§āϝāĻžāϰā§, āϏā§āϞāĻžāĻāϏ, āĻŽā§āϝāĻžāĻĒ, āĻā§āϝāĻžāύā§āϞ āĻ āϏā§āĻā§āϰāĻŋāĻ-āĻāϰ āĻāĻĒāϰ āϞā§āĻĒ āĻāĻžāϞāĻžāύ⧠āϝāĻžāϝāĻŧāĨ¤
for range āϞā§āĻĒnums := []int{10, 20, 30}
for index, value := range nums {
fmt.Println(index, value)
}
â āĻļā§āϧ⧠āĻŽāĻžāύ āύāĻŋāϤ⧠āĻāĻžāĻāϞā§:
for _, value := range nums {
fmt.Println(value)
}
for range āϞā§āĻĒstudents := map[string]int{"Alice": 85, "Bob": 90}
for name, score := range students {
fmt.Println(name, score)
}
â āĻļā§āϧ⧠āĻā§ āύāĻŋāϤ⧠āĻāĻžāĻāϞā§:
for name := range students {
fmt.Println(name)
}
for range āϞā§āĻĒtext := "Hello"
for _, char := range text {
fmt.Println(string(char))
}
â āĻ āĻā§āώāϰ āĻĒā§āϰāĻŋāύā§āĻ āĻāϰāĻŦā§, UTF-8 āϏāĻžāĻĒā§āϰā§āĻā§āĻĄāĨ¤
for range āϞā§āĻĒch := make(chan int, 3)
ch <- 1
ch <- 2
ch <- 3
close(ch)
for value := range ch {
fmt.Println(value)
}
â āĻā§āϝāĻžāύā§āϞ āĻŦāύā§āϧ āύāĻž āĻāϰāϞ⧠āϞā§āĻĒ āĻāϞāϤ⧠āĻĨāĻžāĻāĻŦā§!
for range āϞā§āĻĒ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 āϞā§āĻĒ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻāϰ⧠āϏāĻšāĻā§ āĻ
ā§āϝāĻžāϰā§, āĻŽā§āϝāĻžāĻĒ, āĻā§āϝāĻžāύā§āϞ, āĻ āϏā§āĻā§āϰāĻŋāĻ-āĻāϰ āĻāĻĒāϰ āϞā§āĻĒ āĻāĻžāϞāĻžāύ⧠āϝāĻžāϝāĻŧāĨ¤ đ