diff --git a/codes/ch5_maps/check-existence-into-map.go b/codes/ch5_maps/check-existence-into-map.go new file mode 100644 index 0000000..52e75bf --- /dev/null +++ b/codes/ch5_maps/check-existence-into-map.go @@ -0,0 +1,38 @@ +package main + +import "fmt"; + +func GetPrefix(name string, mustDel bool) (prefix string) { + + /** + * Short hand way to declare and initialize map + */ + prefixMap := map[string] string { + "Ashwin": "Sr. Fullstack Engineer", + "Kumar": "Sr. Engineering Manager", + "Saju": "Sr. Solution Architect", + "Ajay": "Sr. Solution Architect", // comma is needed here + } + + if mustDel { + delete(prefixMap, "Saju") + } + + /** + * Check if the value exist into the map or not. + */ + if _, exists := prefixMap[name]; exists { + return prefixMap[name] + } else { + return "dude" + } + +} + +func main() { + + fmt.Println("What is Saju's role? He is " + GetPrefix("Saju", false)) + + fmt.Println("What is Saju's role? He is " + GetPrefix("Saju", true)) + +} \ No newline at end of file diff --git a/codes/ch6_slices/appending-to-slices.go b/codes/ch6_slices/appending-to-slices.go new file mode 100644 index 0000000..ceb3559 --- /dev/null +++ b/codes/ch6_slices/appending-to-slices.go @@ -0,0 +1,35 @@ +package main + +import "fmt" + +func main() { + + /** + * Blank [] will declare a slices. + * If you provide size like [5] will declare an array. + */ + var employee []string + + /** + * Initialize a slices with type, length and capacity. + * length is required field. + * + * make([]string, length, [capacity]) + */ + employee = make([]string, 3) + + employee[0] = "Ashwin" + employee[1] = "Kumar" + employee[2] = "Saju" + + fmt.Println("Slices: ", employee) + + /** + * Appending into slices. + */ + employee = append(employee, "Ajay") + employee = append(employee, "Vinayak") + + fmt.Println("After appending: ", employee) + +} \ No newline at end of file diff --git a/codes/ch6_slices/create-a-slices.go b/codes/ch6_slices/create-a-slices.go new file mode 100644 index 0000000..9b02002 --- /dev/null +++ b/codes/ch6_slices/create-a-slices.go @@ -0,0 +1,23 @@ +package main + +func main() { + + /** + * Blank [] will declare a slices. + * If you provide size like [5] will declare an array. + */ + var employee []string + + /** + * Initialize a slices with type, length and capacity. + * length is required field. + * + * make([]string, length, [capacity]) + */ + employee = make([]string, 3) + + employee[0] = "Ashwin" + employee[1] = "Kumar" + employee[2] = "Saju" + // employee[3] = "Ajay" // This will throw an error `out of range` +} \ No newline at end of file diff --git a/codes/ch6_slices/delete-from-a-slices.go b/codes/ch6_slices/delete-from-a-slices.go new file mode 100644 index 0000000..0a44846 --- /dev/null +++ b/codes/ch6_slices/delete-from-a-slices.go @@ -0,0 +1,36 @@ +package main + +import "fmt" + +func main() { + + /** + * Blank [] will declare a slices. + * If you provide size like [5] will declare an array. + */ + var employee []string + + /** + * Initialize a slices with type, length and capacity. + * length is required field. + * + * make([]string, length, [capacity]) + */ + employee = make([]string, 6) + + employee[0] = "Ashwin" + employee[1] = "Kumar" + employee[2] = "Saju" + employee[3] = "Ajay" + employee[4] = "Vinayak" + employee[5] = "Jerin" + + fmt.Println("Slices: ", employee) + + // 0:2 - include 0, 1 but excludes 2 + // 4: - include last + // ... - expanding slices + employee = append(employee[0:2], employee[4:]...) + + fmt.Println("After Delete: ", employee) +} \ No newline at end of file diff --git a/codes/ch6_slices/short-hand-way.go b/codes/ch6_slices/short-hand-way.go new file mode 100644 index 0000000..ec474de --- /dev/null +++ b/codes/ch6_slices/short-hand-way.go @@ -0,0 +1,12 @@ +package main + +func main() { + + /** + * Short hand way to declare and initialize slices + */ + employee := []string { + "Ashwin", "Kumar", "Saju", "Ajay" + } + +} \ No newline at end of file diff --git a/codes/ch6_slices/slicing-slices.go b/codes/ch6_slices/slicing-slices.go new file mode 100644 index 0000000..3679576 --- /dev/null +++ b/codes/ch6_slices/slicing-slices.go @@ -0,0 +1,49 @@ +package main + +import "fmt" + +func main() { + + /** + * Blank [] will declare a slices. + * If you provide size like [5] will declare an array. + */ + var employee []string + + /** + * Initialize a slices with type, length and capacity. + * length is required field. + * + * make([]string, length, [capacity]) + */ + employee = make([]string, 7) + + employee[0] = "Ashwin" + employee[1] = "Kumar" + employee[2] = "Saju" + employee[3] = "Ajay" + employee[4] = "Jerin" + employee[5] = "Vinayak" + employee[6] = "Pavan" + + /** + * Slices and updating slices + */ + employee = employee[0:] + fmt.Println("[0:]", employee) + + employee = employee[0:7] + fmt.Println("[0:7]", employee) + + employee = employee[0:len(employee)] + fmt.Println("[0:len(employee)]", employee) + + employee = employee[:4] + fmt.Println("[0:4]", employee) + + employee = employee[0:4] + fmt.Println("[:4]", employee) + + employee = employee[4:7] + fmt.Println("[4:7]", employee) +} \ No newline at end of file diff --git a/codes/session-6/.gitkeep b/codes/session-6/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/codes/session-7/.gitkeep b/codes/session-7/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/codes/session-8/.gitkeep b/codes/session-8/.gitkeep deleted file mode 100644 index e69de29..0000000