← Back to Go Course | Chapter 5: Arrays Slices Maps | Lesson 7 of 7

Maps पर Iteration करना

किसी map को range से चलना हर key और value visit करता है, लेकिन Go हर बार जान-बूझकर order को shuffle करता है ताकि आप कभी गलती से इस पर निर्भर न रहें।
Syntax
go
for key, value := range m {
    // body
}

Basic Map Iteration

किसी map पर range हर key और उसकी corresponding value देता है, आपको पहले से keys जाने बिना हर entry process करने देते हुए।

उदाहरण: Basic Map Iteration

markup
// 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)
}

Iteration Order Is Randomized

Go जान-बूझकर उस order को randomize करता है जिसमें range किसी map की entries visit करता है, program के हर single run पर। यह जान-बूझकर है, developers को एक ऐसे order पर गलती से निर्भर होने से रोकने के लिए जिसकी कभी गारंटी नहीं थी।

Note: कभी ऐसा code (या tests) न लिखें जो एक particular map iteration order मान ले।

उदाहरण: Iteration Order Is Randomized

markup
// 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)")
}

Getting a Deterministic Order

जब एक stable, repeatable order मायने रखे -- जैसे एक report print करना -- map की keys को एक slice में इकट्ठा करें, उस slice को sort करें, और फिर sorted keys के ज़रिए हर value ढूंढें।

उदाहरण: Getting a Deterministic Order

markup
// 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])
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. ऐसा code लिखना जो maps के insertion या sorted order में iterate होने पर निर्भर करे -- Go इसे हर run explicitly randomize करता है।
  2. किसी map की keys को उस पर range करते समय modify करने की कोशिश करना, जो नई जोड़ी keys के लिए undefined behavior रखता है।
  3. जब एक 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.