Skip to content

Commit 68db215

Browse files
committed
Added maps tutorial under go_basics
1 parent 9c158ee commit 68db215

File tree

4 files changed

+170
-0
lines changed

4 files changed

+170
-0
lines changed

go_basics/go.work

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use (
66
./datatypes
77
./defer_panic_recover
88
./functions
9+
./gomaps
910
./loops
1011
./numeric_constants
1112
./pointers

go_basics/gomaps/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module anarchymonkey.com/go-basics/gomaps
2+
3+
go 1.20

go_basics/gomaps/gomaps.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"strings"
7+
"unicode/utf8"
8+
)
9+
10+
type Address struct {
11+
city string
12+
state string
13+
basicAddress string
14+
extendedAddress string
15+
pincode int
16+
}
17+
type UserProfile struct {
18+
name string
19+
nickName string
20+
21+
age int
22+
23+
address Address
24+
}
25+
26+
func WordCount(s string) (map[string]int, error) {
27+
28+
if !utf8.ValidString(s) || s == "" {
29+
return nil, errors.New("Cannot parse non utf-8 embedded string")
30+
}
31+
wordCount := make(map[string]int)
32+
33+
for _, value := range strings.Split(s, " ") {
34+
if _, hasKey := wordCount[string(value)]; hasKey {
35+
wordCount[string(value)] = wordCount[string(value)] + 1
36+
} else {
37+
wordCount[string(value)] = 1
38+
}
39+
}
40+
41+
return wordCount, nil
42+
}
43+
44+
var m map[string]UserProfile
45+
46+
func GetUserProfile() {
47+
// What are maps?
48+
// Ans: Maps are just a store of keys and values
49+
50+
m = make(map[string]UserProfile)
51+
52+
m["Aniket"] = UserProfile{
53+
name: "Aniket",
54+
nickName: "Rick",
55+
age: 27,
56+
address: Address{
57+
city: "Bangalore",
58+
state: "Karnataka",
59+
basicAddress: "Jhingalala huhu",
60+
extendedAddress: "huhu huhu",
61+
pincode: 560001,
62+
},
63+
}
64+
65+
fmt.Println("Maps")
66+
fmt.Println("Map: ", m["Aniket"])
67+
68+
// map literals
69+
70+
var mapLiteral map[string]UserProfile = map[string]UserProfile{
71+
"Meeta": {
72+
name: "Meeta",
73+
nickName: "Meetu",
74+
age: 27,
75+
address: Address{
76+
city: "Bangalore",
77+
state: "Karnataka",
78+
basicAddress: "Jhingalala huhu",
79+
extendedAddress: "huhu huhu",
80+
pincode: 560075,
81+
},
82+
},
83+
"Aniket": {
84+
name: "Aniket",
85+
nickName: "Rick",
86+
age: 26,
87+
address: Address{
88+
city: "Bangalore",
89+
state: "Karnataka",
90+
basicAddress: "Jhingalala huhu",
91+
extendedAddress: "huhu huhu",
92+
pincode: 560015,
93+
},
94+
},
95+
}
96+
97+
for key, userProfile := range mapLiteral {
98+
fmt.Printf("Nickname at index = %s is %v \n", key, userProfile.address)
99+
}
100+
101+
aniketsDetails := mapLiteral["Aniket"]
102+
103+
fmt.Println("Anikets details are ", aniketsDetails)
104+
105+
// delete a key
106+
107+
delete(mapLiteral, "Meeta")
108+
109+
// check if key exists or not
110+
111+
elem, ok := mapLiteral["Aniket"]
112+
113+
if ok {
114+
fmt.Println("The values are", elem, ok)
115+
}
116+
}
117+
118+
func main() {
119+
GetUserProfile()
120+
121+
fmt.Println("Word count map is ")
122+
fmt.Println(WordCount("Hey my name is Aniket"))
123+
fmt.Println(WordCount("Aniket is awesome, Aniket is so good"))
124+
}

go_basics/gomaps/gomaps_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import "testing"
4+
5+
func TestWordCount(t *testing.T) {
6+
7+
actual_output, error := WordCount("Hey my name is Aniket")
8+
9+
if error != nil {
10+
t.Error("Found error", error)
11+
return
12+
}
13+
14+
newMap := make(map[string]int)
15+
16+
newMap = map[string]int{
17+
"Aniket": 1,
18+
"is": 1,
19+
"name": 1,
20+
"my": 1,
21+
"Hey": 1,
22+
}
23+
24+
for key, value := range actual_output {
25+
keyCountInNewMap := newMap[key]
26+
actualOutputCount := value
27+
28+
if actualOutputCount != keyCountInNewMap {
29+
t.Error("The map count does not match")
30+
}
31+
}
32+
}
33+
34+
func TestWordCountEmpty(t *testing.T) {
35+
36+
_, error := WordCount("")
37+
38+
if error == nil {
39+
t.Error("Found error", error)
40+
return
41+
}
42+
}

0 commit comments

Comments
 (0)