Skip to content

Commit 73b609e

Browse files
committed
maps
1 parent f80bca8 commit 73b609e

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

10_maps/maps.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package main
2+
import (
3+
"fmt"
4+
"maps"
5+
)
6+
7+
8+
9+
10+
11+
12+
//maps->hash,pbjects,dictionary
13+
func main() {
14+
//creating map
15+
m:=make(map[string]string) //[key]value
16+
17+
//add elments
18+
m["name"]="golang"
19+
m["area"]="backend"
20+
21+
//get an element
22+
fmt.Println(m["name"],m["area"])
23+
24+
//if accessing the key which doesnt exists
25+
//fmt.Println(m["phone"]) --------output-----empty value(zero value)
26+
27+
n:=make(map[string]int)
28+
n["age"]=30
29+
fmt.Println(n["phone"]) // output-0 (zero value according to int type in value)
30+
31+
32+
//can get length of map
33+
fmt.Println(len(m)) //---2
34+
35+
36+
//delete from map
37+
delete(m,"area")
38+
fmt.Println(m)
39+
40+
41+
42+
//to empty the whole map
43+
//clear(m)
44+
//fmt.Println(m)
45+
46+
47+
//without using make fn we can make map
48+
//m:=map[string]int{"phone":40, "phones":3} - |
49+
// |
50+
//check element in map or not or take action |
51+
v,ok:=m["price"] // |
52+
fmt.Println(v) //we get values 3 <-
53+
54+
if ok{
55+
fmt.Println("all ok")
56+
}else{
57+
fmt.Println("not ok")
58+
} ///all ok
59+
60+
61+
62+
//to check both maps are equal or not
63+
64+
65+
m1:=map[string]int{"phone":40, "phones":3}
66+
67+
m2:=map[string]int{"phone":40, "phones":3}
68+
69+
fmt.Println(maps.Equal(m1,m2))
70+
71+
72+
73+
74+
75+
76+
77+
78+
}

0 commit comments

Comments
 (0)