Skip to content

Commit b01a5d8

Browse files
committed
Adds checking existence into a map eg
1 parent 2697eb4 commit b01a5d8

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
import "fmt";
4+
5+
func GetPrefix(name string, mustDel bool) (prefix string) {
6+
7+
/**
8+
* Short hand way to declare and initialize map
9+
*/
10+
prefixMap := map[string] string {
11+
"Ashwin": "Sr. Fullstack Engineer",
12+
"Kumar": "Sr. Engineering Manager",
13+
"Saju": "Sr. Solution Architect",
14+
"Ajay": "Sr. Solution Architect", // comma is needed here
15+
}
16+
17+
if mustDel {
18+
delete(prefixMap, "Saju")
19+
}
20+
21+
/**
22+
* Check if the value exist into the map or not.
23+
*/
24+
if _, exists := prefixMap[name]; exists {
25+
return prefixMap[name]
26+
} else {
27+
return "dude"
28+
}
29+
30+
}
31+
32+
func main() {
33+
34+
fmt.Println("What is Saju's role? He is " + GetPrefix("Saju", false))
35+
36+
fmt.Println("What is Saju's role? He is " + GetPrefix("Saju", true))
37+
38+
}

0 commit comments

Comments
 (0)