range Keyword क्या है
In this page:
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
// 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)
}
}
Login to try C/C++/Java/PHP code in the editor
Ignoring the Index or Value
अगर आपको सिर्फ value की परवाह है index की नहीं (या उल्टा), blank identifier _ उस हिस्से को discard कर देता है जो आपको नहीं चाहिए, Go के इस नियम को satisfy करते हुए कि हर declared variable उपयोग होना चाहिए।
उदाहरण: Ignoring the Index or Value
// 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)
}
}
Login to try C/C++/Java/PHP code in the editor
Ranging Over a Map
किसी map पर range हर key-value pair देता है, लेकिन iteration का order Go runtime द्वारा हर बार program चलने पर जान-बूझकर randomize किया जाता है, जो code को गलती से किसी specific order पर निर्भर होने से रोकता है।
उदाहरण: Ranging Over a Map
// 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)
}
Login to try C/C++/Java/PHP code in the editor
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
// 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)
}
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि पुराने Go versions में range loop variable हर iteration में एक नया variable है -- 1.22 से पहले यह reuse होता था, आम closure-capture bugs का कारण बनते हुए।
- सिर्फ value चाहिए होने पर range द्वारा दिए गए index/key को नज़रअंदाज़ करना, बजाय इसे discard करने के लिए blank identifier _ उपयोग करने के।
- किसी 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: