From d618f27c2298a9bdd3ff615c7525abb94caf9deb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=B5=B7=E6=88=90?= Date: Mon, 25 Apr 2022 10:09:27 +0800 Subject: [PATCH 1/5] More graceful for empty interface slice declaration --- examples/module1/interface/main.go | 2 +- go.sum | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/module1/interface/main.go b/examples/module1/interface/main.go index d81e76d..445affb 100644 --- a/examples/module1/interface/main.go +++ b/examples/module1/interface/main.go @@ -32,7 +32,7 @@ func (c *Car) getName() string { } func main() { - interfaces := []IF{} + var interfaces []IF h := new(Human) h.firstName = "first" h.lastName = "last" diff --git a/go.sum b/go.sum index 4badd68..ba06ac8 100644 --- a/go.sum +++ b/go.sum @@ -7,6 +7,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 37dfb08d4e4147f158458eb271274a8878361c6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=B5=B7=E6=88=90?= Date: Tue, 26 Apr 2022 15:58:18 +0800 Subject: [PATCH 2/5] TestIncrease should test func increase(int, int) int in main.go --- examples/module1/callbacks/main.go | 10 +++++----- examples/module1/callbacks/main_test.go | 5 +---- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/examples/module1/callbacks/main.go b/examples/module1/callbacks/main.go index d02fdde..b5cef08 100644 --- a/examples/module1/callbacks/main.go +++ b/examples/module1/callbacks/main.go @@ -1,20 +1,20 @@ package main func main() { - var a *int - *a += 1 - // DoOperation(1, increase) + DoOperation(1, increase) DoOperation(1, decrease) } func increase(a, b int) int { + println("increase result is:", a+b) return a + b } -func DoOperation(y int, f func(int, int)) { +func DoOperation(y int, f func(int, int) int) { f(y, 1) } -func decrease(a, b int) { +func decrease(a, b int) int { println("decrease result is:", a-b) + return a - b } diff --git a/examples/module1/callbacks/main_test.go b/examples/module1/callbacks/main_test.go index c1950b7..53d5613 100644 --- a/examples/module1/callbacks/main_test.go +++ b/examples/module1/callbacks/main_test.go @@ -6,11 +6,8 @@ import ( "github.com/stretchr/testify/assert" ) -func add(a, b int) int { - return a + b -} func TestIncrease(t *testing.T) { t.Log("Start testing") - result := add(1, 2) + result := increase(1, 2) assert.Equal(t, result, 3) } From 56b2fc677d9ee1c6b078473d1d2632621bc45ade Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=B5=B7=E6=88=90?= Date: Tue, 26 Apr 2022 16:01:21 +0800 Subject: [PATCH 3/5] Add a Fruit interface demo --- examples/module1/interface/main.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/examples/module1/interface/main.go b/examples/module1/interface/main.go index 445affb..3a23440 100644 --- a/examples/module1/interface/main.go +++ b/examples/module1/interface/main.go @@ -6,6 +6,20 @@ type IF interface { getName() string } +type Fruit interface { + Describe() string +} + +type Apple struct { + Color string + Shape string + Taste string +} + +func (a Apple) Describe() string { + return fmt.Sprintf("Apple: the color is %s, the shape is %s, the taste is %s", a.Color, a.Shape, a.Taste) +} + type Human struct { firstName, lastName string } @@ -48,4 +62,11 @@ func main() { p.vendor = "testVendor" p.model = "testModel" fmt.Println(p.getName()) + + apple := new(Apple) + apple.Color = "Red" + apple.Shape = "Round" + apple.Taste = "Sweet" + description := apple.Describe() + fmt.Print(description) } From 8c587d726a68cf83fa967488f9fbe269ea2194c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=B5=B7=E6=88=90?= Date: Tue, 26 Apr 2022 16:25:51 +0800 Subject: [PATCH 4/5] TestIncrease should test func increase(int, int) int in main.go --- examples/module1/callbacks/main.go | 10 +++++----- examples/module1/callbacks/main_test.go | 5 +---- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/examples/module1/callbacks/main.go b/examples/module1/callbacks/main.go index d02fdde..b5cef08 100644 --- a/examples/module1/callbacks/main.go +++ b/examples/module1/callbacks/main.go @@ -1,20 +1,20 @@ package main func main() { - var a *int - *a += 1 - // DoOperation(1, increase) + DoOperation(1, increase) DoOperation(1, decrease) } func increase(a, b int) int { + println("increase result is:", a+b) return a + b } -func DoOperation(y int, f func(int, int)) { +func DoOperation(y int, f func(int, int) int) { f(y, 1) } -func decrease(a, b int) { +func decrease(a, b int) int { println("decrease result is:", a-b) + return a - b } diff --git a/examples/module1/callbacks/main_test.go b/examples/module1/callbacks/main_test.go index c1950b7..53d5613 100644 --- a/examples/module1/callbacks/main_test.go +++ b/examples/module1/callbacks/main_test.go @@ -6,11 +6,8 @@ import ( "github.com/stretchr/testify/assert" ) -func add(a, b int) int { - return a + b -} func TestIncrease(t *testing.T) { t.Log("Start testing") - result := add(1, 2) + result := increase(1, 2) assert.Equal(t, result, 3) } From 482b3ac77a88180212060843c15a874751961eb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=B5=B7=E6=88=90?= Date: Tue, 26 Apr 2022 16:38:51 +0800 Subject: [PATCH 5/5] new() return a pointer, make() return the real type --- examples/module1/slice/makenew/main.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/examples/module1/slice/makenew/main.go b/examples/module1/slice/makenew/main.go index b4cca47..d773aa3 100644 --- a/examples/module1/slice/makenew/main.go +++ b/examples/module1/slice/makenew/main.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "reflect" ) func main() { @@ -9,8 +10,8 @@ func main() { mySlice2 := make([]int, 0) mySlice3 := make([]int, 10) mySlice4 := make([]int, 10, 20) - fmt.Printf("mySlice1 %+v\n", mySlice1) - fmt.Printf("mySlice2 %+v\n", mySlice2) + fmt.Printf("mySlice1 %+v\n", reflect.TypeOf(mySlice1)) + fmt.Printf("mySlice2 %+v\n", reflect.TypeOf(mySlice2)) fmt.Printf("mySlice3 %+v\n", mySlice3) fmt.Printf("mySlice4 %+v\n", mySlice4) }