Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More graceful for empty interface slice declaration #6

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions examples/module1/callbacks/main.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 1 addition & 4 deletions examples/module1/callbacks/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
23 changes: 22 additions & 1 deletion examples/module1/interface/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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"
Expand All @@ -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)
}
5 changes: 3 additions & 2 deletions examples/module1/slice/makenew/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package main

import (
"fmt"
"reflect"
)

func main() {
mySlice1 := new([]int)
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)
}
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=