Maps क्या हैं
In this page:
var name map[keyType]valueType
name := map[keyType]valueType{key: value}
name := make(map[keyType]valueType)
Creating a Map
एक map keys को values से जोड़ता है, Python में dictionary या JavaScript में object जैसा। यह या तो make(map[KeyType]ValueType) से या initial key-value pairs list करती एक map literal से बनाया जाता है।
उदाहरण: Creating 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 prices and infer the type
prices := map[string]float64{
"apple": 0.5,
"banana": 0.25,
}
// Print the values followed by a newline
fmt.Println(prices["apple"])
}
Login to try C/C++/Java/PHP code in the editor
The nil Map Trap
सिर्फ 'var m map[string]int' से declared एक map nil है, और किसी nil map से पढ़ना सुरक्षित है (यह zero value return करता है), इसमें लिखना एक runtime panic का कारण बनता है। यह एक बहुत आम beginner mistake है।
उदाहरण: The nil Map Trap
// 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() {
// Declare scores with the zero value of its type
var scores map[string]int
// Print the values followed by a newline
fmt.Println("read from nil map is safe:", scores["nobody"])
scores = make(map[string]int)
scores["alice"] = 90
// Print the values followed by a newline
fmt.Println(scores)
}
Login to try C/C++/Java/PHP code in the editor
Checking If a Key Exists
किसी ऐसी key को पढ़ना जो मौजूद नहीं, एक error के बजाय map की value type की zero value return करता है, जो अस्पष्ट हो सकता है। 'comma ok' form -- value, ok := m[key] -- आपको definitively बताता है कि key असल में मौजूद थी या नहीं।
उदाहरण: Checking If a Key Exists
// 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}
// Short declaration: create age, ok and infer the type
age, ok := ages["priya"]
// Print the values followed by a newline
fmt.Println("age:", age, "found:", ok)
}
Login to try C/C++/Java/PHP code in the editor
- किसी nil map में लिखना ('var m map[K]V' से declared लेकिन कभी make से initialize नहीं हुआ) जो एक runtime panic का कारण बनता है।
- यह मान लेना कि iterate करते समय maps insertion order बनाए रखते हैं -- Go जान-बूझकर map iteration order को randomize करता है।
- किसी गायब key को पढ़ना और returned zero value को ऐसे treat करना जैसे key मौजूद थी, बजाय second 'comma ok' return value जांचने के।
- एक map key-value pairs store करता है, map[KeyType]ValueType के रूप में declared।
- आप उनमें लिखने से पहले maps को make() या एक map literal से initialize करना चाहिए -- एक nil map write पर panic करता है।
- किसी गायब key को पढ़ना value type की zero value return करता है, कोई error नहीं, जब तक आप comma-ok form उपयोग न करें।
- Map iteration order Go runtime द्वारा जान-बूझकर randomized है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: