mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
refactor: interface{} -> any
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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]`
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -136,7 +136,7 @@ func main() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func ToBytes(data interface{}) ([]byte, error)
|
||||
func ToBytes(data any) ([]byte, error)
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -199,7 +199,7 @@ func main() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func ToFloat(value interface{}) (float64, error)
|
||||
func ToFloat(value any) (float64, error)
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -232,7 +232,7 @@ func main() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func ToInt(value interface{}) (int64, error)
|
||||
func ToInt(value any) (int64, error)
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -265,7 +265,7 @@ func main() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func ToJson(value interface{}) (string, error)
|
||||
func ToJson(value any) (string, error)
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -293,7 +293,7 @@ func main() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func ToString(value interface{}) string
|
||||
func ToString(value any) string
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -321,7 +321,7 @@ func main() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func StructToMap(value interface{}) (map[string]interface{}, error)
|
||||
func StructToMap(value any) (map[string]any, error)
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
|
||||
@@ -138,7 +138,7 @@ func main() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func ToBytes(data interface{}) ([]byte, error)
|
||||
func ToBytes(data any) ([]byte, error)
|
||||
```
|
||||
<b>列子:</b>
|
||||
|
||||
@@ -201,7 +201,7 @@ func main() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func ToFloat(value interface{}) (float64, error)
|
||||
func ToFloat(value any) (float64, error)
|
||||
```
|
||||
<b>列子:</b>
|
||||
|
||||
@@ -234,7 +234,7 @@ func main() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func ToInt(value interface{}) (int64, error)
|
||||
func ToInt(value any) (int64, error)
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
@@ -267,7 +267,7 @@ func main() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func ToJson(value interface{}) (string, error)
|
||||
func ToJson(value any) (string, error)
|
||||
```
|
||||
<b>列子:</b>
|
||||
|
||||
@@ -295,7 +295,7 @@ func main() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func ToString(value interface{}) string
|
||||
func ToString(value any) string
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
@@ -323,7 +323,7 @@ func main() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func StructToMap(value interface{}) (map[string]interface{}, error)
|
||||
func StructToMap(value any) (map[string]any, error)
|
||||
```
|
||||
<b>列子:</b>
|
||||
|
||||
|
||||
@@ -155,7 +155,7 @@ func main() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func MiMeType(file interface{}) string
|
||||
func MiMeType(file any) string
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
|
||||
@@ -155,7 +155,7 @@ func main() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func MiMeType(file interface{}) string
|
||||
func MiMeType(file any) string
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ Param should be number or numberic string.</p>
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func Comma(v interface{}, symbol string) string
|
||||
func Comma(v any, symbol string) string
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
|
||||
@@ -33,7 +33,7 @@ import (
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func Comma(v interface{}, symbol string) string
|
||||
func Comma(v any, symbol string) string
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ import (
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
||||
func After(n int, fn any) func(args ...any) []reflect.Value
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -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() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
||||
func Before(n int, fn any) func(args ...any) []reflect.Value
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -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() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```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
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -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() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) interface{}
|
||||
func Compose(fnList ...func(...any) any) func(...any) any
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -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() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func Delay(delay time.Duration, fn interface{}, args ...interface{})
|
||||
func Delay(delay time.Duration, fn any, args ...any)
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -275,7 +275,7 @@ func main() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool
|
||||
func Schedule(d time.Duration, fn any, args ...any) chan bool
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
|
||||
@@ -40,7 +40,7 @@ import (
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
||||
func After(n int, fn any) func(args ...any) []reflect.Value
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
@@ -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() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value
|
||||
func Before(n int, fn any) func(args ...any) []reflect.Value
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
@@ -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() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```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
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
@@ -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() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) interface{}
|
||||
func Compose(fnList ...func(...any) any) func(...any) any
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
@@ -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() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func Delay(delay time.Duration, fn interface{}, args ...interface{})
|
||||
func Delay(delay time.Duration, fn any, args ...any)
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
@@ -275,7 +275,7 @@ func main() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool
|
||||
func Schedule(d time.Duration, fn any, args ...any) chan bool
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ import (
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func ConvertMapToQueryString(param map[string]interface{}) string
|
||||
func ConvertMapToQueryString(param map[string]any) string
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -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)
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -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)
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -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)
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -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)
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -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)
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -466,7 +466,7 @@ func main() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func ParseHttpResponse(resp *http.Response, obj interface{}) error
|
||||
func ParseHttpResponse(resp *http.Response, obj any) error
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ import (
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func ConvertMapToQueryString(param map[string]interface{}) string
|
||||
func ConvertMapToQueryString(param map[string]any) string
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
@@ -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)
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
@@ -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)
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
@@ -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)
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -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)
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
@@ -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)
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
@@ -464,7 +464,7 @@ func main() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func ParseHttpResponse(resp *http.Response, obj interface{}) error
|
||||
func ParseHttpResponse(resp *http.Response, obj any) error
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
|
||||
@@ -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() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func FlattenDeep(slice interface{}) interface{}
|
||||
func FlattenDeep(slice any) any
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -570,7 +570,7 @@ func main() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func IntSlice(slice interface{}) []int
|
||||
func IntSlice(slice any) []int
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -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() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func InterfaceSlice(slice interface{}) []interface{}
|
||||
func InterfaceSlice(slice any) []any
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -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() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func InsertAt[T any](slice []T, index int, value interface{}) []T
|
||||
func InsertAt[T any](slice []T, index int, value any) []T
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -788,7 +788,7 @@ func main() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func SortByField(slice interface{}, field string, sortType ...string) error
|
||||
func SortByField(slice any, field string, sortType ...string) error
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -860,7 +860,7 @@ func main() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func StringSlice(slice interface{}) []string
|
||||
func StringSlice(slice any) []string
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
@@ -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"}
|
||||
}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
|
||||
### <span id="Every">Every</span>
|
||||
<p>如果切片中的所有值都通过谓词函数,则返回true。 函数签名应该是func(index int, value interface{}) bool</p>
|
||||
<p>如果切片中的所有值都通过谓词函数,则返回true。 函数签名应该是func(index int, value any) bool</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
@@ -390,7 +390,7 @@ func main() {
|
||||
|
||||
|
||||
### <span id="Filter">Filter</span>
|
||||
<p>返回与函数匹配的所有元素。 函数签名应该是 func(index int, value interface{}) bool</p>
|
||||
<p>返回与函数匹配的所有元素。 函数签名应该是 func(index int, value any) bool</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
@@ -485,7 +485,7 @@ func main() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func FlattenDeep(slice interface{}) interface{}
|
||||
func FlattenDeep(slice any) any
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
@@ -572,7 +572,7 @@ func main() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func IntSlice(slice interface{}) []int
|
||||
func IntSlice(slice any) []int
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
@@ -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() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func InterfaceSlice(slice interface{}) []interface{}
|
||||
func InterfaceSlice(slice any) []any
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
@@ -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() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func InsertAt[T any](slice []T, index int, value interface{}) []T
|
||||
func InsertAt[T any](slice []T, index int, value any) []T
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
@@ -790,7 +790,7 @@ func main() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func SortByField(slice interface{}, field string, sortType ...string) error
|
||||
func SortByField(slice any, field string, sortType ...string) error
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
@@ -862,7 +862,7 @@ func main() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func StringSlice(slice interface{}) []string
|
||||
func StringSlice(slice any) []string
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
@@ -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"}
|
||||
}
|
||||
|
||||
@@ -237,7 +237,7 @@ func main() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func IsString(v interface{}) bool
|
||||
func IsString(v any) bool
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
|
||||
@@ -238,7 +238,7 @@ func main() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func IsString(v interface{}) bool
|
||||
func IsString(v any) bool
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 ""
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user