diff --git a/algorithm/sorter_test.go b/algorithm/sorter_test.go index fec7c00..3aa6019 100644 --- a/algorithm/sorter_test.go +++ b/algorithm/sorter_test.go @@ -17,7 +17,7 @@ type people struct { type peopleAgeComparator struct{} // Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator -func (pc *peopleAgeComparator) Compare(v1 interface{}, v2 interface{}) int { +func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int { p1, _ := v1.(people) p2, _ := v2.(people) @@ -47,7 +47,7 @@ var peoples = []people{ type intComparator struct{} -func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { +func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) diff --git a/convertor/convertor.go b/convertor/convertor.go index 9d03bbe..9acf416 100644 --- a/convertor/convertor.go +++ b/convertor/convertor.go @@ -21,7 +21,7 @@ func ToBool(s string) (bool, error) { } // ToBytes convert interface to bytes -func ToBytes(data interface{}) ([]byte, error) { +func ToBytes(data any) ([]byte, error) { var buf bytes.Buffer enc := gob.NewEncoder(&buf) err := enc.Encode(data) @@ -44,7 +44,7 @@ func ToChar(s string) []string { } // ToString convert value to string -func ToString(value interface{}) string { +func ToString(value any) string { res := "" if value == nil { return res @@ -76,7 +76,7 @@ func ToString(value interface{}) string { } // ToJson convert value to a valid json string -func ToJson(value interface{}) (string, error) { +func ToJson(value any) (string, error) { res, err := json.Marshal(value) if err != nil { return "", err @@ -86,7 +86,7 @@ func ToJson(value interface{}) (string, error) { } // ToFloat convert value to a float64, if input is not a float return 0.0 and error -func ToFloat(value interface{}) (float64, error) { +func ToFloat(value any) (float64, error) { v := reflect.ValueOf(value) res := 0.0 @@ -113,7 +113,7 @@ func ToFloat(value interface{}) (float64, error) { } // ToInt convert value to a int64, if input is not a numeric format return 0 and error -func ToInt(value interface{}) (int64, error) { +func ToInt(value any) (int64, error) { v := reflect.ValueOf(value) var res int64 @@ -141,7 +141,7 @@ func ToInt(value interface{}) (int64, error) { // StructToMap convert struct to map, only convert exported struct field // map key is specified same as struct field tag `json` value -func StructToMap(value interface{}) (map[string]interface{}, error) { +func StructToMap(value any) (map[string]any, error) { v := reflect.ValueOf(value) t := reflect.TypeOf(value) @@ -152,7 +152,7 @@ func StructToMap(value interface{}) (map[string]interface{}, error) { return nil, fmt.Errorf("data type %T not support, shuld be struct or pointer to struct", value) } - res := make(map[string]interface{}) + res := make(map[string]any) fieldNum := t.NumField() pattern := `^[A-Z]` diff --git a/convertor/convertor_test.go b/convertor/convertor_test.go index 90b0d6d..3501438 100644 --- a/convertor/convertor_test.go +++ b/convertor/convertor_test.go @@ -36,7 +36,7 @@ func TestToBool(t *testing.T) { func TestToBytes(t *testing.T) { assert := internal.NewAssert(t, "TestToBytes") - cases := []interface{}{ + cases := []any{ 0, false, "1", @@ -55,7 +55,7 @@ func TestToBytes(t *testing.T) { func TestToInt(t *testing.T) { assert := internal.NewAssert(t, "TestToInt") - cases := []interface{}{"123", "-123", 123, + cases := []any{"123", "-123", 123, uint(123), uint8(123), uint16(123), uint32(123), uint64(123), float32(12.3), float64(12.3), "abc", false, "111111111111111111111111111111111111111"} @@ -71,7 +71,7 @@ func TestToInt(t *testing.T) { func TestToFloat(t *testing.T) { assert := internal.NewAssert(t, "TestToFloat") - cases := []interface{}{ + cases := []any{ "", "-1", "-.11", "1.23e3", ".123e10", "abc", int(0), int8(1), int16(-1), int32(123), int64(123), uint(123), uint8(123), uint16(123), uint32(123), uint64(123), @@ -99,7 +99,7 @@ func TestToString(t *testing.T) { } aStruct := TestStruct{Name: "TestStruct"} - cases := []interface{}{ + cases := []any{ "", nil, int(0), int8(1), int16(-1), int32(123), int64(123), uint(123), uint8(123), uint16(123), uint32(123), uint64(123), @@ -147,7 +147,7 @@ func TestStructToMap(t *testing.T) { 100, } pm, _ := StructToMap(p) - var expected = map[string]interface{}{"name": "test"} + var expected = map[string]any{"name": "test"} assert.Equal(expected, pm) } diff --git a/docs/algorithm.md b/docs/algorithm.md index e71b0c8..dfc09b6 100644 --- a/docs/algorithm.md +++ b/docs/algorithm.md @@ -61,7 +61,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) @@ -113,7 +113,7 @@ func main() { type peopleAgeComparator struct{} // Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator - func (pc *peopleAgeComparator) Compare(v1 interface{}, v2 interface{}) int { + func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int { p1, _ := v1.(people) p2, _ := v2.(people) @@ -171,7 +171,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) @@ -216,7 +216,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) @@ -261,7 +261,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) @@ -306,7 +306,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) @@ -351,7 +351,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) @@ -395,7 +395,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) @@ -440,7 +440,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) @@ -486,7 +486,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) @@ -533,7 +533,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) diff --git a/docs/algorithm_zh-CN.md b/docs/algorithm_zh-CN.md index 8283391..b9abd81 100644 --- a/docs/algorithm_zh-CN.md +++ b/docs/algorithm_zh-CN.md @@ -61,7 +61,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) @@ -113,7 +113,7 @@ func main() { type peopleAgeComparator struct{} // Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator - func (pc *peopleAgeComparator) Compare(v1 interface{}, v2 interface{}) int { + func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int { p1, _ := v1.(people) p2, _ := v2.(people) @@ -171,7 +171,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) @@ -216,7 +216,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) @@ -261,7 +261,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) @@ -306,7 +306,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) @@ -351,7 +351,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) @@ -395,7 +395,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) @@ -440,7 +440,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) @@ -486,7 +486,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) @@ -533,7 +533,7 @@ import ( func main() { type intComparator struct{} - func (c *intComparator) Compare(v1 interface{}, v2 interface{}) int { + func (c *intComparator) Compare(v1 any, v2 any) int { val1, _ := v1.(int) val2, _ := v2.(int) diff --git a/docs/convertor.md b/docs/convertor.md index 8229fae..0383219 100644 --- a/docs/convertor.md +++ b/docs/convertor.md @@ -136,7 +136,7 @@ func main() { Signature: ```go -func ToBytes(data interface{}) ([]byte, error) +func ToBytes(data any) ([]byte, error) ``` Example: @@ -199,7 +199,7 @@ func main() { Signature: ```go -func ToFloat(value interface{}) (float64, error) +func ToFloat(value any) (float64, error) ``` Example: @@ -232,7 +232,7 @@ func main() { Signature: ```go -func ToInt(value interface{}) (int64, error) +func ToInt(value any) (int64, error) ``` Example: @@ -265,7 +265,7 @@ func main() { Signature: ```go -func ToJson(value interface{}) (string, error) +func ToJson(value any) (string, error) ``` Example: @@ -293,7 +293,7 @@ func main() { Signature: ```go -func ToString(value interface{}) string +func ToString(value any) string ``` Example: @@ -321,7 +321,7 @@ func main() { Signature: ```go -func StructToMap(value interface{}) (map[string]interface{}, error) +func StructToMap(value any) (map[string]any, error) ``` Example: diff --git a/docs/convertor_zh-CN.md b/docs/convertor_zh-CN.md index 704b471..6f5e7d7 100644 --- a/docs/convertor_zh-CN.md +++ b/docs/convertor_zh-CN.md @@ -138,7 +138,7 @@ func main() { 函数签名: ```go -func ToBytes(data interface{}) ([]byte, error) +func ToBytes(data any) ([]byte, error) ``` 列子: @@ -201,7 +201,7 @@ func main() { 函数签名: ```go -func ToFloat(value interface{}) (float64, error) +func ToFloat(value any) (float64, error) ``` 列子: @@ -234,7 +234,7 @@ func main() { 函数签名: ```go -func ToInt(value interface{}) (int64, error) +func ToInt(value any) (int64, error) ``` 例子: @@ -267,7 +267,7 @@ func main() { 函数签名: ```go -func ToJson(value interface{}) (string, error) +func ToJson(value any) (string, error) ``` 列子: @@ -295,7 +295,7 @@ func main() { 函数签名: ```go -func ToString(value interface{}) string +func ToString(value any) string ``` 例子: @@ -323,7 +323,7 @@ func main() { 函数签名: ```go -func StructToMap(value interface{}) (map[string]interface{}, error) +func StructToMap(value any) (map[string]any, error) ``` 列子: diff --git a/docs/fileutil.md b/docs/fileutil.md index c53a57f..19d6035 100644 --- a/docs/fileutil.md +++ b/docs/fileutil.md @@ -155,7 +155,7 @@ func main() { Signature: ```go -func MiMeType(file interface{}) string +func MiMeType(file any) string ``` Example: diff --git a/docs/fileutil_zh-CN.md b/docs/fileutil_zh-CN.md index 2a693fd..1a2b70d 100644 --- a/docs/fileutil_zh-CN.md +++ b/docs/fileutil_zh-CN.md @@ -155,7 +155,7 @@ func main() { 函数签名: ```go -func MiMeType(file interface{}) string +func MiMeType(file any) string ``` 例子: diff --git a/docs/formatter.md b/docs/formatter.md index 97b8518..64dd772 100644 --- a/docs/formatter.md +++ b/docs/formatter.md @@ -34,7 +34,7 @@ Param should be number or numberic string.
Signature: ```go -func Comma(v interface{}, symbol string) string +func Comma(v any, symbol string) string ``` Example: diff --git a/docs/formatter_zh-CN.md b/docs/formatter_zh-CN.md index b8f1408..971fde4 100644 --- a/docs/formatter_zh-CN.md +++ b/docs/formatter_zh-CN.md @@ -33,7 +33,7 @@ import ( 函数签名: ```go -func Comma(v interface{}, symbol string) string +func Comma(v any, symbol string) string ``` 例子: diff --git a/docs/function.md b/docs/function.md index 93def3f..1b70703 100644 --- a/docs/function.md +++ b/docs/function.md @@ -40,7 +40,7 @@ import ( Signature: ```go -func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value +func After(n int, fn any) func(args ...any) []reflect.Value ``` Example: @@ -59,7 +59,7 @@ func main() { return i }) - type cb func(args ...interface{}) []reflect.Value + type cb func(args ...any) []reflect.Value print := func(i int, s string, fn cb) { fmt.Printf("arr[%d] is %s \n", i, s) fn(i) @@ -87,7 +87,7 @@ func main() { Signature: ```go -func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value +func Before(n int, fn any) func(args ...any) []reflect.Value ``` Example: @@ -109,7 +109,7 @@ func main() { }) var res []int64 - type cb func(args ...interface{}) []reflect.Value + type cb func(args ...any) []reflect.Value appendStr := func(i int, s string, fn cb) { v := fn(i) res = append(res, v[0].Int()) @@ -133,8 +133,8 @@ func main() { Signature: ```go -type Fn func(...interface{}) interface{} -func (f Fn) Curry(i interface{}) func(...interface{}) interface{} +type Fn func(...any) any +func (f Fn) Curry(i any) func(...any) any ``` Example: @@ -150,7 +150,7 @@ func main() { add := func(a, b int) int { return a + b } - var addCurry function.Fn = func(values ...interface{}) interface{} { + var addCurry function.Fn = func(values ...any) any { return add(values[0].(int), values[1].(int)) } add1 := addCurry.Curry(1) @@ -168,7 +168,7 @@ func main() { Signature: ```go -func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) interface{} +func Compose(fnList ...func(...any) any) func(...any) any ``` Example: @@ -181,10 +181,10 @@ import ( ) func main() { - add1 := func(v ...interface{}) interface{} { + add1 := func(v ...any) any { return v[0].(int) + 1 } - add2 := func(v ...interface{}) interface{} { + add2 := func(v ...any) any { return v[0].(int) + 2 } @@ -246,7 +246,7 @@ func main() { Signature: ```go -func Delay(delay time.Duration, fn interface{}, args ...interface{}) +func Delay(delay time.Duration, fn any, args ...any) ``` Example: @@ -275,7 +275,7 @@ func main() { Signature: ```go -func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool +func Schedule(d time.Duration, fn any, args ...any) chan bool ``` Example: diff --git a/docs/function_zh-CN.md b/docs/function_zh-CN.md index 4d68a86..caa2016 100644 --- a/docs/function_zh-CN.md +++ b/docs/function_zh-CN.md @@ -40,7 +40,7 @@ import ( 函数签名: ```go -func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value +func After(n int, fn any) func(args ...any) []reflect.Value ``` 例子: @@ -59,7 +59,7 @@ func main() { return i }) - type cb func(args ...interface{}) []reflect.Value + type cb func(args ...any) []reflect.Value print := func(i int, s string, fn cb) { fmt.Printf("arr[%d] is %s \n", i, s) fn(i) @@ -87,7 +87,7 @@ func main() { 函数签名: ```go -func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value +func Before(n int, fn any) func(args ...any) []reflect.Value ``` 例子: @@ -109,7 +109,7 @@ func main() { }) var res []int64 - type cb func(args ...interface{}) []reflect.Value + type cb func(args ...any) []reflect.Value appendStr := func(i int, s string, fn cb) { v := fn(i) res = append(res, v[0].Int()) @@ -133,8 +133,8 @@ func main() { 函数签名: ```go -type Fn func(...interface{}) interface{} -func (f Fn) Curry(i interface{}) func(...interface{}) interface{} +type Fn func(...any) any +func (f Fn) Curry(i any) func(...any) any ``` 例子: @@ -150,7 +150,7 @@ func main() { add := func(a, b int) int { return a + b } - var addCurry function.Fn = func(values ...interface{}) interface{} { + var addCurry function.Fn = func(values ...any) any { return add(values[0].(int), values[1].(int)) } add1 := addCurry.Curry(1) @@ -168,7 +168,7 @@ func main() { 函数签名: ```go -func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) interface{} +func Compose(fnList ...func(...any) any) func(...any) any ``` 例子: @@ -181,10 +181,10 @@ import ( ) func main() { - add1 := func(v ...interface{}) interface{} { + add1 := func(v ...any) any { return v[0].(int) + 1 } - add2 := func(v ...interface{}) interface{} { + add2 := func(v ...any) any { return v[0].(int) + 2 } @@ -246,7 +246,7 @@ func main() { 函数签名: ```go -func Delay(delay time.Duration, fn interface{}, args ...interface{}) +func Delay(delay time.Duration, fn any, args ...any) ``` 例子: @@ -275,7 +275,7 @@ func main() { 函数签名: ```go -func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool +func Schedule(d time.Duration, fn any, args ...any) chan bool ``` 例子: diff --git a/docs/netutil.md b/docs/netutil.md index 98a1054..0d020dc 100644 --- a/docs/netutil.md +++ b/docs/netutil.md @@ -46,7 +46,7 @@ import ( Signature: ```go -func ConvertMapToQueryString(param map[string]interface{}) string +func ConvertMapToQueryString(param map[string]any) string ``` Example: @@ -59,7 +59,7 @@ import ( ) func main() { - var m = map[string]interface{}{ + var m = map[string]any{ "c": 3, "a": 1, "b": 2, @@ -237,10 +237,10 @@ func main() { ```go // params[0] is header which type should be http.Header or map[string]string, -// params[1] is query param which type should be url.Values or map[string]interface{}, +// params[1] is query param which type should be url.Values or map[string]any, // params[2] is post body which type should be []byte. // params[3] is http client which type should be http.Client. -func HttpGet(url string, params ...interface{}) (*http.Response, error) +func HttpGet(url string, params ...any) (*http.Response, error) ``` Example: @@ -279,10 +279,10 @@ func main() { ```go // params[0] is header which type should be http.Header or map[string]string, -// params[1] is query param which type should be url.Values or map[string]interface{}, +// params[1] is query param which type should be url.Values or map[string]any, // params[2] is post body which type should be []byte. // params[3] is http client which type should be http.Client. -func HttpPost(url string, params ...interface{}) (*http.Response, error) +func HttpPost(url string, params ...any) (*http.Response, error) ``` Example: @@ -328,10 +328,10 @@ func main() { ```go // params[0] is header which type should be http.Header or map[string]string, -// params[1] is query param which type should be url.Values or map[string]interface{}, +// params[1] is query param which type should be url.Values or map[string]any, // params[2] is post body which type should be []byte. // params[3] is http client which type should be http.Client. -func HttpPut(url string, params ...interface{}) (*http.Response, error) +func HttpPut(url string, params ...any) (*http.Response, error) ``` Example: @@ -378,10 +378,10 @@ func main() { ```go // params[0] is header which type should be http.Header or map[string]string, -// params[1] is query param which type should be url.Values or map[string]interface{}, +// params[1] is query param which type should be url.Values or map[string]any, // params[2] is post body which type should be []byte. // params[3] is http client which type should be http.Client. -func HttpDelete(url string, params ...interface{}) (*http.Response, error) +func HttpDelete(url string, params ...any) (*http.Response, error) ``` Example: @@ -417,10 +417,10 @@ func main() { ```go // params[0] is header which type should be http.Header or map[string]string, -// params[1] is query param which type should be url.Values or map[string]interface{}, +// params[1] is query param which type should be url.Values or map[string]any, // params[2] is post body which type should be []byte. // params[3] is http client which type should be http.Client. -func HttpPatch(url string, params ...interface{}) (*http.Response, error) +func HttpPatch(url string, params ...any) (*http.Response, error) ``` Example: @@ -466,7 +466,7 @@ func main() { Signature: ```go -func ParseHttpResponse(resp *http.Response, obj interface{}) error +func ParseHttpResponse(resp *http.Response, obj any) error ``` Example: diff --git a/docs/netutil_zh-CN.md b/docs/netutil_zh-CN.md index bbca8a3..6bcdbb7 100644 --- a/docs/netutil_zh-CN.md +++ b/docs/netutil_zh-CN.md @@ -45,7 +45,7 @@ import ( 函数签名: ```go -func ConvertMapToQueryString(param map[string]interface{}) string +func ConvertMapToQueryString(param map[string]any) string ``` 例子: @@ -58,7 +58,7 @@ import ( ) func main() { - var m = map[string]interface{}{ + var m = map[string]any{ "c": 3, "a": 1, "b": 2, @@ -235,10 +235,10 @@ func main() { ```go // params[0] http请求header,类型必须是http.Header或者map[string]string -// params[1] http查询字符串,类型必须是url.Values或者map[string]interface{} +// params[1] http查询字符串,类型必须是url.Values或者map[string]any // params[2] post请求体,类型必须是[]byte // params[3] http client,类型必须是http.Client -func HttpGet(url string, params ...interface{}) (*http.Response, error) +func HttpGet(url string, params ...any) (*http.Response, error) ``` 例子: @@ -277,10 +277,10 @@ func main() { ```go // params[0] http请求header,类型必须是http.Header或者map[string]string -// params[1] http查询字符串,类型必须是url.Values或者map[string]interface{} +// params[1] http查询字符串,类型必须是url.Values或者map[string]any // params[2] post请求体,类型必须是[]byte // params[3] http client,类型必须是http.Client -func HttpPost(url string, params ...interface{}) (*http.Response, error) +func HttpPost(url string, params ...any) (*http.Response, error) ``` 例子: @@ -326,10 +326,10 @@ func main() { ```go // params[0] http请求header,类型必须是http.Header或者map[string]string -// params[1] http查询字符串,类型必须是url.Values或者map[string]interface{} +// params[1] http查询字符串,类型必须是url.Values或者map[string]any // params[2] post请求体,类型必须是[]byte // params[3] http client,类型必须是http.Client -func HttpPut(url string, params ...interface{}) (*http.Response, error) +func HttpPut(url string, params ...any) (*http.Response, error) ``` Example: @@ -376,10 +376,10 @@ func main() { ```go // params[0] http请求header,类型必须是http.Header或者map[string]string -// params[1] http查询字符串,类型必须是url.Values或者map[string]interface{} +// params[1] http查询字符串,类型必须是url.Values或者map[string]any // params[2] post请求体,类型必须是[]byte // params[3] http client,类型必须是http.Client -func HttpDelete(url string, params ...interface{}) (*http.Response, error) +func HttpDelete(url string, params ...any) (*http.Response, error) ``` 例子: @@ -415,10 +415,10 @@ func main() { ```go // params[0] http请求header,类型必须是http.Header或者map[string]string -// params[1] http查询字符串,类型必须是url.Values或者map[string]interface{} +// params[1] http查询字符串,类型必须是url.Values或者map[string]any // params[2] post请求体,类型必须是[]byte // params[3] http client,类型必须是http.Client -func HttpPatch(url string, params ...interface{}) (*http.Response, error) +func HttpPatch(url string, params ...any) (*http.Response, error) ``` 例子: @@ -464,7 +464,7 @@ func main() { 函数签名: ```go -func ParseHttpResponse(resp *http.Response, obj interface{}) error +func ParseHttpResponse(resp *http.Response, obj any) error ``` 例子: diff --git a/docs/slice.md b/docs/slice.md index 33eabc3..81e2ccc 100644 --- a/docs/slice.md +++ b/docs/slice.md @@ -126,7 +126,7 @@ import ( func main() { arr := []string{"a", "b", "c", "d", "e"} res := slice.Chunk(InterfaceSlice(arr), 3) - fmt.Println(res) //[][]interface{}{{"a", "b", "c"}, {"d", "e"}} + fmt.Println(res) //[][]any{{"a", "b", "c"}, {"d", "e"}} } ``` @@ -483,7 +483,7 @@ func main() { Signature: ```go -func FlattenDeep(slice interface{}) interface{} +func FlattenDeep(slice any) any ``` Example: @@ -570,7 +570,7 @@ func main() { Signature: ```go -func IntSlice(slice interface{}) []int +func IntSlice(slice any) []int ``` Example: @@ -581,7 +581,7 @@ import ( ) func main() { - var nums = []interface{}{1, 2, 3} + var nums = []any{1, 2, 3} res := slice.IntSlice(nums) fmt.Println(res) //[]int{1, 2, 3} } @@ -596,7 +596,7 @@ func main() { Signature: ```go -func InterfaceSlice(slice interface{}) []interface{} +func InterfaceSlice(slice any) []any ``` Example: @@ -609,7 +609,7 @@ import ( func main() { var nums = []int{}{1, 2, 3} res := slice.InterfaceSlice(nums) - fmt.Println(res) //[]interface{}{1, 2, 3} + fmt.Println(res) //[]any{1, 2, 3} } ``` @@ -650,7 +650,7 @@ func main() { Signature: ```go -func InsertAt[T any](slice []T, index int, value interface{}) []T +func InsertAt[T any](slice []T, index int, value any) []T ``` Example: @@ -788,7 +788,7 @@ func main() { Signature: ```go -func SortByField(slice interface{}, field string, sortType ...string) error +func SortByField(slice any, field string, sortType ...string) error ``` Example: @@ -860,7 +860,7 @@ func main() { Signature: ```go -func StringSlice(slice interface{}) []string +func StringSlice(slice any) []string ``` Example: @@ -871,7 +871,7 @@ import ( ) func main() { - var s = []interface{}{"a", "b", "c"} + var s = []any{"a", "b", "c"} res := slice.StringSlice(s) fmt.Println(res) //[]string{"a", "b", "c"} } diff --git a/docs/slice_zh-CN.md b/docs/slice_zh-CN.md index 7001dc2..aa7d3ea 100644 --- a/docs/slice_zh-CN.md +++ b/docs/slice_zh-CN.md @@ -126,7 +126,7 @@ import ( func main() { arr := []string{"a", "b", "c", "d", "e"} res := slice.Chunk(InterfaceSlice(arr), 3) - fmt.Println(res) //[][]interface{}{{"a", "b", "c"}, {"d", "e"}} + fmt.Println(res) //[][]any{{"a", "b", "c"}, {"d", "e"}} } ``` @@ -360,7 +360,7 @@ func main() { ### Every -如果切片中的所有值都通过谓词函数,则返回true。 函数签名应该是func(index int, value interface{}) bool
+如果切片中的所有值都通过谓词函数,则返回true。 函数签名应该是func(index int, value any) bool
函数签名: @@ -390,7 +390,7 @@ func main() { ### Filter -返回与函数匹配的所有元素。 函数签名应该是 func(index int, value interface{}) bool
+返回与函数匹配的所有元素。 函数签名应该是 func(index int, value any) bool
函数签名: @@ -485,7 +485,7 @@ func main() { 函数签名: ```go -func FlattenDeep(slice interface{}) interface{} +func FlattenDeep(slice any) any ``` 例子: @@ -572,7 +572,7 @@ func main() { 函数签名: ```go -func IntSlice(slice interface{}) []int +func IntSlice(slice any) []int ``` 例子: @@ -583,7 +583,7 @@ import ( ) func main() { - var nums = []interface{}{1, 2, 3} + var nums = []any{1, 2, 3} res := slice.IntSlice(nums) fmt.Println(res) //[]int{1, 2, 3} } @@ -598,7 +598,7 @@ func main() { 函数签名: ```go -func InterfaceSlice(slice interface{}) []interface{} +func InterfaceSlice(slice any) []any ``` 例子: @@ -611,7 +611,7 @@ import ( func main() { var nums = []int{}{1, 2, 3} res := slice.InterfaceSlice(nums) - fmt.Println(res) //[]interface{}{1, 2, 3} + fmt.Println(res) //[]any{1, 2, 3} } ``` @@ -652,7 +652,7 @@ func main() { 函数签名: ```go -func InsertAt[T any](slice []T, index int, value interface{}) []T +func InsertAt[T any](slice []T, index int, value any) []T ``` 例子: @@ -790,7 +790,7 @@ func main() { 函数签名: ```go -func SortByField(slice interface{}, field string, sortType ...string) error +func SortByField(slice any, field string, sortType ...string) error ``` 例子: @@ -862,7 +862,7 @@ func main() { 函数签名: ```go -func StringSlice(slice interface{}) []string +func StringSlice(slice any) []string ``` 例子: @@ -873,7 +873,7 @@ import ( ) func main() { - var s = []interface{}{"a", "b", "c"} + var s = []any{"a", "b", "c"} res := slice.StringSlice(s) fmt.Println(res) //[]string{"a", "b", "c"} } diff --git a/docs/strutil.md b/docs/strutil.md index f8344f8..05b6c0b 100644 --- a/docs/strutil.md +++ b/docs/strutil.md @@ -237,7 +237,7 @@ func main() { Signature: ```go -func IsString(v interface{}) bool +func IsString(v any) bool ``` Example: diff --git a/docs/strutil_zh-CN.md b/docs/strutil_zh-CN.md index f012a05..02dc648 100644 --- a/docs/strutil_zh-CN.md +++ b/docs/strutil_zh-CN.md @@ -238,7 +238,7 @@ func main() { 函数签名: ```go -func IsString(v interface{}) bool +func IsString(v any) bool ``` 例子: diff --git a/fileutil/file.go b/fileutil/file.go index 11812a4..29e1ade 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -263,7 +263,7 @@ func FileMode(path string) (fs.FileMode, error) { // MiMeType return file mime type // param `file` should be string(file path) or *os.File -func MiMeType(file interface{}) string { +func MiMeType(file any) string { var mediatype string readBuffer := func(f *os.File) ([]byte, error) { diff --git a/formatter/formatter.go b/formatter/formatter.go index 9d5687b..eb56dd3 100644 --- a/formatter/formatter.go +++ b/formatter/formatter.go @@ -7,7 +7,7 @@ package formatter import "strings" // Comma add comma to number by every 3 numbers from right. ahead by symbol char -func Comma(v interface{}, symbol string) string { +func Comma(v any, symbol string) string { s := numString(v) dotIndex := strings.Index(s, ".") if dotIndex != -1 { diff --git a/formatter/formatter_internal.go b/formatter/formatter_internal.go index 4928147..7d698a9 100644 --- a/formatter/formatter_internal.go +++ b/formatter/formatter_internal.go @@ -14,7 +14,7 @@ func commaString(s string) string { return commaString(s[:len(s)-3]) + "," + commaString(s[len(s)-3:]) } -func numString(value interface{}) string { +func numString(value any) string { switch reflect.TypeOf(value).Kind() { case reflect.Int, reflect.Int64, reflect.Float32, reflect.Float64: return fmt.Sprintf("%v", value) diff --git a/function/function.go b/function/function.go index 6e2cddb..25ad94b 100644 --- a/function/function.go +++ b/function/function.go @@ -10,11 +10,11 @@ import ( ) // After creates a function that invokes func once it's called n or more times -func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value { +func After(n int, fn any) func(args ...any) []reflect.Value { // Catch programming error while constructing the closure mustBeFunction(fn) - return func(args ...interface{}) []reflect.Value { + return func(args ...any) []reflect.Value { n-- if n < 1 { return unsafeInvokeFunc(fn, args...) @@ -24,11 +24,11 @@ func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value { } // Before creates a function that invokes func once it's called less than n times -func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value { +func Before(n int, fn any) func(args ...any) []reflect.Value { // Catch programming error while constructing the closure mustBeFunction(fn) var res []reflect.Value - return func(args ...interface{}) []reflect.Value { + return func(args ...any) []reflect.Value { if n > 0 { res = unsafeInvokeFunc(fn, args...) } @@ -40,20 +40,20 @@ func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value { } } -// Fn is for curry function which is func(...interface{}) interface{} -type Fn func(...interface{}) interface{} +// Fn is for curry function which is func(...any) any +type Fn func(...any) any // Curry make a curry function -func (f Fn) Curry(i interface{}) func(...interface{}) interface{} { - return func(values ...interface{}) interface{} { - v := append([]interface{}{i}, values...) +func (f Fn) Curry(i any) func(...any) any { + return func(values ...any) any { + v := append([]any{i}, values...) return f(v...) } } // Compose compose the functions from right to left -func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) interface{} { - return func(s ...interface{}) interface{} { +func Compose(fnList ...func(...any) any) func(...any) any { + return func(s ...any) any { f := fnList[0] restFn := fnList[1:] @@ -66,7 +66,7 @@ func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) in } // Delay make the function execution after delayed time -func Delay(delay time.Duration, fn interface{}, args ...interface{}) { +func Delay(delay time.Duration, fn any, args ...any) { // Catch programming error while constructing the closure mustBeFunction(fn) @@ -95,7 +95,7 @@ func Debounced(fn func(), duration time.Duration) func() { } // Schedule invoke function every duration time, util close the returned bool chan -func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool { +func Schedule(d time.Duration, fn any, args ...any) chan bool { // Catch programming error while constructing the closure mustBeFunction(fn) diff --git a/function/function_internal.go b/function/function_internal.go index b819d95..0de3f65 100644 --- a/function/function_internal.go +++ b/function/function_internal.go @@ -5,7 +5,7 @@ import ( "reflect" ) -func invokeFunc(fn interface{}, args ...interface{}) []reflect.Value { +func invokeFunc(fn any, args ...any) []reflect.Value { fv := functionValue(fn) params := make([]reflect.Value, len(args)) for i, item := range args { @@ -14,7 +14,7 @@ func invokeFunc(fn interface{}, args ...interface{}) []reflect.Value { return fv.Call(params) } -func unsafeInvokeFunc(fn interface{}, args ...interface{}) []reflect.Value { +func unsafeInvokeFunc(fn any, args ...any) []reflect.Value { fv := reflect.ValueOf(fn) params := make([]reflect.Value, len(args)) for i, item := range args { @@ -23,7 +23,7 @@ func unsafeInvokeFunc(fn interface{}, args ...interface{}) []reflect.Value { return fv.Call(params) } -func functionValue(function interface{}) reflect.Value { +func functionValue(function any) reflect.Value { v := reflect.ValueOf(function) if v.Kind() != reflect.Func { panic(fmt.Sprintf("Invalid function type, value of type %T", function)) @@ -31,7 +31,7 @@ func functionValue(function interface{}) reflect.Value { return v } -func mustBeFunction(function interface{}) { +func mustBeFunction(function any) { v := reflect.ValueOf(function) if v.Kind() != reflect.Func { panic(fmt.Sprintf("Invalid function type, value of type %T", function)) diff --git a/function/function_test.go b/function/function_test.go index 98f53f5..65f1b3e 100644 --- a/function/function_test.go +++ b/function/function_test.go @@ -16,7 +16,7 @@ func TestAfter(t *testing.T) { fmt.Println("print done") return i }) - type cb func(args ...interface{}) []reflect.Value + type cb func(args ...any) []reflect.Value print := func(i int, s string, fn cb) { fmt.Printf("print: arr[%d] is %s \n", i, s) v := fn(i) @@ -42,7 +42,7 @@ func TestBefore(t *testing.T) { }) var res []int64 - type cb func(args ...interface{}) []reflect.Value + type cb func(args ...any) []reflect.Value appendStr := func(i int, s string, fn cb) { v := fn(i) res = append(res, v[0].Int()) @@ -62,7 +62,7 @@ func TestCurry(t *testing.T) { add := func(a, b int) int { return a + b } - var addCurry Fn = func(values ...interface{}) interface{} { + var addCurry Fn = func(values ...any) any { return add(values[0].(int), values[1].(int)) } add1 := addCurry.Curry(1) @@ -72,10 +72,10 @@ func TestCurry(t *testing.T) { func TestCompose(t *testing.T) { assert := internal.NewAssert(t, "TestCompose") - toUpper := func(a ...interface{}) interface{} { + toUpper := func(a ...any) any { return strings.ToUpper(a[0].(string)) } - toLower := func(a ...interface{}) interface{} { + toLower := func(a ...any) any { return strings.ToLower(a[0].(string)) } diff --git a/internal/assert.go b/internal/assert.go index 3e817d8..6fb5dba 100644 --- a/internal/assert.go +++ b/internal/assert.go @@ -30,14 +30,14 @@ func NewAssert(t *testing.T, caseName string) *Assert { } // Equal check if expected is equal with actual -func (a *Assert) Equal(expected, actual interface{}) { +func (a *Assert) Equal(expected, actual any) { if compare(expected, actual) != compareEqual { makeTestFailed(a.T, a.CaseName, expected, actual) } } // NotEqual check if expected is not equal with actual -func (a *Assert) NotEqual(expected, actual interface{}) { +func (a *Assert) NotEqual(expected, actual any) { if compare(expected, actual) == compareEqual { expectedInfo := fmt.Sprintf("not %v", expected) makeTestFailed(a.T, a.CaseName, expectedInfo, actual) @@ -45,7 +45,7 @@ func (a *Assert) NotEqual(expected, actual interface{}) { } // Greater check if expected is greate than actual -func (a *Assert) Greater(expected, actual interface{}) { +func (a *Assert) Greater(expected, actual any) { if compare(expected, actual) != compareGreater { expectedInfo := fmt.Sprintf("> %v", expected) makeTestFailed(a.T, a.CaseName, expectedInfo, actual) @@ -53,7 +53,7 @@ func (a *Assert) Greater(expected, actual interface{}) { } // GreaterOrEqual check if expected is greate than or equal with actual -func (a *Assert) GreaterOrEqual(expected, actual interface{}) { +func (a *Assert) GreaterOrEqual(expected, actual any) { isGreatOrEqual := compare(expected, actual) == compareGreater || compare(expected, actual) == compareEqual if !isGreatOrEqual { expectedInfo := fmt.Sprintf(">= %v", expected) @@ -62,7 +62,7 @@ func (a *Assert) GreaterOrEqual(expected, actual interface{}) { } // Less check if expected is less than actual -func (a *Assert) Less(expected, actual interface{}) { +func (a *Assert) Less(expected, actual any) { if compare(expected, actual) != compareLess { expectedInfo := fmt.Sprintf("< %v", expected) makeTestFailed(a.T, a.CaseName, expectedInfo, actual) @@ -70,7 +70,7 @@ func (a *Assert) Less(expected, actual interface{}) { } // LessOrEqual check if expected is less than or equal with actual -func (a *Assert) LessOrEqual(expected, actual interface{}) { +func (a *Assert) LessOrEqual(expected, actual any) { isLessOrEqual := compare(expected, actual) == compareLess || compare(expected, actual) == compareEqual if !isLessOrEqual { expectedInfo := fmt.Sprintf("<= %v", expected) @@ -79,14 +79,14 @@ func (a *Assert) LessOrEqual(expected, actual interface{}) { } // IsNil check if value is nil -func (a *Assert) IsNil(value interface{}) { +func (a *Assert) IsNil(value any) { if value != nil { makeTestFailed(a.T, a.CaseName, nil, value) } } // IsNotNil check if value is not nil -func (a *Assert) IsNotNil(value interface{}) { +func (a *Assert) IsNotNil(value any) { if value == nil { makeTestFailed(a.T, a.CaseName, "not nil", value) } @@ -94,7 +94,7 @@ func (a *Assert) IsNotNil(value interface{}) { // compare x and y return : // x > y -> 1, x < y -> -1, x == y -> 0, x != y -> -2 -func compare(x, y interface{}) int { +func compare(x, y any) int { vx := reflect.ValueOf(x) vy := reflect.ValueOf(y) @@ -163,7 +163,7 @@ func compare(x, y interface{}) int { } // logFailedInfo make test failed and log error info -func makeTestFailed(t *testing.T, caseName string, expected, actual interface{}) { +func makeTestFailed(t *testing.T, caseName string, expected, actual any) { _, file, line, _ := runtime.Caller(2) errInfo := fmt.Sprintf("Case %v failed. file: %v, line: %v, expected: %v, actual: %v.", caseName, file, line, expected, actual) t.Error(errInfo) diff --git a/lancetconstraints/constraints.go b/lancetconstraints/constraints.go index b7f7774..b663ba6 100644 --- a/lancetconstraints/constraints.go +++ b/lancetconstraints/constraints.go @@ -9,5 +9,5 @@ type Comparator interface { // Compare v1 and v2 // Ascending order: should return 1 -> v1 > v2, 0 -> v1 = v2, -1 -> v1 < v2 // Descending order: should return 1 -> v1 < v2, 0 -> v1 = v2, -1 -> v1 > v2 - Compare(v1, v2 interface{}) int + Compare(v1, v2 any) int } diff --git a/netutil/http.go b/netutil/http.go index c46c8c0..51656a9 100644 --- a/netutil/http.go +++ b/netutil/http.go @@ -6,7 +6,7 @@ // HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `url` is required. // HttpGet, HttpPost, HttpDelete, HttpPut, HttpPatch, function param `params` is variable, the order is: // params[0] is header which type should be http.Header or map[string]string, -// params[1] is query param which type should be url.Values or map[string]interface{}, +// params[1] is query param which type should be url.Values or map[string]any, // params[2] is post body which type should be []byte. // params[3] is http client which type should be http.Client. package netutil @@ -21,32 +21,32 @@ import ( ) //HttpGet send get http request -func HttpGet(url string, params ...interface{}) (*http.Response, error) { +func HttpGet(url string, params ...any) (*http.Response, error) { return doHttpRequest(http.MethodGet, url, params...) } //HttpPost send post http request -func HttpPost(url string, params ...interface{}) (*http.Response, error) { +func HttpPost(url string, params ...any) (*http.Response, error) { return doHttpRequest(http.MethodPost, url, params...) } //HttpPut send put http request -func HttpPut(url string, params ...interface{}) (*http.Response, error) { +func HttpPut(url string, params ...any) (*http.Response, error) { return doHttpRequest(http.MethodPut, url, params...) } //HttpDelete send delete http request -func HttpDelete(url string, params ...interface{}) (*http.Response, error) { +func HttpDelete(url string, params ...any) (*http.Response, error) { return doHttpRequest(http.MethodDelete, url, params...) } // HttpPatch send patch http request -func HttpPatch(url string, params ...interface{}) (*http.Response, error) { +func HttpPatch(url string, params ...any) (*http.Response, error) { return doHttpRequest(http.MethodPatch, url, params...) } // ParseHttpResponse decode http response to specified interface -func ParseHttpResponse(resp *http.Response, obj interface{}) error { +func ParseHttpResponse(resp *http.Response, obj any) error { if resp == nil { return errors.New("InvalidResp") } @@ -55,7 +55,7 @@ func ParseHttpResponse(resp *http.Response, obj interface{}) error { } // ConvertMapToQueryString convert map to sorted url query string -func ConvertMapToQueryString(param map[string]interface{}) string { +func ConvertMapToQueryString(param map[string]any) string { if param == nil { return "" } diff --git a/netutil/http_test.go b/netutil/http_test.go index 701f998..0ef8b28 100644 --- a/netutil/http_test.go +++ b/netutil/http_test.go @@ -104,7 +104,7 @@ func TestHttpDelete(t *testing.T) { func TestConvertMapToQueryString(t *testing.T) { assert := internal.NewAssert(t, "TestConvertMapToQueryString") - var m = map[string]interface{}{ + var m = map[string]any{ "c": 3, "a": 1, "b": 2, diff --git a/netutil/net_internal.go b/netutil/net_internal.go index f796091..667a47a 100644 --- a/netutil/net_internal.go +++ b/netutil/net_internal.go @@ -10,7 +10,7 @@ import ( "strings" ) -func doHttpRequest(method, reqUrl string, params ...interface{}) (*http.Response, error) { +func doHttpRequest(method, reqUrl string, params ...any) (*http.Response, error) { if len(reqUrl) == 0 { return nil, errors.New("url should be specified") } @@ -60,7 +60,7 @@ func doHttpRequest(method, reqUrl string, params ...interface{}) (*http.Response return resp, e } -func setHeaderAndQueryParam(req *http.Request, reqUrl string, header, queryParam interface{}) error { +func setHeaderAndQueryParam(req *http.Request, reqUrl string, header, queryParam any) error { err := setHeader(req, header) if err != nil { return err @@ -72,7 +72,7 @@ func setHeaderAndQueryParam(req *http.Request, reqUrl string, header, queryParam return nil } -func setHeaderAndQueryAndBody(req *http.Request, reqUrl string, header, queryParam, body interface{}) error { +func setHeaderAndQueryAndBody(req *http.Request, reqUrl string, header, queryParam, body any) error { err := setHeader(req, header) if err != nil { return err @@ -88,7 +88,7 @@ func setHeaderAndQueryAndBody(req *http.Request, reqUrl string, header, queryPar return nil } -func setHeader(req *http.Request, header interface{}) error { +func setHeader(req *http.Request, header any) error { if header != nil { switch v := header.(type) { case map[string]string: @@ -122,11 +122,11 @@ func setUrl(req *http.Request, reqUrl string) error { return nil } -func setQueryParam(req *http.Request, reqUrl string, queryParam interface{}) error { +func setQueryParam(req *http.Request, reqUrl string, queryParam any) error { var values url.Values if queryParam != nil { switch v := queryParam.(type) { - case map[string]interface{}: + case map[string]any: values = url.Values{} for k := range v { values.Set(k, fmt.Sprintf("%v", v[k])) @@ -134,7 +134,7 @@ func setQueryParam(req *http.Request, reqUrl string, queryParam interface{}) err case url.Values: values = v default: - return errors.New("query params type should be url.Values or map[string]interface{}") + return errors.New("query params type should be url.Values or map[string]any") } } @@ -155,7 +155,7 @@ func setQueryParam(req *http.Request, reqUrl string, queryParam interface{}) err return nil } -func setBodyByte(req *http.Request, body interface{}) error { +func setBodyByte(req *http.Request, body any) error { if body != nil { switch b := body.(type) { case []byte: @@ -168,7 +168,7 @@ func setBodyByte(req *http.Request, body interface{}) error { return nil } -func getClient(client interface{}) (*http.Client, error) { +func getClient(client any) (*http.Client, error) { c := http.Client{} if client != nil { switch v := client.(type) { diff --git a/slice/slice.go b/slice/slice.go index 81c88b6..d6919e9 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -306,7 +306,7 @@ func FindLast[T any](slice []T, predicate func(index int, t T) bool) (*T, bool) } // FlattenDeep flattens slice recursive -func FlattenDeep(slice interface{}) interface{} { +func FlattenDeep(slice any) any { sv := sliceValue(slice) st := sliceElemType(sv.Type()) tmp := reflect.MakeSlice(reflect.SliceOf(st), 0, 0) @@ -377,13 +377,13 @@ func Reduce[T any](slice []T, iteratee func(index int, t1, t2 T) T, initial T) T } // InterfaceSlice convert param to slice of interface. -func InterfaceSlice(slice interface{}) []interface{} { +func InterfaceSlice(slice any) []any { sv := sliceValue(slice) if sv.IsNil() { return nil } - res := make([]interface{}, sv.Len()) + res := make([]any, sv.Len()) for i := 0; i < sv.Len(); i++ { res[i] = sv.Index(i).Interface() } @@ -392,7 +392,7 @@ func InterfaceSlice(slice interface{}) []interface{} { } // StringSlice convert param to slice of string. -func StringSlice(slice interface{}) []string { +func StringSlice(slice any) []string { v := sliceValue(slice) out := make([]string, v.Len()) @@ -408,7 +408,7 @@ func StringSlice(slice interface{}) []string { } // IntSlice convert param to slice of int. -func IntSlice(slice interface{}) []int { +func IntSlice(slice any) []int { sv := sliceValue(slice) out := make([]int, sv.Len()) @@ -473,7 +473,7 @@ func Drop[T any](slice []T, n int) []T { } // InsertAt insert the value or other slice into slice at index. -func InsertAt[T any](slice []T, index int, value interface{}) []T { +func InsertAt[T any](slice []T, index int, value any) []T { size := len(slice) if index < 0 || index > size { @@ -608,7 +608,7 @@ func Shuffle[T any](slice []T) []T { // SortByField return sorted slice by field // Slice element should be struct, field type should be int, uint, string, or bool // default sortType is ascending (asc), if descending order, set sortType to desc -func SortByField(slice interface{}, field string, sortType ...string) error { +func SortByField(slice any, field string, sortType ...string) error { sv := sliceValue(slice) t := sv.Type().Elem() diff --git a/slice/slice_internal.go b/slice/slice_internal.go index b3113f1..0c9907d 100644 --- a/slice/slice_internal.go +++ b/slice/slice_internal.go @@ -6,7 +6,7 @@ import ( ) // sliceValue return the reflect value of a slice -func sliceValue(slice interface{}) reflect.Value { +func sliceValue(slice any) reflect.Value { v := reflect.ValueOf(slice) if v.Kind() != reflect.Slice { panic(fmt.Sprintf("Invalid slice type, value of type %T", slice)) @@ -15,7 +15,7 @@ func sliceValue(slice interface{}) reflect.Value { } // functionValue return the reflect value of a function -func functionValue(function interface{}) reflect.Value { +func functionValue(function any) reflect.Value { v := reflect.ValueOf(function) if v.Kind() != reflect.Func { panic(fmt.Sprintf("Invalid function type, value of type %T", function)) diff --git a/slice/slice_test.go b/slice/slice_test.go index 09ae4de..01f27ac 100644 --- a/slice/slice_test.go +++ b/slice/slice_test.go @@ -268,7 +268,7 @@ func TestReduce(t *testing.T) { } func TestIntSlice(t *testing.T) { - var nums []interface{} + var nums []any nums = append(nums, 1, 2, 3) assert := internal.NewAssert(t, "TestIntSlice") @@ -276,7 +276,7 @@ func TestIntSlice(t *testing.T) { } func TestStringSlice(t *testing.T) { - var strs []interface{} + var strs []any strs = append(strs, "a", "b", "c") assert := internal.NewAssert(t, "TestStringSlice") @@ -285,7 +285,7 @@ func TestStringSlice(t *testing.T) { func TestInterfaceSlice(t *testing.T) { strs := []string{"a", "b", "c"} - expect := []interface{}{"a", "b", "c"} + expect := []any{"a", "b", "c"} assert := internal.NewAssert(t, "TestInterfaceSlice") assert.Equal(expect, InterfaceSlice(strs)) @@ -380,7 +380,7 @@ func TestIntersection(t *testing.T) { {1, 2, 3}, {}, } - res := []interface{}{ + res := []any{ Intersection(s1, s2, s3), Intersection(s1, s2), Intersection(s1), diff --git a/strutil/string.go b/strutil/string.go index 268099d..5c0cec1 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -1,4 +1,3 @@ -// Copyright 2021 dudaodong@gmail.com. All rights reserved. // Use of this source code is governed by MIT license // Package strutil implements some functions to manipulate string. @@ -197,7 +196,7 @@ func AfterLast(s, char string) string { } // IsString check if the value data type is string or not. -func IsString(v interface{}) bool { +func IsString(v any) bool { if v == nil { return false }