Skip to content

Commit 2544b69

Browse files
author
Ashwin Hegde
committed
Merge pull request #21 from hegdeashwin/develop
Develop
2 parents f94e051 + 2697eb4 commit 2544b69

File tree

6 files changed

+142
-0
lines changed

6 files changed

+142
-0
lines changed

codes/ch5_maps/create-a-map.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
/**
4+
* Note: The code will not run and throw an error: prefixMap declared and not used
5+
* As we have declare map and not used in and this is error in go-lang
6+
*/
7+
func main() {
8+
9+
/**
10+
* Declare a map of map type with string.
11+
* var <variable-name> map[<key-type>] <value-type>
12+
*/
13+
var prefixMap map[string] string
14+
15+
/**
16+
* make will initialize the map; if we don't use make
17+
* declaration will set to null;
18+
*/
19+
prefixMap = make(map[string] string)
20+
21+
}

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+
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import "fmt";
4+
5+
func GetPrefix(name string) (prefix string) {
6+
7+
var prefixMap map[string] string
8+
prefixMap = make(map[string] string)
9+
10+
/**
11+
* Insert key and value into the map.
12+
*
13+
* <map-variable>[<key>] = <value>
14+
*/
15+
prefixMap["Ashwin"] = "Sr. Fullstack Engineer"
16+
prefixMap["Kumar"] = "Sr. Engineering Manager"
17+
prefixMap["Saju"] = "Sr. Solution Architect"
18+
prefixMap["Ajay"] = "Sr. solution Architect"
19+
20+
return prefixMap[name]
21+
}
22+
23+
func main() {
24+
25+
fmt.Println("What is Ashwin's role? He is " + GetPrefix("Ashwin"))
26+
27+
}

codes/ch5_maps/short-hand-way.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import "fmt";
4+
5+
func GetPrefix(name string) (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+
return prefixMap[name]
18+
}
19+
20+
func main() {
21+
22+
fmt.Println("What is Ashwin's role? He is " + GetPrefix("Ashwin"))
23+
24+
}

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import "fmt";
4+
5+
func GetPrefix(name string) (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+
/**
18+
* Perform update operation on map.
19+
*/
20+
prefixMap["Kumar"] = "Delivery Manager"
21+
prefixMap["Ajay"] = "Sr. Project Manager"
22+
23+
return prefixMap[name]
24+
}
25+
26+
func main() {
27+
28+
fmt.Println("What is Kumar's role? He is " + GetPrefix("Kumar"))
29+
fmt.Println("What is Ajay's role? He is " + GetPrefix("Ajay"))
30+
31+
}

codes/session-5/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)