26 files changed

+418
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@
1010

1111
5. [Golang Control Flow Statements: If, Switch and For](https://www.callicoder.com/golang-control-flow/)
1212

13-
6. [Working with Arrays in Golang](https://www.callicoder.com/golang-arrays/)
13+
6. [Working with Arrays in Golang](https://www.callicoder.com/golang-arrays/)
14+
15+
7. [Introduction to Slices in Golang](https://www.callicoder.com/golang-slices/)
16+
17+
8. [Golang Maps by Example](https://www.callicoder.com/golang-maps/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var slice1 = []string{"C", "C++", "Java"}
7+
var slice2 = append(slice1, "Python", "Ruby", "Go")
8+
9+
fmt.Printf("slice1 = %v, len = %d, cap = %d\n", slice1, len(slice1), cap(slice1))
10+
fmt.Printf("slice2 = %v, len = %d, cap = %d\n", slice2, len(slice2), cap(slice2))
11+
12+
slice1[0] = "C#"
13+
fmt.Println("\nslice1 = ", slice1)
14+
fmt.Println("slice2 = ", slice2)
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var slice1 = make([]string, 3, 10)
7+
copy(slice1, []string{"C", "C++", "Java"})
8+
9+
var slice2 = append(slice1, "Python", "Ruby", "Go")
10+
11+
fmt.Printf("slice1 = %v, len = %d, cap = %d\n", slice1, len(slice1), cap(slice1))
12+
fmt.Printf("slice2 = %v, len = %d, cap = %d\n", slice2, len(slice2), cap(slice2))
13+
14+
slice1[0] = "C#"
15+
fmt.Println("\nslice1 = ", slice1)
16+
fmt.Println("slice2 = ", slice2)
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var s []string
7+
8+
// Appending to a nil slice
9+
s = append(s, "Cat", "Dog", "Lion", "Tiger")
10+
11+
fmt.Printf("s = %v, len = %d, cap = %d\n", s, len(s), cap(s))
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var slice1 = []string{"Jack", "John", "Peter"}
7+
var slice2 = []string{"Bill", "Mark", "Steve"}
8+
9+
var slice3 = append(slice1, slice2...)
10+
11+
fmt.Println("slice1 = ", slice1)
12+
fmt.Println("slice2 = ", slice2)
13+
fmt.Println("After appending slice1 & slice2 = ", slice3)
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var s = []int{10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
7+
fmt.Println("Original Slice")
8+
fmt.Printf("s = %v, len = %d, cap = %d\n", s, len(s), cap(s))
9+
10+
s = s[1:5]
11+
fmt.Println("\nAfter slicing from index 1 to 5")
12+
fmt.Printf("s = %v, len = %d, cap = %d\n", s, len(s), cap(s))
13+
14+
s = s[:8]
15+
fmt.Println("\nAfter extending the length")
16+
fmt.Printf("s = %v, len = %d, cap = %d\n", s, len(s), cap(s))
17+
18+
s = s[2:]
19+
fmt.Println("\nAfter dropping the first two elements")
20+
fmt.Printf("s = %v, len = %d, cap = %d\n", s, len(s), cap(s))
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var src = []string{"Sublime", "VSCode", "IntelliJ", "Eclipse"}
7+
var dest = make([]string, 2)
8+
9+
var numElementsCopied = copy(dest, src)
10+
11+
fmt.Println("src = ", src)
12+
fmt.Println("dest = ", dest)
13+
fmt.Println("Number of elements copied from src to dest = ", numElementsCopied)
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var a = [5]string{"C", "C++", "Java", "Python", "Go"}
7+
8+
var slice1 = a[1:4]
9+
var slice2 = a[:3]
10+
var slice3 = a[2:]
11+
var slice4 = a[:]
12+
13+
fmt.Println("Array a = ", a)
14+
fmt.Println("slice1 = ", slice1)
15+
fmt.Println("slice2 = ", slice2)
16+
fmt.Println("slice3 = ", slice3)
17+
fmt.Println("slice4 = ", slice4)
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var a = [5]string{"Alpha", "Beta", "Gamma", "Delta", "Epsilon"}
7+
8+
// Creating a slice from the array
9+
var s []string = a[1:4]
10+
11+
fmt.Println("Array a = ", a)
12+
fmt.Println("Slice s = ", s)
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var a = [6]int{10, 20, 30, 40, 50, 60}
7+
var s = a[1:4]
8+
9+
fmt.Printf("s = %v, len = %d, cap = %d\n", s, len(s), cap(s))
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// Creating a slice using a slice literal
7+
var s = []int{3, 5, 7, 9, 11, 13, 17} // Creates an array, and returns a slice reference to the array
8+
9+
// Short hand declaration
10+
t := []int{2, 4, 8, 16, 32, 64}
11+
12+
fmt.Println("s = ", s)
13+
fmt.Println("t = ", t)
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// Creates an array of size 10, slices it till index 5, and returns the slice reference
7+
var s = make([]int, 5, 10)
8+
fmt.Printf("s = %v, len = %d, cap = %d\n", s, len(s), cap(s))
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// Creates an array of size 5, and returns a slice reference to it
7+
var s = make([]int, 5)
8+
fmt.Printf("s = %v, len = %d, cap = %d\n", s, len(s), cap(s))
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var a = [7]string{"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"}
7+
8+
var slice1 = a[1:]
9+
var slice2 = a[3:]
10+
11+
fmt.Println("------- Before Modifications -------")
12+
fmt.Println("a = ", a)
13+
fmt.Println("slice1 = ", slice1)
14+
fmt.Println("slice2 = ", slice2)
15+
16+
slice1[0] = "TUE"
17+
slice1[1] = "WED"
18+
slice1[2] = "THU"
19+
20+
slice2[1] = "FRIDAY"
21+
22+
fmt.Println("\n-------- After Modifications --------")
23+
fmt.Println("a = ", a)
24+
fmt.Println("slice1 = ", slice1)
25+
fmt.Println("slice2 = ", slice2)
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var s = [][]string{
7+
{"James Smith", "United States"},
8+
{"Maria Gracia", "England"},
9+
{"Sarah Johnson", "France"},
10+
}
11+
12+
fmt.Println("Slice s = ", s)
13+
fmt.Println("length = ", len(s))
14+
fmt.Println("capacity = ", cap(s))
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var s []int
7+
fmt.Printf("s = %v, len = %d, cap = %d\n", s, len(s), cap(s))
8+
9+
if s == nil {
10+
fmt.Println("s is nil")
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var cities = []string{"New York", "London", "Chicago", "Beijing", "Delhi", "Mumbai", "Bangalore", "Hyderabad", "Hong Kong"}
7+
8+
var asianCities = cities[3:]
9+
var indianCities = asianCities[1:5]
10+
11+
fmt.Println("cities = ", cities)
12+
fmt.Println("asianCities = ", asianCities)
13+
fmt.Println("indianCities = ", indianCities)
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var employeeSalary = map[string]float64{
7+
"John": 78000.00,
8+
"Steve": 160000.50,
9+
"David": 85000.00,
10+
}
11+
12+
var salary = employeeSalary["Steve"]
13+
fmt.Printf("Steve's Salary = %f\n", salary)
14+
15+
// If a key doesn't exist in the map, we get the zero value of the value type
16+
salary = employeeSalary["Jack"]
17+
fmt.Printf("Jack's Salary = %f\n", salary)
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// Initializing a map
7+
var tinderMatch = make(map[string]string)
8+
9+
// Adding keys to a map
10+
tinderMatch["Rajeev"] = "Angelina" // Assigns the value "Angelina" to the key "Rajeev"
11+
tinderMatch["James"] = "Sophia"
12+
tinderMatch["David"] = "Emma"
13+
14+
fmt.Println(tinderMatch)
15+
16+
/*
17+
Adding a key that already exists will simply override
18+
the existing key with the new value
19+
*/
20+
tinderMatch["Rajeev"] = "Jennifer"
21+
fmt.Println(tinderMatch)
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var employees = map[int]string{
7+
1001: "Rajeev",
8+
1002: "Sachin",
9+
1003: "James",
10+
}
11+
12+
printEmployee(employees, 1001)
13+
printEmployee(employees, 1010)
14+
15+
if isEmployeeExists(employees, 1002) {
16+
fmt.Println("EmployeeId 1002 found")
17+
}
18+
}
19+
20+
func printEmployee(employees map[int]string, employeeId int) {
21+
if name, ok := employees[employeeId]; ok {
22+
fmt.Printf("name = %s, ok = %v\n", name, ok)
23+
} else {
24+
fmt.Printf("EmployeeId %d not found\n", employeeId)
25+
}
26+
}
27+
28+
func isEmployeeExists(employees map[int]string, employeeId int) bool {
29+
_, ok := employees[employeeId]
30+
return ok
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var m map[string]int
7+
fmt.Println(m)
8+
if m == nil {
9+
fmt.Println("m is nil")
10+
}
11+
12+
// The following statement will result in a runtime error
13+
// m["key"] = 100
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var fileExtensions = map[string]string{
7+
"Python": ".py",
8+
"C++": ".cpp",
9+
"Java": ".java",
10+
"Golang": ".go",
11+
"Kotlin": ".kt",
12+
}
13+
14+
fmt.Println(fileExtensions)
15+
16+
delete(fileExtensions, "Kotlin")
17+
18+
// delete function doesn't do anything if the key doesn't exist
19+
delete(fileExtensions, "Javascript")
20+
21+
fmt.Println(fileExtensions)
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var m = make(map[string]int)
7+
8+
fmt.Println(m)
9+
10+
if m == nil {
11+
fmt.Println("m is nil")
12+
} else {
13+
fmt.Println("m is not nil")
14+
}
15+
16+
// make() function returns an initialized and ready to use map.
17+
// Since it is initialized, you can add new keys to it.
18+
m["one hundred"] = 100
19+
fmt.Println(m)
20+
21+
}

0 commit comments

Comments
 (0)