From e3c5ab9a39e44e1c5bc71c6eadb841ab5b7a77d5 Mon Sep 17 00:00:00 2001
From: Bill Xue <xueeinstein@gmail.com>
Date: Sun, 28 Jul 2019 15:43:58 +0800
Subject: [PATCH 1/2] read and try code from emacs

---
 01-hello-world/main.go                        |  2 +-
 02-variables/01-variable-declaration/main.go  |  8 ++---
 02-variables/02-type-inference/main.go        |  2 ++
 03-basic-types/03-numeric-operations/main.go  |  7 +++--
 05-control-flow/01-if-condition/main.go       | 15 +++++-----
 05-control-flow/04-switch-statement/main.go   | 29 ++++++++++++-------
 .../05-switch-combine-cases/main.go           | 15 +++++-----
 .../06-switch-with-no-expression/main.go      | 19 ++++++------
 05-control-flow/07-for-loop/main.go           |  4 ++-
 05-control-flow/11-for-break/main.go          |  3 +-
 .../main.go                                   |  2 +-
 07-packages/main/main.go                      |  8 ++---
 07-packages/myapp/app.go                      | 10 +++----
 07-packages/numbers/prime.go                  | 13 +++++----
 07-packages/strings/greeting/texts.go         | 12 ++++----
 08-arrays/07-multi-dimensional-array/main.go  |  6 ++++
 09-slices/05-slice-length-capacity/main.go    |  2 ++
 .../13-slice-append-another-slice/main.go     |  4 +++
 10-maps/01-map-declaration/main.go            |  3 +-
 11-pointers/02-pointer-initialization/main.go |  1 +
 11-pointers/03-pointer-dereference/main.go    |  6 ++++
 .../main/main.go                              | 12 ++++----
 .../main.go                                   |  4 +++
 23 files changed, 113 insertions(+), 74 deletions(-)

diff --git a/01-hello-world/main.go b/01-hello-world/main.go
index 52f4492..5a7148a 100644
--- a/01-hello-world/main.go
+++ b/01-hello-world/main.go
@@ -5,4 +5,4 @@ import "fmt"
 
 func main() {
 	fmt.Println("Hello, World")
-}
\ No newline at end of file
+}
diff --git a/02-variables/01-variable-declaration/main.go b/02-variables/01-variable-declaration/main.go
index 8b01d82..209f034 100644
--- a/02-variables/01-variable-declaration/main.go
+++ b/02-variables/01-variable-declaration/main.go
@@ -9,24 +9,20 @@ func main() {
 	var myFloat float64 = 45.12
 	fmt.Println(myStr, myInt, myFloat)
 
-
 	//================================
 
-
 	// Multiple Declarations
 	var (
-		employeeId int = 5
+		employeeId          int    = 5
 		firstName, lastName string = "Satoshi", "Nakamoto"
 	)
 	fmt.Println(employeeId, firstName, lastName)
 
-
 	//================================
 
-
 	// Short variable declaration syntax
 	name := "Rajeev Singh"
 	age, salary, isProgrammer := 35, 50000.0, true
 
 	fmt.Println(name, age, salary, isProgrammer)
-}
\ No newline at end of file
+}
diff --git a/02-variables/02-type-inference/main.go b/02-variables/02-type-inference/main.go
index f4f1216..3cb34fd 100644
--- a/02-variables/02-type-inference/main.go
+++ b/02-variables/02-type-inference/main.go
@@ -14,4 +14,6 @@ func main() {
 
 	fmt.Printf("firstName: %T, lastName: %T, age: %T, salary: %T\n",
 		firstName, lastName, age, salary)
+	var yes = true
+	fmt.Printf("yes: %T\n", yes)
 }
diff --git a/03-basic-types/03-numeric-operations/main.go b/03-basic-types/03-numeric-operations/main.go
index 6f9aaec..5b81901 100644
--- a/03-basic-types/03-numeric-operations/main.go
+++ b/03-basic-types/03-numeric-operations/main.go
@@ -7,7 +7,7 @@ import (
 
 func main() {
 	var a, b = 4, 5
-	var res1 = (a + b) * (a + b)/2  // Arithmetic operations
+	var res1 = (a + b) * (a + b) / 2 // Arithmetic operations
 
 	a++ // Increment a by 1
 
@@ -16,7 +16,8 @@ func main() {
 	var res2 = a ^ b // Bitwise XOR
 
 	var r = 3.5
-	var res3 = math.Pi * r * r  // Operations on floating-point type
+	var res3 = math.Pi * r * r // Operations on floating-point type
 
 	fmt.Printf("res1 : %v, res2 : %v, res3 : %v\n", res1, res2, res3)
-}
\ No newline at end of file
+	fmt.Printf("pi: %v\n", math.Pi)
+}
diff --git a/05-control-flow/01-if-condition/main.go b/05-control-flow/01-if-condition/main.go
index bd64b51..24b90af 100644
--- a/05-control-flow/01-if-condition/main.go
+++ b/05-control-flow/01-if-condition/main.go
@@ -1,10 +1,11 @@
 package main
+
 import "fmt"
 
 func main() {
 	// If Statement
 	var x = 25
-	if(x % 5 == 0) {
+	if x%5 == 0 {
 		fmt.Printf("%d is a multiple of 5\n", x)
 	}
 
@@ -13,16 +14,16 @@ func main() {
 	if y < 0 {
 		fmt.Printf("%d is negative\n", y)
 	}
-	
+
 	// If with a condition consisting of short circuit operators
 	var age = 21
 	if age >= 17 && age <= 30 {
 		fmt.Println("My Age is between 17 and 30")
-	}	
+	}
 
-	// If with a short statement	
+	// If with a short statement
 	if n := 10; n%2 == 0 {
 		fmt.Printf("%d is even\n", n)
-	} 
-	
-}
\ No newline at end of file
+	}
+
+}
diff --git a/05-control-flow/04-switch-statement/main.go b/05-control-flow/04-switch-statement/main.go
index c43d822..f1d10b6 100644
--- a/05-control-flow/04-switch-statement/main.go
+++ b/05-control-flow/04-switch-statement/main.go
@@ -1,22 +1,31 @@
 package main
+
 import "fmt"
 
 func main() {
-	var dayOfWeek = 6
+	var dayOfWeek = 7
 	switch dayOfWeek {
-		case 1: fmt.Println("Monday")
-		case 2: fmt.Println("Tuesday")
-		case 3: fmt.Println("Wednesday")
-		case 4: fmt.Println("Thursday")
-		case 5: fmt.Println("Friday")
-		case 6: {
+	case 1:
+		fmt.Println("Monday")
+	case 2:
+		fmt.Println("Tuesday")
+	case 3:
+		fmt.Println("Wednesday")
+	case 4:
+		fmt.Println("Thursday")
+	case 5:
+		fmt.Println("Friday")
+	case 6:
+		{
 			fmt.Println("Saturday")
 			fmt.Println("Weekend. Yaay!")
 		}
-		case 7: {
+	case 7:
+		{
 			fmt.Println("Sunday")
 			fmt.Println("Weekend. Yaay!")
 		}
-		default: fmt.Println("Invalid day")
+	default:
+		fmt.Println("Invalid day")
 	}
-}
\ No newline at end of file
+}
diff --git a/05-control-flow/05-switch-combine-cases/main.go b/05-control-flow/05-switch-combine-cases/main.go
index 4f059a3..52d394e 100644
--- a/05-control-flow/05-switch-combine-cases/main.go
+++ b/05-control-flow/05-switch-combine-cases/main.go
@@ -1,13 +1,14 @@
 package main
+
 import "fmt"
 
 func main() {
 	switch dayOfWeek := 5; dayOfWeek {
-		case 1, 2, 3, 4, 5:
-			fmt.Println("Weekday")
-		case 6, 7:
-			fmt.Println("Weekend")
-		default:
-			fmt.Println("Invalid Day")		
+	case 1, 2, 3, 4, 5:
+		fmt.Println("Weekday")
+	case 6, 7:
+		fmt.Println("Weekend")
+	default:
+		fmt.Println("Invalid Day")
 	}
-}
\ No newline at end of file
+}
diff --git a/05-control-flow/06-switch-with-no-expression/main.go b/05-control-flow/06-switch-with-no-expression/main.go
index 2c3f391..5016c5a 100644
--- a/05-control-flow/06-switch-with-no-expression/main.go
+++ b/05-control-flow/06-switch-with-no-expression/main.go
@@ -1,16 +1,17 @@
 package main
+
 import "fmt"
 
 func main() {
-	var BMI = 21.0 
+	var BMI = 21.0
 	switch {
-		case BMI < 18.5:
-			fmt.Println("You're underweight")
-		case BMI >= 18.5 && BMI < 25.0:
-			fmt.Println("Your weight is normal")
-		case BMI >= 25.0 && BMI < 30.0:
-			fmt.Println("You're overweight")
-		default:
-			fmt.Println("You're obese")	
+	case BMI < 18.5:
+		fmt.Println("You're underweight")
+	case BMI >= 18.5 && BMI < 25.0:
+		fmt.Println("Your weight is normal")
+	case BMI >= 25.0 && BMI < 30.0:
+		fmt.Println("You're overweight")
+	default:
+		fmt.Println("You're obese")
 	}
 }
diff --git a/05-control-flow/07-for-loop/main.go b/05-control-flow/07-for-loop/main.go
index aa98c4f..2440035 100644
--- a/05-control-flow/07-for-loop/main.go
+++ b/05-control-flow/07-for-loop/main.go
@@ -1,8 +1,10 @@
 package main
+
 import "fmt"
 
 func main() {
 	for i := 0; i < 10; i++ {
 		fmt.Printf("%d ", i)
 	}
-}
\ No newline at end of file
+	fmt.Printf("\n")
+}
diff --git a/05-control-flow/11-for-break/main.go b/05-control-flow/11-for-break/main.go
index a4c9da3..fd990f8 100644
--- a/05-control-flow/11-for-break/main.go
+++ b/05-control-flow/11-for-break/main.go
@@ -1,4 +1,5 @@
 package main
+
 import "fmt"
 
 func main() {
@@ -8,4 +9,4 @@ func main() {
 			break
 		}
 	}
-}
\ No newline at end of file
+}
diff --git a/06-functions/02-function-multiple-return-values/main.go b/06-functions/02-function-multiple-return-values/main.go
index cdb8e52..cdd4ab4 100644
--- a/06-functions/02-function-multiple-return-values/main.go
+++ b/06-functions/02-function-multiple-return-values/main.go
@@ -12,7 +12,7 @@ func getStockPriceChange(prevPrice, currentPrice float64) (float64, float64) {
 }
 
 func main() {
-	prevStockPrice := 0.0
+	prevStockPrice := 10.0
 	currentStockPrice := 100000.0
 
 	change, percentChange := getStockPriceChange(prevStockPrice, currentStockPrice)
diff --git a/07-packages/main/main.go b/07-packages/main/main.go
index 2db9858..28e800d 100644
--- a/07-packages/main/main.go
+++ b/07-packages/main/main.go
@@ -4,15 +4,15 @@ package main
 // Importing Packages
 import (
 	"fmt"
-	"time"
 	"math"
-    "math/rand"
+	"math/rand"
+	"time"
 )
 
 func main() {
 	// Finding the Max of two numbers
 	fmt.Println(math.Max(73.15, 92.46))
-		
+
 	// Calculate the square root of a number
 	fmt.Println(math.Sqrt(225))
 
@@ -26,4 +26,4 @@ func main() {
 	// Generating a random integer between 0 to 100
 	rand.Seed(epoch)
 	fmt.Println(rand.Intn(100))
-}
\ No newline at end of file
+}
diff --git a/07-packages/myapp/app.go b/07-packages/myapp/app.go
index f6c9e07..5781760 100644
--- a/07-packages/myapp/app.go
+++ b/07-packages/myapp/app.go
@@ -2,10 +2,10 @@ package main
 
 import (
 	"fmt"
-	"github.com/callicoder/golang-tutorials/07-packages/numbers"
-	"github.com/callicoder/golang-tutorials/07-packages/strings"	
-	"github.com/callicoder/golang-tutorials/07-packages/strings/greeting" // Importing a nested package
-	str "strings"	// Package Alias
+	"golang-tutorials/07-packages/numbers"
+	"golang-tutorials/07-packages/strings"
+	"golang-tutorials/07-packages/strings/greeting" // Importing a nested package
+	str "strings"                                   // Package Alias
 )
 
 func main() {
@@ -16,4 +16,4 @@ func main() {
 	fmt.Println(strings.Reverse("callicoder"))
 
 	fmt.Println(str.Count("Go is Awesome. I love Go", "Go"))
-}
\ No newline at end of file
+}
diff --git a/07-packages/numbers/prime.go b/07-packages/numbers/prime.go
index 268884a..2c02493 100644
--- a/07-packages/numbers/prime.go
+++ b/07-packages/numbers/prime.go
@@ -4,10 +4,11 @@ import "math"
 
 // Checks if a number is prime or not
 func IsPrime(num int) bool {
-    for i := 2; i <= int(math.Floor(math.Sqrt(float64(num)))); i++ {
-        if num%i == 0 {
-            return false
-        }
+	for i := 2; i <= int(math.Floor(math.Sqrt(float64(num)))); i++ {
+		if num%i == 0 {
+			return false
+		}
+
 	}
-    return num > 1
-}
\ No newline at end of file
+	return num > 1
+}
diff --git a/07-packages/strings/greeting/texts.go b/07-packages/strings/greeting/texts.go
index 403aabe..0f00fe3 100644
--- a/07-packages/strings/greeting/texts.go
+++ b/07-packages/strings/greeting/texts.go
@@ -2,14 +2,14 @@
 package greeting
 
 // Exported
-const  (
+const (
 	WelcomeText = "Hello, World to Golang"
-    MorningText = "Good Morning"
+	MorningText = "Good Morning"
 	EveningText = "Good Evening"
 )
 
 // Not exported (only visible inside the `greeting` package)
-var loremIpsumText = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, 
-sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad 
-minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea 
-commodo consequat.`
\ No newline at end of file
+var loremIpsumText = `Lorem ipsum dolor sit amet, consectetur adipiscing elit,
+sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
+minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
+commodo consequat.`
diff --git a/08-arrays/07-multi-dimensional-array/main.go b/08-arrays/07-multi-dimensional-array/main.go
index b9b4a70..a093d64 100644
--- a/08-arrays/07-multi-dimensional-array/main.go
+++ b/08-arrays/07-multi-dimensional-array/main.go
@@ -17,4 +17,10 @@ func main() {
 		{6, 2, 11},
 	}
 	fmt.Println(b)
+
+	// Multi-dim array copy via `:=`
+	c := a
+	c[0][0] = 1
+	fmt.Println("a: ", c)
+	fmt.Println("c: ", a)
 }
diff --git a/09-slices/05-slice-length-capacity/main.go b/09-slices/05-slice-length-capacity/main.go
index 4d6d91e..692a2d3 100644
--- a/09-slices/05-slice-length-capacity/main.go
+++ b/09-slices/05-slice-length-capacity/main.go
@@ -9,6 +9,8 @@ func main() {
 	*/
 	a := [6]int{10, 20, 30, 40, 50, 60}
 	s := a[1:4]
+	p := a[:3]
 
 	fmt.Printf("s = %v, len = %d, cap = %d\n", s, len(s), cap(s))
+	fmt.Printf("p = %v, len = %d, cap = %d\n", p, len(p), cap(p))
 }
diff --git a/09-slices/13-slice-append-another-slice/main.go b/09-slices/13-slice-append-another-slice/main.go
index d40cc24..c30aa64 100644
--- a/09-slices/13-slice-append-another-slice/main.go
+++ b/09-slices/13-slice-append-another-slice/main.go
@@ -11,4 +11,8 @@ func main() {
 	fmt.Println("slice1 = ", slice1)
 	fmt.Println("slice2 = ", slice2)
 	fmt.Println("After appending slice1 & slice2 = ", slice3)
+
+	slice1[0] = "JChen"
+	fmt.Println("slice1 = ", slice1)
+	fmt.Println("After modifying slice1, slice3 = ", slice3)
 }
diff --git a/10-maps/01-map-declaration/main.go b/10-maps/01-map-declaration/main.go
index 1370fc1..df8ab84 100644
--- a/10-maps/01-map-declaration/main.go
+++ b/10-maps/01-map-declaration/main.go
@@ -3,6 +3,7 @@ package main
 import "fmt"
 
 func main() {
+	// map[KeyType]ValueType
 	var m map[string]int
 	fmt.Println(m)
 	if m == nil {
@@ -10,5 +11,5 @@ func main() {
 	}
 
 	// The following statement will result in a runtime error
-	// m["key"] = 100
+	m["key"] = 100
 }
diff --git a/11-pointers/02-pointer-initialization/main.go b/11-pointers/02-pointer-initialization/main.go
index 40473ec..cc59bd3 100644
--- a/11-pointers/02-pointer-initialization/main.go
+++ b/11-pointers/02-pointer-initialization/main.go
@@ -9,4 +9,5 @@ func main() {
 	fmt.Println("Value stored in variable a = ", a)
 	fmt.Println("Address of variable a = ", &a)
 	fmt.Println("Value stored in variable p = ", p)
+	fmt.Println(&a == p)
 }
diff --git a/11-pointers/03-pointer-dereference/main.go b/11-pointers/03-pointer-dereference/main.go
index 468dae5..7c9931d 100644
--- a/11-pointers/03-pointer-dereference/main.go
+++ b/11-pointers/03-pointer-dereference/main.go
@@ -13,4 +13,10 @@ func main() {
 	// Changing the value stored in the pointed variable through the pointer
 	*p = 2000
 	fmt.Println("a (after) = ", a)
+
+	var s = "abcdefg"
+	var ps = &s
+	fmt.Println("s = ", s)
+	fmt.Println("*ps = ", *ps)
+	// fmt.Println("*(ps+1) = ", *(ps + 1))
 }
diff --git a/12-structs/05-struct-exported-vs-unexported/main/main.go b/12-structs/05-struct-exported-vs-unexported/main/main.go
index fc9da84..d474352 100644
--- a/12-structs/05-struct-exported-vs-unexported/main/main.go
+++ b/12-structs/05-struct-exported-vs-unexported/main/main.go
@@ -2,18 +2,18 @@ package main
 
 import (
 	"fmt"
-	"github.com/callicoder/golang-tutorials/12-structs/05-struct-exported-vs-unexported/model"
+	"golang-tutorials/12-structs/05-struct-exported-vs-unexported/model"
 )
 
 func main() {
 	c := model.Customer{
-		Id: 1, 
+		Id:   1,
 		Name: "Rajeev Singh",
 	}
 
-	// c.married = true	// Error: can not refer to unexported field or method
+	// c.married = true // Error: can not refer to unexported field or method
 
 	// a := model.address{} // Error: can not refer to unexported name
-	
-	fmt.Println("Programmer = ", c);
-}
\ No newline at end of file
+
+	fmt.Println("Programmer = ", c)
+}
diff --git a/13-methods/03-same-method-names-on-different-types/main.go b/13-methods/03-same-method-names-on-different-types/main.go
index 5841c48..c24ea9f 100644
--- a/13-methods/03-same-method-names-on-different-types/main.go
+++ b/13-methods/03-same-method-names-on-different-types/main.go
@@ -29,4 +29,8 @@ func main() {
 
 	fmt.Println("5th Term of the Arithmetic series = ", ap.NthTerm(5))
 	fmt.Println("5th Term of the Geometric series = ", gp.NthTerm(5))
+	for i := 1; i < 5; i++ {
+		fmt.Printf("%dth Term of the Arithmetic series = %.0f\n", i, ap.NthTerm(i))
+	}
+
 }

From 6deb73305f9d99314187f11835ba06ea4fd59769 Mon Sep 17 00:00:00 2001
From: Bill Xue <xueeinstein@gmail.com>
Date: Sun, 28 Jul 2019 16:28:26 +0800
Subject: [PATCH 2/2] goroutine

---
 15-goroutines/01-create-goroutine/main.go     |  31 +++++++++++
 15-goroutines/02-multiple-goroutine/main.go   |  50 ++++++++++++++++++
 .../02-multiple-goroutine/two-goroutines.jpg  | Bin 0 -> 57810 bytes
 3 files changed, 81 insertions(+)
 create mode 100644 15-goroutines/01-create-goroutine/main.go
 create mode 100644 15-goroutines/02-multiple-goroutine/main.go
 create mode 100644 15-goroutines/02-multiple-goroutine/two-goroutines.jpg

diff --git a/15-goroutines/01-create-goroutine/main.go b/15-goroutines/01-create-goroutine/main.go
new file mode 100644
index 0000000..2370990
--- /dev/null
+++ b/15-goroutines/01-create-goroutine/main.go
@@ -0,0 +1,31 @@
+package main
+
+import (
+	"fmt"
+	"time"
+)
+
+func display(str string) {
+	for i := 0; i < 5; i++ {
+		// Uncomment to disable sleep in goroutine, we wouldn't see "Welcome", because it executes instantly
+		time.Sleep(100 * time.Microsecond)
+
+		fmt.Println(str)
+	}
+}
+
+func main() {
+	fmt.Println(time.Microsecond)
+	// Calling Goroutine with `go` keyword
+	go display("Welcome")
+
+	// Calling normal function
+	display("Hello")
+
+	// Advangate of Goroutine
+	// 1. cheaper than threads
+	// 2. are stored in stack and its size can grow and shrink correspondingly, whereas stack of thread fixed.
+	// 3. Goroutines can communicate using the channel and these channels are specially designed to prevent race conditions when accessing shared memory using Goroutines
+	// 4. Suppose a program has one thread, and that thread has many Goroutines associated with it. If any of Goroutine blocks the thread due to resource requirement then all the remaining Goroutines will assign to a newly created OS thread. All these details are hidden from the programmers.
+
+}
diff --git a/15-goroutines/02-multiple-goroutine/main.go b/15-goroutines/02-multiple-goroutine/main.go
new file mode 100644
index 0000000..14231a1
--- /dev/null
+++ b/15-goroutines/02-multiple-goroutine/main.go
@@ -0,0 +1,50 @@
+package main
+
+import (
+	"fmt"
+	"time"
+)
+
+// For goroutine 1
+func Aname() {
+
+	arr1 := [4]string{"Rohit", "Suman", "Aman", "Ria"}
+
+	for t1 := 0; t1 <= 3; t1++ {
+
+		time.Sleep(150 * time.Millisecond)
+		fmt.Printf("%s\n", arr1[t1])
+	}
+}
+
+// For goroutine 2
+func Aid() {
+
+	arr2 := [4]int{300, 301, 302, 303}
+
+	for t2 := 0; t2 <= 3; t2++ {
+
+		time.Sleep(500 * time.Millisecond)
+		fmt.Printf("%d\n", arr2[t2])
+	}
+}
+
+// Main function
+func main() {
+
+	fmt.Println("!...Main Go-routine Start...!")
+
+	// calling Goroutine 1
+	go Aname()
+
+	// calling Goroutine 2
+	go Aid()
+
+	// The output is fixed, see `two-goroutines.jpg` for the reason.
+	time.Sleep(3500 * time.Millisecond)
+
+	// When sleep for 1800ms, not "303" output
+	// time.Sleep(1800 * time.Millisecond)
+	fmt.Println("\n!...Main Go-routine End...!")
+
+}
diff --git a/15-goroutines/02-multiple-goroutine/two-goroutines.jpg b/15-goroutines/02-multiple-goroutine/two-goroutines.jpg
new file mode 100644
index 0000000000000000000000000000000000000000..48a29fc78916d879f5ad12e28e69038d347d9161
GIT binary patch
