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

Maps क्या हैं

एक map phone book जैसा है: आप एक नाम (key) ढूंढते हैं और यह तुरंत आपको matching number (value) बता देती है, किसी slice की तरह entries को 0, 1, 2 number करने के बजाय।
Syntax
go
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

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 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"])
}

The nil Map Trap

सिर्फ 'var m map[string]int' से declared एक map nil है, और किसी nil map से पढ़ना सुरक्षित है (यह zero value return करता है), इसमें लिखना एक runtime panic का कारण बनता है। यह एक बहुत आम beginner mistake है।

Note: किसी map में लिखने से पहले हमेशा इसे make() या एक literal से initialize करें।

उदाहरण: The nil Map Trap

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() {
	// 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)
}

Checking If a Key Exists

किसी ऐसी key को पढ़ना जो मौजूद नहीं, एक error के बजाय map की value type की zero value return करता है, जो अस्पष्ट हो सकता है। 'comma ok' form -- value, ok := m[key] -- आपको definitively बताता है कि key असल में मौजूद थी या नहीं।

Note: जब zero value एक valid, अस्पष्ट result हो (जैसे किसी असली score के लिए 0) तो हमेशा comma-ok form उपयोग करें।

उदाहरण: Checking If a Key Exists

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}
	// 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)
}
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. किसी nil map में लिखना ('var m map[K]V' से declared लेकिन कभी make से initialize नहीं हुआ) जो एक runtime panic का कारण बनता है।
  2. यह मान लेना कि iterate करते समय maps insertion order बनाए रखते हैं -- Go जान-बूझकर map iteration order को randomize करता है।
  3. किसी गायब 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:

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.