Maps पर Iteration करना
for key, value := range m {
// body
}
Basic Map Iteration
किसी map पर range हर key और उसकी corresponding value देता है, आपको पहले से keys जाने बिना हर entry process करने देते हुए।
उदाहरण: Basic Map Iteration
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create fruitCounts and infer the type
fruitCounts := map[string]int{"apple": 3, "banana": 5}
// Short declaration: create total and infer the type
total := 0
// Loop over fruitCounts; the blank identifier _ ignores the index
for _, count := range fruitCounts {
total += count
}
// Print the values followed by a newline
fmt.Println("total fruit:", total)
}
Login to try C/C++/Java/PHP code in the editor
Iteration Order Is Randomized
Go जान-बूझकर उस order को randomize करता है जिसमें range किसी map की entries visit करता है, program के हर single run पर। यह जान-बूझकर है, developers को एक ऐसे order पर गलती से निर्भर होने से रोकने के लिए जिसकी कभी गारंटी नहीं थी।
उदाहरण: Iteration Order Is Randomized
// Every Go file belongs to a package; main builds an executable
package main
// Import the fmt package
import "fmt"
// main is where the program starts running
func main() {
// Short declaration: create m and infer the type
m := map[int]string{1: "a", 2: "b", 3: "c"}
// Short declaration: create count and infer the type
count := 0
for k := range m {
count++
_ = k
}
// Print the values followed by a newline
fmt.Println("visited", count, "entries (order varies each run)")
}
Login to try C/C++/Java/PHP code in the editor
Getting a Deterministic Order
जब एक stable, repeatable order मायने रखे -- जैसे एक report print करना -- map की keys को एक slice में इकट्ठा करें, उस slice को sort करें, और फिर sorted keys के ज़रिए हर value ढूंढें।
उदाहरण: Getting a Deterministic Order
// Every Go file belongs to a package; main builds an executable
package main
// Import the packages this program uses
import (
"fmt"
"sort"
)
// main is where the program starts running
func main() {
// Short declaration: create scores and infer the type
scores := map[string]int{"charlie": 3, "alice": 1, "bob": 2}
// Short declaration: create keys and infer the type
keys := make([]string, 0, len(scores))
for k := range scores {
// append adds elements and returns the updated slice
keys = append(keys, k)
}
sort.Strings(keys)
// Loop over keys; the blank identifier _ ignores the index
for _, k := range keys {
// Print the values followed by a newline
fmt.Println(k, scores[k])
}
}
Login to try C/C++/Java/PHP code in the editor
- ऐसा code लिखना जो maps के insertion या sorted order में iterate होने पर निर्भर करे -- Go इसे हर run explicitly randomize करता है।
- किसी map की keys को उस पर range करते समय modify करने की कोशिश करना, जो नई जोड़ी keys के लिए undefined behavior रखता है।
- जब एक deterministic, repeatable output order असल में ज़रूरी हो तब explicitly keys sort करना (जैसे एक अलग slice के ज़रिए) भूल जाना।
- 'for key, value := range m' किसी map की हर entry पर iterate करता है।
- Map iteration order Go runtime द्वारा randomized है और इस पर कभी भरोसा नहीं करना चाहिए।
- एक predictable order पाने के लिए, पहले keys को एक slice में इकट्ठा करें और उस slice को sort करें।
- Iteration के दौरान current key हटाना सुरक्षित है; iteration के दौरान नई keys जोड़ने का undefined प्रभाव है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: