Lessons
Go Introduction
Go Get Started
Go Syntax
Go Comments
Go Variables
- Declare Variables
- Go में Variables का परिचय
- Go में Multiple Variable Declaration
- Variable Naming Rules (नामकरण के नियम)
Go Constants
Go Output
Go Data Types
- Basic Data Types
- Go Boolean Data Type (बूलियन डेटा प्रकार)
- Go Integer Data Types
- Go Float Data Types
- Go String Data Type
Go Arrays
Go Slices
Go Operators
- Go Operators
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
Go Conditions
Go Switch
Go Loops
Go Functions
Go Struct
Go Maps
Project Structure
Go Maps
Go Maps का हिंदी में विवरण
Maps का उपयोग key:value जोड़े में डेटा को संग्रहित करने के लिए किया जाता है।
हर element एक key:value pair होता है। Maps unordered और changeable होते हैं और duplicates allow नहीं करते।
Map का default value nil होता है। Map के length को len() function से जाना जा सकता है।
Maps बनाना - var और := का उपयोग करके
package main
import ("fmt")
func main() {
var a = map[string]string{"brand": "Ford", "model": "Mustang", "year": "1964"}
b := map[string]int{"Oslo": 1, "Bergen": 2, "Trondheim": 3, "Stavanger": 4}
fmt.Printf("a\t%v\n", a)
fmt.Printf("b\t%v\n", b)
}
Result:
a map[brand:Ford model:Mustang year:1964] b map[Bergen:2 Oslo:1 Stavanger:4 Trondheim:3]
Maps बनाना - make() function का उपयोग करके
var a = make(map[string]string) a["brand"] = "Ford" a["model"] = "Mustang" a["year"] = "1964" b := make(map[string]int) b["Oslo"] = 1 b["Bergen"] = 2 b["Trondheim"] = 3 b["Stavanger"] = 4
Result:
a map[brand:Ford model:Mustang year:1964] b map[Bergen:2 Oslo:1 Stavanger:4 Trondheim:3]
Empty Map बनाना
var a = make(map[string]string) // सही तरीका var b map[string]string // b अभी nil है fmt.Println(a == nil) // false fmt.Println(b == nil) // true
Result:
false true
Map Element Access करना
a["brand"] = "Ford" fmt.Printf(a["brand"])
Result:
Ford
Map Update और Add करना
a["year"] = "1970" // Update a["color"] = "red" // Add fmt.Println(a)
Result:
map[brand:Ford model:Mustang year:1970 color:red]
Map से Element हटाना
delete(a, "year") fmt.Println(a)
Result:
map[brand:Ford model:Mustang color:red]
Map में Key Check करना
val, ok := a["brand"] val2, ok2 := a["color"] _, ok3 := a["model"] fmt.Println(val, ok) // Ford true fmt.Println(val2, ok2) // "" false fmt.Println(ok3) // true
Result:
Ford true false true
Maps Are References
b := a b["year"] = "1970" fmt.Println(a) // a भी बदल जाएगा
Result:
map[brand:Ford model:Mustang year:1970 color:red]
Map Iterate करना
for k, v := range a {
fmt.Printf("%v : %v, ", k, v)
}
Result:
brand : Ford, model : Mustang, year : 1970, color : red,
Map Iterate in Specific Order
order := []string{"brand", "model", "year", "color"}
for _, key := range order {
fmt.Printf("%v : %v, ", key, a[key])
}
Result:
brand : Ford, model : Mustang, year : 1970, color : red,