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) } diff --git a/examples/module1/interface/main.go b/examples/module1/interface/main.go index d81e76d..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 } @@ -32,7 +46,7 @@ func (c *Car) getName() string { } func main() { - interfaces := []IF{} + var interfaces []IF h := new(Human) h.firstName = "first" h.lastName = "last" @@ -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) } 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) } 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=