Programming Languages
Go
Subjective
Oct 04, 2025
What is the range keyword and how is it used?
Detailed Explanation
The range keyword in Go:
• Iterates over arrays, slices, maps, channels, strings
• Returns index/key and value
• Can ignore values using blank identifier
• Different behavior for different types
Examples:
// Slice
for i, v := range slice {
fmt.Println(i, v)
}
// Map
for key, value := range m {
fmt.Println(key, value)
}
// String (runes)
for i, r := range "hello" {
fmt.Println(i, string(r))
}
// Channel
for value := range ch {
fmt.Println(value)
}
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts