← Back to Go Course | Chapter 3: Control Flow | Lesson 7 of 7

range Keyword क्या है

range Go का यह कहने का तरीका है 'इस collection के हर item से एक-एक करके गुज़रो, और मुझे बताओ हर एक कहां है'।
Syntax
go
for index, value := range collection {
    // body
}

for key, value := range mapValue {
    // body
}

Ranging Over a Slice

किसी slice या array पर range आपको हर step पर index और value दोनों देता है, manually एक index variable और bounds checks manage करने से बचाते हुए।

उदाहरण: Ranging Over a Slice

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 fruits and infer the type
	fruits := []string{"apple", "banana", "cherry"}
	// Loop over fruits, getting each index and value
	for i, fruit := range fruits {
		// Print the values followed by a newline
		fmt.Println(i, fruit)
	}
}

Ignoring the Index or Value

अगर आपको सिर्फ value की परवाह है index की नहीं (या उल्टा), blank identifier _ उस हिस्से को discard कर देता है जो आपको नहीं चाहिए, Go के इस नियम को satisfy करते हुए कि हर declared variable उपयोग होना चाहिए।

Note: Go का compiler एक declared-but-unused variable को reject करता है, इसलिए _ ज़रूरी है जब आपको दो range values में से सिर्फ एक चाहिए।

उदाहरण: Ignoring the Index or Value

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 fruits and infer the type
	fruits := []string{"apple", "banana", "cherry"}
	// Loop over fruits; the blank identifier _ ignores the index
	for _, fruit := range fruits {
		// Print the values followed by a newline
		fmt.Println("fruit:", fruit)
	}
}

Ranging Over a Map

किसी map पर range हर key-value pair देता है, लेकिन iteration का order Go runtime द्वारा हर बार program चलने पर जान-बूझकर randomize किया जाता है, जो code को गलती से किसी specific order पर निर्भर होने से रोकता है।

Note: Map iteration order Go द्वारा हर run पर जान-बूझकर randomize किया जाता है -- कभी इसके consistent होने पर भरोसा न करें।

उदाहरण: Ranging Over a Map

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 ages and infer the type
	ages := map[string]int{"Amit": 30, "Sara": 25}
	// Short declaration: create total and infer the type
	total := 0
	// Loop over ages; the blank identifier _ ignores the index
	for _, age := range ages {
		total += age
	}
	// Print the values followed by a newline
	fmt.Println("total age:", total)
}

Ranging Over a String

किसी string पर range इसे UTF-8 के रूप में decode करता है, उस position पर byte index और rune (पूरा character) देते हुए -- एक साधारण byte-index loop के विपरीत multi-byte characters को सही से handle करते हुए।

उदाहरण: Ranging Over a String

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() {
	for i, r := range "Go!" {
		// Print using a format string
		fmt.Printf("%d: %c\n", i, r)
	}
}
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. यह मान लेना कि पुराने Go versions में range loop variable हर iteration में एक नया variable है -- 1.22 से पहले यह reuse होता था, आम closure-capture bugs का कारण बनते हुए।
  2. सिर्फ value चाहिए होने पर range द्वारा दिए गए index/key को नज़रअंदाज़ करना, बजाय इसे discard करने के लिए blank identifier _ उपयोग करने के।
  3. किसी slice की length को उस पर range करते हुए modify करना, जो पहले से शुरू हो चुकी range कितनी iterations करती है यह नहीं बदलता।
चैप्टर सारांश
  • range arrays, slices, strings, maps, और channels पर iterate करता है, अपना output हर type के हिसाब से adapt करते हुए।
  • Slices/arrays के लिए, range index और value देता है; maps के लिए, key और value; strings के लिए, byte index और rune।
  • किसी ऐसी value को नज़रअंदाज़ करने के लिए blank identifier _ उपयोग करें जो आपको range से नहीं चाहिए।
  • Go 1.22 से, हर range iteration को अपना एक नया loop variable मिलता है, closure bugs की एक पुरानी class से बचते हुए।
🔒

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.