Skip to content

Commit e29f73e

Browse files
author
Ashwin Hegde
committed
Merge pull request #22 from hegdeashwin/develop
Develop
2 parents 2544b69 + 0593fe6 commit e29f73e

File tree

9 files changed

+193
-0
lines changed

9 files changed

+193
-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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
7+
/**
8+
* Blank [] will declare a slices.
9+
* If you provide size like [5] will declare an array.
10+
*/
11+
var employee []string
12+
13+
/**
14+
* Initialize a slices with type, length and capacity.
15+
* length is required field.
16+
*
17+
* make([]string, length, [capacity])
18+
*/
19+
employee = make([]string, 3)
20+
21+
employee[0] = "Ashwin"
22+
employee[1] = "Kumar"
23+
employee[2] = "Saju"
24+
25+
fmt.Println("Slices: ", employee)
26+
27+
/**
28+
* Appending into slices.
29+
*/
30+
employee = append(employee, "Ajay")
31+
employee = append(employee, "Vinayak")
32+
33+
fmt.Println("After appending: ", employee)
34+
35+
}

codes/ch6_slices/create-a-slices.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
func main() {
4+
5+
/**
6+
* Blank [] will declare a slices.
7+
* If you provide size like [5] will declare an array.
8+
*/
9+
var employee []string
10+
11+
/**
12+
* Initialize a slices with type, length and capacity.
13+
* length is required field.
14+
*
15+
* make([]string, length, [capacity])
16+
*/
17+
employee = make([]string, 3)
18+
19+
employee[0] = "Ashwin"
20+
employee[1] = "Kumar"
21+
employee[2] = "Saju"
22+
// employee[3] = "Ajay" // This will throw an error `out of range`
23+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
7+
/**
8+
* Blank [] will declare a slices.
9+
* If you provide size like [5] will declare an array.
10+
*/
11+
var employee []string
12+
13+
/**
14+
* Initialize a slices with type, length and capacity.
15+
* length is required field.
16+
*
17+
* make([]string, length, [capacity])
18+
*/
19+
employee = make([]string, 6)
20+
21+
employee[0] = "Ashwin"
22+
employee[1] = "Kumar"
23+
employee[2] = "Saju"
24+
employee[3] = "Ajay"
25+
employee[4] = "Vinayak"
26+
employee[5] = "Jerin"
27+
28+
fmt.Println("Slices: ", employee)
29+
30+
// 0:2 - include 0, 1 but excludes 2
31+
// 4: - include last
32+
// ... - expanding slices
33+
employee = append(employee[0:2], employee[4:]...)
34+
35+
fmt.Println("After Delete: ", employee)
36+
}

codes/ch6_slices/short-hand-way.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
func main() {
4+
5+
/**
6+
* Short hand way to declare and initialize slices
7+
*/
8+
employee := []string {
9+
"Ashwin", "Kumar", "Saju", "Ajay"
10+
}
11+
12+
}

codes/ch6_slices/slicing-slices.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
7+
/**
8+
* Blank [] will declare a slices.
9+
* If you provide size like [5] will declare an array.
10+
*/
11+
var employee []string
12+
13+
/**
14+
* Initialize a slices with type, length and capacity.
15+
* length is required field.
16+
*
17+
* make([]string, length, [capacity])
18+
*/
19+
employee = make([]string, 7)
20+
21+
employee[0] = "Ashwin"
22+
employee[1] = "Kumar"
23+
employee[2] = "Saju"
24+
employee[3] = "Ajay"
25+
employee[4] = "Jerin"
26+
employee[5] = "Vinayak"
27+
employee[6] = "Pavan"
28+
29+
/**
30+
* Slices and updating slices
31+
*/
32+
employee = employee[0:]
33+
fmt.Println("[0:]", employee)
34+
35+
employee = employee[0:7]
36+
fmt.Println("[0:7]", employee)
37+
38+
employee = employee[0:len(employee)]
39+
fmt.Println("[0:len(employee)]", employee)
40+
41+
employee = employee[:4]
42+
fmt.Println("[0:4]", employee)
43+
44+
employee = employee[0:4]
45+
fmt.Println("[:4]", employee)
46+
47+
employee = employee[4:7]
48+
fmt.Println("[4:7]", employee)
49+
}

codes/session-6/.gitkeep

Whitespace-only changes.

codes/session-7/.gitkeep

Whitespace-only changes.

codes/session-8/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)