Skip to content

Commit 2697eb4

Browse files
committed
Adds delete into a map eg
1 parent 68f6d7f commit 2697eb4

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

codes/ch5_maps/delete-into-a-map.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
/**
19+
* Old way to perform delete operation on map.
20+
* Will no more work for new version of Go compiler
21+
*/
22+
// prefixMap["Saju"] = "", false
23+
24+
/**
25+
* New way to perform delete operation on map.
26+
*/
27+
delete(prefixMap, "Saju")
28+
}
29+
30+
return prefixMap[name]
31+
}
32+
33+
func main() {
34+
35+
fmt.Println("What is Saju's role? He is " + GetPrefix("Saju", false))
36+
37+
fmt.Println("What is Saju's role? He is " + GetPrefix("Saju", true))
38+
39+
}

0 commit comments

Comments
 (0)