diff --git a/convertor/convertor.go b/convertor/convertor.go index 083c4d3..5b995ea 100644 --- a/convertor/convertor.go +++ b/convertor/convertor.go @@ -347,7 +347,7 @@ func DeepClone[T any](src T) T { func CopyProperties[T, U any](dst T, src U) (err error) { defer func() { if e := recover(); e != nil { - err = errors.New(fmt.Sprintf("%v", e)) + err = fmt.Errorf("%v", e) } }() diff --git a/convertor/convertor_example_test.go b/convertor/convertor_example_test.go index 8720e48..a1db8e5 100644 --- a/convertor/convertor_example_test.go +++ b/convertor/convertor_example_test.go @@ -255,12 +255,12 @@ func ExampleDecodeByte() { func ExampleDeepClone() { type Struct struct { - Str string - Int int - Float float64 - Bool bool - Nil interface{} - unexported string + Str string + Int int + Float float64 + Bool bool + Nil interface{} + // unexported string } cases := []interface{}{ @@ -293,7 +293,7 @@ func ExampleDeepClone() { // 1 false // 0.1 false // map[a:1 b:2] false - // &{test 1 0.1 true } false + // &{test 1 0.1 true } false } func ExampleCopyProperties() { @@ -323,12 +323,18 @@ func ExampleCopyProperties() { user := User{Name: "user001", Age: 10, Role: "Admin", Addr: Address{Country: "CN", ZipCode: "001"}, Hobbys: []string{"a", "b"}, salary: 1000} employee1 := Employee{} - CopyProperties(&employee1, &user) + err := CopyProperties(&employee1, &user) + if err != nil { + return + } employee2 := Employee{Name: "employee001", Age: 20, Role: "User", Addr: Address{Country: "UK", ZipCode: "002"}, salary: 500} - CopyProperties(&employee2, &user) + err = CopyProperties(&employee2, &user) + if err != nil { + return + } fmt.Println(employee1) fmt.Println(employee2) diff --git a/convertor/convertor_test.go b/convertor/convertor_test.go index 54bb019..e9fd172 100644 --- a/convertor/convertor_test.go +++ b/convertor/convertor_test.go @@ -259,12 +259,12 @@ func TestDeepClone(t *testing.T) { // assert := internal.NewAssert(t, "TestDeepClone") type Struct struct { - Str string - Int int - Float float64 - Bool bool - Nil interface{} - unexported string + Str string + Int int + Float float64 + Bool bool + Nil interface{} + // unexported string } cases := []interface{}{ diff --git a/datastructure/hashmap/hashmap.go b/datastructure/hashmap/hashmap.go index ff257f4..464cb38 100644 --- a/datastructure/hashmap/hashmap.go +++ b/datastructure/hashmap/hashmap.go @@ -17,7 +17,7 @@ type mapNode struct { next *mapNode } -//HashMap implements a hash map +// HashMap implements a hash map type HashMap struct { capacity uint64 size uint64 diff --git a/retry/retry_example_test.go b/retry/retry_example_test.go index ac1d003..3ecb856 100644 --- a/retry/retry_example_test.go +++ b/retry/retry_example_test.go @@ -19,11 +19,15 @@ func ExampleContext() { return errors.New("error occurs") } - Retry(increaseNumber, + err := Retry(increaseNumber, RetryDuration(time.Microsecond*50), Context(ctx), ) + if err != nil { + return + } + fmt.Println(number) // Output: diff --git a/stream/stream.go b/stream/stream.go index aa13787..cdab636 100644 --- a/stream/stream.go +++ b/stream/stream.go @@ -64,6 +64,7 @@ func Generate[T any](generator func() func() (item T, ok bool)) stream[T] { var zeroValue T for next, item, ok := generator(), zeroValue, true; ok; { + item, ok = next() if ok { source = append(source, item) @@ -98,7 +99,7 @@ func FromRange[T constraints.Integer | constraints.Float](start, end, step T) st } l := int((end-start)/step) + 1 - source := make([]T, l, l) + source := make([]T, l) for i := 0; i < l; i++ { source[i] = start + (T(i) * step) @@ -160,7 +161,7 @@ func (s stream[T]) Filter(predicate func(item T) bool) stream[T] { // Map returns a stream consisting of the elements of this stream that apply the given function to elements of stream. func (s stream[T]) Map(mapper func(item T) T) stream[T] { - source := make([]T, s.Count(), s.Count()) + source := make([]T, s.Count()) for i, v := range s.source { source[i] = mapper(v)