diff --git a/README.cn.md b/README.cn.md index fe9e14b..f2d8bd4 100644 --- a/README.cn.md +++ b/README.cn.md @@ -14,10 +14,16 @@ 这是一个基于 Go 语言开发的通用数据类型处理工具类,帮助开发者在业务代码实现中处理常见的数据类型和数据操作。可以让您专注于您的业务代码的实现,而免去处理基本数据类型转换和验证的功能。该工具库无侵入式的设计可以让您的业务代码更容易阅读和优雅。 +## 公告 + +如果您的go版本小于v1.18,那么您应该使用 [v1.1.0](https://github.com/jefferyjob/go-easy-utils/tree/v1.1.0) 版本。如果您的go版本大于等于v1.18版本,那么您应该使用 v2.x.x 版本。 + +该扩展包 v2.0 支持泛型和any。 + ## 快速开始 **安装** ```bash -go get github.com/jefferyjob/go-easy-utils@v1.1.0 +go get -u github.com/jefferyjob/go-easy-utils ``` **使用Demo** ```go @@ -30,7 +36,7 @@ import ( func main() { var slice = []string{"this", "is", "go", "easy", "utils"} - chunkSlice := sliceUtil.ChunkStr(slice, 2) + chunkSlice := sliceUtil.ChunkSlice(slice, 2) fmt.Printf("%v", chunkSlice) } ``` @@ -41,7 +47,7 @@ func main() { ```go // JsonToStruct 将 JSON 字符串解析为指定的结构体指针 -func JsonToStruct(jsonData string, result interface{}) error +func JsonToStruct(jsonData string, result any) error ``` ### ValidUtil 验证工具 @@ -164,150 +170,36 @@ func StrToBytes(v string) []byte ### sliceUtil 切片处理工具 ```go -// Chunk 将一个切片按指定大小分成多个切片,并返回一个包含这些切片的二维切片。 -func Chunk(slice []interface{}, size int) [][]interface{} +// Chunk 把slice分割为新的数组块 +func ChunkSlice[T any](slice []T, size int) [][]T -// ChunkStr 将一个 string 类型的切片切割成指定大小的多个子切片 -func ChunkStr(slice []string, size int) [][]string +// Column 获取slice中某个单一列的值 +func ColumnSlice[T any](slice []T, column string) []any -// ChunkInt 将一个 int 类型的切片切割成指定大小的多个子切片 -func ChunkInt(slice []int, size int) [][]int +// In 判断value是否在slice中 +func InSlice[T comparable](value T, slices []T) bool -// ChunkInt8 将一个 int8 类型的切片切割成指定大小的多个子切片 -func ChunkInt8(slice []int8, size int) [][]int8 +// Is 判断指定值i是否是slice类型 +func IsSlice(slice any) bool -// ChunkInt32 将一个 int32 类型的切片切割成指定大小的多个子切片 -func ChunkInt32(slice []int32, size int) [][]int32 +// Merge 将多个slice合并成一个slice +func MergeSlice[T any](slices ...[]T) []T -// ChunkInt64 将一个 int64 类型的切片切割成指定大小的多个子切片 -func ChunkInt64(slice []int64, size int) [][]int64 +// Sum 对slice中的元素求和 +func SumSlice[T Numeric](slice []T) T -// ChunkUint 将一个 uint 类型的切片切割成指定大小的多个子切片 -func ChunkUint(slice []uint, size int) [][]uint - -// ChunkUint8 将一个 uint8 类型的切片切割成指定大小的多个子切片 -func ChunkUint8(slice []uint8, size int) [][]uint8 - -// ChunkUint16 将一个 uint16 类型的切片切割成指定大小的多个子切片 -func ChunkUint16(slice []uint16, size int) [][]uint16 - -// ChunkUint32 将一个 uint32 类型的切片切分成固定大小的子切片 -func ChunkUint32(slice []uint32, size int) [][]uint32 - -// ChunkUint64 将一个 uint64 类型的切片切分成固定大小的子切片 -func ChunkUint64(slice []uint64, size int) [][]uint64 - -// InSlices 判断指定值value是否在指定的slice中存在 -func InSlices(value interface{}, slices []interface{}) bool - -// InStrSlices 判断指定值value是否在指定的slice中存在 -func InStrSlices(value string, slices []string) bool - -// InIntSlices 判断指定值value是否在指定的slice中存在 -func InIntSlices(value int, slices []int) bool - -// InInt8Slices 判断指定值value是否在指定的slice中存在 -func InInt8Slices(value int8, slices []int8) bool - -// InInt16Slices 判断指定值value是否在指定的slice中存在 -func InInt16Slices(value int16, slices []int16) bool - -// InInt32Slices 判断指定值value是否在指定的slice中存在 -func InInt32Slices(value int32, slices []int32) bool - -// InInt64Slices 判断指定值value是否在指定的slice中存在 -func InInt64Slices(value int64, slices []int64) bool - -// InUintSlices 判断指定值value是否在指定的slice中存在 -func InUintSlices(value uint, slices []uint) bool - -// InUint8Slices 判断指定值value是否在指定的slice中存在 -func InUint8Slices(value uint8, slices []uint8) bool - -// InUint16Slices 判断指定值value是否在指定的slice中存在 -func InUint16Slices(value uint16, slices []uint16) bool - -// InUint32Slices 判断指定值value是否在指定的slice中存在 -func InUint32Slices(value uint32, slices []uint32) bool - -// InUint64Slices 判断指定值value是否在指定的slice中存在 -func InUint64Slices(value uint64, slices []uint64) bool - -// MergeSlices 将多个slice合并成一个slice -func MergeSlices(slices ...[]interface{}) []interface{} - -// MergeStrSlices 将多个slice合并成一个slice -func MergeStrSlices(slices ...[]string) []string - -// MergeIntSlices 将多个slice合并成一个slice -func MergeIntSlices(slices ...[]int) []int - -// MergeInt8Slices 将多个slice合并成一个slice -func MergeInt8Slices(slices ...[]int8) []int8 - -// MergeInt16Slices 将多个slice合并成一个slice -func MergeInt16Slices(slices ...[]int16) []int16 - -// MergeInt32Slices 将多个slice合并成一个slice -func MergeInt32Slices(slices ...[]int32) []int32 - -// MergeInt64Slices 将多个slice合并成一个slice -func MergeInt64Slices(slices ...[]int64) []int64 - -// MergeUintSlices 将多个slice合并成一个slice -func MergeUintSlices(slices ...[]uint) []uint - -// MergeUint8Slices 将多个slice合并成一个slice -func MergeUint8Slices(slices ...[]uint8) []uint8 - -// MergeUint16Slices 将多个slice合并成一个slice -func MergeUint16Slices(slices ...[]uint16) []uint16 - -// MergeUint32Slices 将多个slice合并成一个slice -func MergeUint32Slices(slices ...[]uint32) []uint32 - -// MergeUint64Slices 将多个slice合并成一个slice -func MergeUint64Slices(slices ...[]uint64) []uint64 - -// SumIntSlice 对slice中的元素求和 -func SumIntSlice(slice []int) int - -// SumInt8Slice 对slice中的元素求和 -func SumInt8Slice(slice []int8) int8 - -// SumInt16Slice 对slice中的元素求和 -func SumInt16Slice(slice []int16) int16 - -// SumInt32Slice 对slice中的元素求和 -func SumInt32Slice(slice []int32) int32 - -// SumInt64Slice 对slice中的元素求和 -func SumInt64Slice(slice []int64) int64 - -// SumFloat32Slice 对slice中的元素求和 -func SumFloat32Slice(slice []float32) float32 - -// SumFloat64Slice 对slice中的元素求和 -func SumFloat64Slice(slice []float64) float64 - -// IsSlice 判断指定值i是否是slice类型 -func IsSlice(i interface{}) bool - -// ColumnSlice 获取slice中某个单一列的值 -func ColumnSlice(slice interface{}, column string) ([]interface{}, error) - -// UniqueSlice 移除slice中重复的值 -func UniqueSlice(v interface{}) interface{} +// Unique 移除slice中重复的值 +func UniqueSlice[T comparable](slice []T) []T ``` ### mapUtil map类型处理 ```go // MapValueExists 判断map中的value是否存在 -func MapValueExists(m map[string]interface{}, value interface{}) bool +func MapValueExists[T comparable](m map[string]T, value T) bool // MapKeyExists 判断map中的key是否存在 -func MapKeyExists(m map[string]interface{}, key string) bool +func MapKeyExists(m map[string]any, key string) bool ``` ### intUtil 数值型处理 @@ -336,7 +228,7 @@ func Int64ToString(v int64) string func Float32ToStr(f float32) string // Float64ToStr float64转字符串 -func Float64ToStr(input float64) string +func Float64ToStr(f float64) string // Float32ToFloat64 float32转float64 func Float32ToFloat64(f float32) float64 @@ -376,46 +268,46 @@ func BytesToStr(data []byte) string ```go // AnyToFloat32 将给定的值转换为float32 -func AnyToFloat32(i interface{}) (float32, error) +func AnyToFloat32(i any) (float32, error) // AnyToFloat64 将给定的值转换为float64 -func AnyToFloat64(i interface{}) (float64, error) +func AnyToFloat64(i any) (float64, error) // AnyToInt 将给定的值转换为 int -func AnyToInt(i interface{}) (int, error) +func AnyToInt(i any) (int, error) // AnyToInt8 将给定的值转换为 int8 -func AnyToInt8(i interface{}) (int8, error) +func AnyToInt8(i any) (int8, error) // AnyToInt16 将给定的值转换为 int16 -func AnyToInt16(i interface{}) (int16, error) +func AnyToInt16(i any) (int16, error) // AnyToInt32 将给定的值转换为 int32 -func AnyToInt32(i interface{}) (int32, error) +func AnyToInt32(i any) (int32, error) // AnyToInt64 将给定的值转换为 int64 -func AnyToInt64(i interface{}) (int64, error) +func AnyToInt64(i any) (int64, error) // AnyToStr 任意类型数据转string -func AnyToStr(i interface{}) string +func AnyToStr(i any) string // AnyToUint 将给定的值转换为 uint -func AnyToUint(i interface{}) (uint, error) +func AnyToUint(i any) (uint, error) // AnyToUint8 将给定的值转换为 uint8 -func AnyToUint8(i interface{}) (uint8, error) +func AnyToUint8(i any) (uint8, error) // AnyToUint16 将给定的值转换为 uint16 -func AnyToUint16(i interface{}) (uint16, error) +func AnyToUint16(i any) (uint16, error) // AnyToUint32 将给定的值转换为 uint32 -func AnyToUint32(i interface{}) (uint32, error) +func AnyToUint32(i any) (uint32, error) // AnyToUint64 将给定的值转换为 uint64 -func AnyToUint64(i interface{}) (uint64, error) +func AnyToUint64(i any) (uint64, error) // AnyToBool 将给定的值转换为bool -func AnyToBool(i interface{}) bool +func AnyToBool(i any) bool ``` diff --git a/README.md b/README.md index d7965c7..a695d49 100644 --- a/README.md +++ b/README.md @@ -14,10 +14,16 @@ English | [简体中文](README.cn.md) This is a general data type processing tool class based on Go language, which helps developers process common data types and data operations in business code implementation. It allows you to focus on the implementation of your business code without processing the basic data type conversion and validation functions. The non-intrusive design of the tool library can make your business code easier to read and elegant. +## Notice + +If your go version is less than v1.18, then you should use [v1.1.0](https://github.com/jefferyjob/go-easy-utils/tree/v1.1.0) version. If your go version is greater than or equal to v1.18, then you should use v2.x.x. + +This extension pack v2.0 supports generics and any. + ## Quick Start **Install** ```bash -go get github.com/jefferyjob/go-easy-utils@v1.1.0 +go get -u github.com/jefferyjob/go-easy-utils ``` **Use Demo** ```go diff --git a/anyUtil/README.md b/anyUtil/README.md index fcfa074..8c9bdf3 100644 --- a/anyUtil/README.md +++ b/anyUtil/README.md @@ -6,48 +6,56 @@ go get -u github.com/jefferyjob/go-easy-utils/anyUtil ``` +## Import + +```go +import ( + "github.com/jefferyjob/go-easy-utils/anyUtil" +) +``` + ## Functions ```go // AnyToFloat32 将给定的值转换为float32 -func AnyToFloat32(i interface{}) (float32, error) +func AnyToFloat32(i any) (float32, error) // AnyToFloat64 将给定的值转换为float64 -func AnyToFloat64(i interface{}) (float64, error) +func AnyToFloat64(i any) (float64, error) // AnyToInt 将给定的值转换为 int -func AnyToInt(i interface{}) (int, error) +func AnyToInt(i any) (int, error) // AnyToInt8 将给定的值转换为 int8 -func AnyToInt8(i interface{}) (int8, error) +func AnyToInt8(i any) (int8, error) // AnyToInt16 将给定的值转换为 int16 -func AnyToInt16(i interface{}) (int16, error) +func AnyToInt16(i any) (int16, error) // AnyToInt32 将给定的值转换为 int32 -func AnyToInt32(i interface{}) (int32, error) +func AnyToInt32(i any) (int32, error) // AnyToInt64 将给定的值转换为 int64 -func AnyToInt64(i interface{}) (int64, error) +func AnyToInt64(i any) (int64, error) // AnyToStr 任意类型数据转string -func AnyToStr(i interface{}) string +func AnyToStr(i any) string // AnyToUint 将给定的值转换为 uint -func AnyToUint(i interface{}) (uint, error) +func AnyToUint(i any) (uint, error) // AnyToUint8 将给定的值转换为 uint8 -func AnyToUint8(i interface{}) (uint8, error) +func AnyToUint8(i any) (uint8, error) // AnyToUint16 将给定的值转换为 uint16 -func AnyToUint16(i interface{}) (uint16, error) +func AnyToUint16(i any) (uint16, error) // AnyToUint32 将给定的值转换为 uint32 -func AnyToUint32(i interface{}) (uint32, error) +func AnyToUint32(i any) (uint32, error) // AnyToUint64 将给定的值转换为 uint64 -func AnyToUint64(i interface{}) (uint64, error) +func AnyToUint64(i any) (uint64, error) // AnyToBool 将给定的值转换为bool -func AnyToBool(i interface{}) bool +func AnyToBool(i any) bool ``` \ No newline at end of file diff --git a/anyUtil/any_to_bool.go b/anyUtil/any_to_bool.go index 6dfd2be..0721916 100644 --- a/anyUtil/any_to_bool.go +++ b/anyUtil/any_to_bool.go @@ -3,7 +3,7 @@ package anyUtil import "reflect" // AnyToBool 将给定的值转换为bool -func AnyToBool(i interface{}) bool { +func AnyToBool(i any) bool { if i == nil { return false } diff --git a/anyUtil/any_to_bool_test.go b/anyUtil/any_to_bool_test.go index db1ed11..5cf2112 100644 --- a/anyUtil/any_to_bool_test.go +++ b/anyUtil/any_to_bool_test.go @@ -8,7 +8,7 @@ func TestToBool(t *testing.T) { iPtr := 90 var tests = []struct { - input interface{} + input any want bool }{ {true, true}, diff --git a/anyUtil/any_to_float_x.go b/anyUtil/any_to_float_x.go index 7a81c2d..7ef468c 100644 --- a/anyUtil/any_to_float_x.go +++ b/anyUtil/any_to_float_x.go @@ -8,7 +8,7 @@ import ( ) // AnyToFloat32 将给定的值转换为float32 -func AnyToFloat32(i interface{}) (float32, error) { +func AnyToFloat32(i any) (float32, error) { f64, err := AnyToFloat64(i) if err != nil { return 0, err @@ -20,7 +20,7 @@ func AnyToFloat32(i interface{}) (float32, error) { } // AnyToFloat64 将给定的值转换为float64 -func AnyToFloat64(i interface{}) (float64, error) { +func AnyToFloat64(i any) (float64, error) { if i == nil { return 0, nil } diff --git a/anyUtil/any_to_float_x_test.go b/anyUtil/any_to_float_x_test.go index 49ca25c..2edc8b2 100644 --- a/anyUtil/any_to_float_x_test.go +++ b/anyUtil/any_to_float_x_test.go @@ -12,7 +12,7 @@ import ( func TestAnyToFloat32(t *testing.T) { tests := []struct { - input interface{} + input any want float32 err error }{ @@ -55,7 +55,7 @@ func TestAnyToFloat64(t *testing.T) { iPtr := 90 tests := []struct { - input interface{} + input any want float64 err error }{ diff --git a/anyUtil/any_to_int_x.go b/anyUtil/any_to_int_x.go index 6df07b6..d800afc 100644 --- a/anyUtil/any_to_int_x.go +++ b/anyUtil/any_to_int_x.go @@ -8,7 +8,7 @@ import ( ) // AnyToInt 将给定的值转换为 int -func AnyToInt(i interface{}) (int, error) { +func AnyToInt(i any) (int, error) { v, err := AnyToInt64(i) if err != nil { return 0, err @@ -23,7 +23,7 @@ func AnyToInt(i interface{}) (int, error) { } // AnyToInt8 将给定的值转换为 int8 -func AnyToInt8(i interface{}) (int8, error) { +func AnyToInt8(i any) (int8, error) { value, err := AnyToInt64(i) if err != nil { return 0, err @@ -35,7 +35,7 @@ func AnyToInt8(i interface{}) (int8, error) { } // AnyToInt16 将给定的值转换为 int16 -func AnyToInt16(i interface{}) (int16, error) { +func AnyToInt16(i any) (int16, error) { value, err := AnyToInt64(i) if err != nil { return 0, err @@ -47,7 +47,7 @@ func AnyToInt16(i interface{}) (int16, error) { } // AnyToInt32 将给定的值转换为 int32 -func AnyToInt32(i interface{}) (int32, error) { +func AnyToInt32(i any) (int32, error) { value, err := AnyToInt64(i) if err != nil { return 0, err @@ -59,7 +59,7 @@ func AnyToInt32(i interface{}) (int32, error) { } // AnyToInt64 将给定的值转换为 int64 -func AnyToInt64(i interface{}) (int64, error) { +func AnyToInt64(i any) (int64, error) { if i == nil { return 0, nil } diff --git a/anyUtil/any_to_int_x_test.go b/anyUtil/any_to_int_x_test.go index 377e9f1..d8d068b 100644 --- a/anyUtil/any_to_int_x_test.go +++ b/anyUtil/any_to_int_x_test.go @@ -13,7 +13,7 @@ import ( func TestAnyToInt(t *testing.T) { type testCase struct { - input interface{} + input any expected int err error } @@ -63,7 +63,7 @@ func TestAnyToInt8(t *testing.T) { // Test cases testCases := []struct { name string - input interface{} + input any expected int8 err error }{ @@ -142,7 +142,7 @@ func TestAnyToInt8(t *testing.T) { func TestAnyToInt16(t *testing.T) { tests := []struct { - input interface{} + input any want int16 err error }{ @@ -178,7 +178,7 @@ func TestAnyToInt16(t *testing.T) { func TestAnyToInt32(t *testing.T) { testCases := []struct { - input interface{} + input any expectedValue int32 expectedError error }{ @@ -208,7 +208,7 @@ func TestAnyToInt64(t *testing.T) { iPtr := 90 testCases := []struct { - input interface{} + input any expected int64 err bool }{ diff --git a/anyUtil/any_to_string.go b/anyUtil/any_to_string.go index c4b8a67..7703540 100644 --- a/anyUtil/any_to_string.go +++ b/anyUtil/any_to_string.go @@ -7,7 +7,7 @@ import ( ) // AnyToStr 任意类型数据转string -func AnyToStr(i interface{}) string { +func AnyToStr(i any) string { if i == nil { return "" } diff --git a/anyUtil/any_to_string_test.go b/anyUtil/any_to_string_test.go index 36e93bf..4cdcf57 100644 --- a/anyUtil/any_to_string_test.go +++ b/anyUtil/any_to_string_test.go @@ -5,7 +5,7 @@ import "testing" func TestAnyToStr(t *testing.T) { iPtr := 90 testCases := []struct { - input interface{} + input any expected string }{ {"hello", "hello"}, diff --git a/anyUtil/any_to_uint_x.go b/anyUtil/any_to_uint_x.go index 7d17c57..23a71cd 100644 --- a/anyUtil/any_to_uint_x.go +++ b/anyUtil/any_to_uint_x.go @@ -8,7 +8,7 @@ import ( ) // AnyToUint 将给定的值转换为 uint -func AnyToUint(i interface{}) (uint, error) { +func AnyToUint(i any) (uint, error) { v, err := AnyToUint64(i) if err != nil { return 0, err @@ -23,7 +23,7 @@ func AnyToUint(i interface{}) (uint, error) { } // AnyToUint8 将给定的值转换为 uint8 -func AnyToUint8(i interface{}) (uint8, error) { +func AnyToUint8(i any) (uint8, error) { value, err := AnyToUint64(i) if err != nil { return 0, err @@ -35,7 +35,7 @@ func AnyToUint8(i interface{}) (uint8, error) { } // AnyToUint16 将给定的值转换为 uint16 -func AnyToUint16(i interface{}) (uint16, error) { +func AnyToUint16(i any) (uint16, error) { value, err := AnyToUint64(i) if err != nil { return 0, err @@ -47,7 +47,7 @@ func AnyToUint16(i interface{}) (uint16, error) { } // AnyToUint32 将给定的值转换为 uint32 -func AnyToUint32(i interface{}) (uint32, error) { +func AnyToUint32(i any) (uint32, error) { value, err := AnyToUint64(i) if err != nil { return 0, err @@ -59,7 +59,7 @@ func AnyToUint32(i interface{}) (uint32, error) { } // AnyToUint64 将给定的值转换为 uint64 -func AnyToUint64(i interface{}) (uint64, error) { +func AnyToUint64(i any) (uint64, error) { if i == nil { return 0, nil } diff --git a/anyUtil/any_to_uint_x_test.go b/anyUtil/any_to_uint_x_test.go index 286b385..86758da 100644 --- a/anyUtil/any_to_uint_x_test.go +++ b/anyUtil/any_to_uint_x_test.go @@ -9,7 +9,7 @@ import ( func TestAnyToUint(t *testing.T) { // Test cases tests := []struct { - input interface{} + input any output uint err error }{ @@ -33,7 +33,7 @@ func TestAnyToUint(t *testing.T) { func TestAnyToUint8(t *testing.T) { // Test cases tests := []struct { - input interface{} + input any output uint8 err error }{ @@ -57,7 +57,7 @@ func TestAnyToUint8(t *testing.T) { func TestAnyToUint16(t *testing.T) { // Test cases tests := []struct { - input interface{} + input any output uint16 err error }{ @@ -81,7 +81,7 @@ func TestAnyToUint16(t *testing.T) { func TestAnyToUint32(t *testing.T) { // Test cases tests := []struct { - input interface{} + input any output uint32 err error }{ @@ -107,7 +107,7 @@ func TestAnyToUint64(t *testing.T) { tests := []struct { name string - input interface{} + input any want uint64 wantError error }{ diff --git a/byteUtil/README.md b/byteUtil/README.md index d19775d..4abb482 100644 --- a/byteUtil/README.md +++ b/byteUtil/README.md @@ -6,6 +6,14 @@ go get -u github.com/jefferyjob/go-easy-utils/byteUtil ``` +## Import + +```go +import ( + "github.com/jefferyjob/go-easy-utils/byteUtil" +) +``` + ## Functions ```go diff --git a/cryptoUtil/README.md b/cryptoUtil/README.md index e85dcc6..b7a1ff9 100644 --- a/cryptoUtil/README.md +++ b/cryptoUtil/README.md @@ -6,6 +6,14 @@ go get -u github.com/jefferyjob/go-easy-utils/cryptoUtil ``` +## Import + +```go +import ( + "github.com/jefferyjob/go-easy-utils/cryptoUtil" +) +``` + ## Functions ```go diff --git a/emojiUtil/README.md b/emojiUtil/README.md index e7aaa74..235f2ee 100644 --- a/emojiUtil/README.md +++ b/emojiUtil/README.md @@ -6,6 +6,14 @@ go get -u github.com/jefferyjob/go-easy-utils/emojiUtil ``` +## Import + +```go +import ( + "github.com/jefferyjob/go-easy-utils/emojiUtil" +) +``` + ## Functions ```go diff --git a/floatUtil/README.md b/floatUtil/README.md index bb43373..b3ec730 100644 --- a/floatUtil/README.md +++ b/floatUtil/README.md @@ -6,6 +6,14 @@ go get -u github.com/jefferyjob/go-easy-utils/floatUtil ``` +## Import + +```go +import ( + "github.com/jefferyjob/go-easy-utils/floatUtil" +) +``` + ## Functions ```go diff --git a/intUtil/README.md b/intUtil/README.md index 4bb3752..6a713f5 100644 --- a/intUtil/README.md +++ b/intUtil/README.md @@ -6,6 +6,14 @@ go get -u github.com/jefferyjob/go-easy-utils/intUtil ``` +## Import + +```go +import ( + "github.com/jefferyjob/go-easy-utils/intUtil" +) +``` + ## Functions ```go diff --git a/jsonUtil/README.md b/jsonUtil/README.md index 2d8db73..bd94071 100644 --- a/jsonUtil/README.md +++ b/jsonUtil/README.md @@ -33,12 +33,20 @@ JsonToStruct 是一个将JSON字符串解析为结构体的方法。这个方法 go get -u github.com/jefferyjob/go-easy-utils/jsonUtil ``` +## Import + +```go +import ( + "github.com/jefferyjob/go-easy-utils/jsonUtil" +) +``` + ## Functions ```go // JsonToStruct Parses JSON into a specified structure pointer // 将JSON解析为指定的结构体指针 -func JsonToStruct(jsonData string, result interface{}) error +func JsonToStruct(jsonData string, result any) error ``` ## Demo @@ -54,33 +62,19 @@ func TestDemo1(t *testing.T) { "is_use": "1" }` - var people1 struct { - Name string `json:"name,omitempty"` - Age int `json:"age"` - IsUse bool `json:"is_use"` + var people struct { + Name string `json:"name,omitempty"` + Age int `json:"age"` + IsUse bool `json:"is_use"` } - var people2 struct { - Name string `json:"name,omitempty"` - Age string `json:"age"` - IsUse bool `json:"is_use"` + if err := JsonToStruct(jsonData, &people); err != nil { + fmt.Println(err) + return } - - if err := JsonToStruct(jsonData, &people1); err != nil { - fmt.Println(err) - return - } - fmt.Printf("people1:%#v \n", people1) + fmt.Printf("%+v \n", people) // return - // people1:struct { Name string "json:\"name,omitempty\""; Age int "json:\"age\""; IsUse bool "json:\"is_use\"" }{Name:"make", Age:22, IsUse:true} - - if err := JsonToStruct(jsonData, &people2); err != nil { - fmt.Println(err) - return - } - fmt.Printf("people2:%#v \n", people2) - // return - // people2:struct { Name string "json:\"name,omitempty\""; Age string "json:\"age\""; IsUse bool "json:\"is_use\"" }{Name:"make", Age:"22", IsUse:true} + // {Name:make Age:22 IsUse:true} } ``` diff --git a/jsonUtil/json_to_struct.go b/jsonUtil/json_to_struct.go index 1149c10..22254f2 100644 --- a/jsonUtil/json_to_struct.go +++ b/jsonUtil/json_to_struct.go @@ -10,12 +10,12 @@ import ( // JsonToStruct Parses JSON into a specified structure pointer // 将JSON解析为指定的结构体指针 -func JsonToStruct(jsonData string, result interface{}) error { +func JsonToStruct(jsonData string, result any) error { if reflect.ValueOf(result).Kind() != reflect.Pointer || reflect.ValueOf(result).IsNil() { return errors.New("the argument to Result must be a non-nil pointer") } - var data map[string]interface{} + var data map[string]any err := json.Unmarshal([]byte(jsonData), &data) if err != nil { return err @@ -72,7 +72,7 @@ func JsonToStruct(jsonData string, result interface{}) error { val := toBoolReflect(value) fieldValue.SetBool(val) case reflect.Struct: - if subData, ok := value.(map[string]interface{}); ok { + if subData, ok := value.(map[string]any); ok { subResult := reflect.New(fieldValue.Type()) err := JsonToStruct(convertToJSONString(subData), subResult.Interface()) if err != nil { @@ -81,14 +81,14 @@ func JsonToStruct(jsonData string, result interface{}) error { fieldValue.Set(subResult.Elem()) } case reflect.Slice: - if subData, ok := value.([]interface{}); ok { + if subData, ok := value.([]any); ok { subResult := reflect.MakeSlice(fieldValue.Type(), len(subData), len(subData)) for j := 0; j < len(subData); j++ { subValue := subData[j] subElem := subResult.Index(j) if subElem.Kind() == reflect.Struct { - if subDataElem, ok := subValue.(map[string]interface{}); ok { + if subDataElem, ok := subValue.(map[string]any); ok { subResultElem := reflect.New(subElem.Type()) err := JsonToStruct(convertToJSONString(subDataElem), subResultElem.Interface()) if err != nil { @@ -109,7 +109,7 @@ func JsonToStruct(jsonData string, result interface{}) error { return nil } -func convertToJSONString(data map[string]interface{}) string { +func convertToJSONString(data map[string]any) string { jsonBytes, _ := json.Marshal(data) return string(jsonBytes) } diff --git a/jsonUtil/json_to_struct_test.go b/jsonUtil/json_to_struct_test.go index 4cc8170..f267eb1 100644 --- a/jsonUtil/json_to_struct_test.go +++ b/jsonUtil/json_to_struct_test.go @@ -16,33 +16,19 @@ func TestDemo1(t *testing.T) { "is_use": "1" }` - var people1 struct { + var people struct { Name string `json:"name,omitempty"` Age int `json:"age"` IsUse bool `json:"is_use"` } - var people2 struct { - Name string `json:"name,omitempty"` - Age string `json:"age"` - IsUse bool `json:"is_use"` - } - - if err := JsonToStruct(jsonData, &people1); err != nil { + if err := JsonToStruct(jsonData, &people); err != nil { fmt.Println(err) return } - fmt.Printf("people1:%#v \n", people1) + fmt.Printf("%+v \n", people) // return - // people1:struct { Name string "json:\"name,omitempty\""; Age int "json:\"age\""; IsUse bool "json:\"is_use\"" }{Name:"make", Age:22, IsUse:true} - - if err := JsonToStruct(jsonData, &people2); err != nil { - fmt.Println(err) - return - } - fmt.Printf("people2:%#v \n", people2) - // return - // people2:struct { Name string "json:\"name,omitempty\""; Age string "json:\"age\""; IsUse bool "json:\"is_use\"" }{Name:"make", Age:"22", IsUse:true} + // {Name:make Age:22 IsUse:true} } // Structure nesting and slice nesting processing @@ -391,7 +377,7 @@ func TestJsonToStructMoreNest(t *testing.T) { //func TestJsonToStruct6(t *testing.T) { // type Student struct { -// Name interface{} `json:"name,omitempty"` +// Name any `json:"name,omitempty"` // Age int `json:"age,omitempty"` // } // jsonStr4 := `{ diff --git a/jsonUtil/to_bool.go b/jsonUtil/to_bool.go index 752a836..52ebfc1 100644 --- a/jsonUtil/to_bool.go +++ b/jsonUtil/to_bool.go @@ -5,7 +5,7 @@ import ( "reflect" ) -func toBool(i interface{}) bool { +func toBool(i any) bool { if i == nil { return false } @@ -67,7 +67,7 @@ func toBool(i interface{}) bool { } } -func toBoolReflect(i interface{}) bool { +func toBoolReflect(i any) bool { if i == nil { return false } diff --git a/jsonUtil/to_bool_test.go b/jsonUtil/to_bool_test.go index c4de00f..5e3b4cf 100644 --- a/jsonUtil/to_bool_test.go +++ b/jsonUtil/to_bool_test.go @@ -6,7 +6,7 @@ import ( func TestToBool(t *testing.T) { var tests = []struct { - input interface{} + input any want bool }{ {true, true}, diff --git a/jsonUtil/to_float.go b/jsonUtil/to_float.go index e7604b2..5382e2c 100644 --- a/jsonUtil/to_float.go +++ b/jsonUtil/to_float.go @@ -6,7 +6,7 @@ import ( "strconv" ) -func toFloat64(i interface{}) (float64, error) { +func toFloat64(i any) (float64, error) { if i == nil { return 0, nil } @@ -105,7 +105,7 @@ func toFloat64(i interface{}) (float64, error) { } } -func toFloat64Reflect(i interface{}) (float64, error) { +func toFloat64Reflect(i any) (float64, error) { if i == nil { return 0, nil } diff --git a/jsonUtil/to_float_test.go b/jsonUtil/to_float_test.go index 9ef9883..c02f84c 100644 --- a/jsonUtil/to_float_test.go +++ b/jsonUtil/to_float_test.go @@ -7,14 +7,14 @@ import ( ) //func TestFloat(t *testing.T) { -// var data1 interface{} +// var data1 any // data1 = 123 // fmt.Println(toFloat64Reflect(data1)) // // data2 := int16Ptr(80) // fmt.Println(toFloat64Reflect(data2)) // -// data3 := map[interface{}]interface{}{ +// data3 := map[any]any{ // "aaa": "aaa", // } // fmt.Println(toFloat64Reflect(data3)) @@ -22,7 +22,7 @@ import ( func TestToFloat64(t *testing.T) { testCases := []struct { - value interface{} + value any expected float64 expectedErr error }{ @@ -64,7 +64,7 @@ func TestToFloat64(t *testing.T) { func TestToFloat64Pointer(t *testing.T) { testCases := []struct { name string - input interface{} + input any expectedValue float64 expectedError error }{ diff --git a/jsonUtil/to_int.go b/jsonUtil/to_int.go index 1537946..b0010ad 100644 --- a/jsonUtil/to_int.go +++ b/jsonUtil/to_int.go @@ -6,7 +6,7 @@ import ( "strconv" ) -func toInt64(i interface{}) (int64, error) { +func toInt64(i any) (int64, error) { if i == nil { return 0, nil } @@ -115,7 +115,7 @@ func toInt64(i interface{}) (int64, error) { } } -func toInt64Reflect(i interface{}) (int64, error) { +func toInt64Reflect(i any) (int64, error) { if i == nil { return 0, nil } diff --git a/jsonUtil/to_int_test.go b/jsonUtil/to_int_test.go index ae033aa..1c8d572 100644 --- a/jsonUtil/to_int_test.go +++ b/jsonUtil/to_int_test.go @@ -7,7 +7,7 @@ import ( func TestToInt64(t *testing.T) { var tests = []struct { - input interface{} + input any expected int64 err error }{ diff --git a/jsonUtil/to_string.go b/jsonUtil/to_string.go index a99978d..9699893 100644 --- a/jsonUtil/to_string.go +++ b/jsonUtil/to_string.go @@ -6,7 +6,7 @@ import ( "strconv" ) -func toString(i interface{}) string { +func toString(i any) string { if i == nil { return "" } @@ -137,7 +137,7 @@ func toString(i interface{}) string { } } -func toStringReflect(i interface{}) string { +func toStringReflect(i any) string { if i == nil { return "" } diff --git a/jsonUtil/to_string_test.go b/jsonUtil/to_string_test.go index 4530953..da71c6e 100644 --- a/jsonUtil/to_string_test.go +++ b/jsonUtil/to_string_test.go @@ -5,7 +5,7 @@ import "testing" func TestToString(t *testing.T) { tests := []struct { name string - value interface{} + value any want string }{ {"nil", nil, ""}, diff --git a/jsonUtil/to_uint.go b/jsonUtil/to_uint.go index fd3c489..14ae944 100644 --- a/jsonUtil/to_uint.go +++ b/jsonUtil/to_uint.go @@ -6,7 +6,7 @@ import ( "strconv" ) -func toUint64(i interface{}) (uint64, error) { +func toUint64(i any) (uint64, error) { if i == nil { return 0, nil } @@ -159,7 +159,7 @@ func toUint64(i interface{}) (uint64, error) { } } -func toUint64Reflect(i interface{}) (uint64, error) { +func toUint64Reflect(i any) (uint64, error) { if i == nil { return 0, nil } diff --git a/jsonUtil/to_uint_test.go b/jsonUtil/to_uint_test.go index 7af8d84..950b9e8 100644 --- a/jsonUtil/to_uint_test.go +++ b/jsonUtil/to_uint_test.go @@ -8,7 +8,7 @@ import ( func TestToUint64(t *testing.T) { tests := []struct { name string - input interface{} + input any want uint64 wantError error }{ diff --git a/mapUtil/README.md b/mapUtil/README.md index 465ce93..9dca50a 100644 --- a/mapUtil/README.md +++ b/mapUtil/README.md @@ -6,12 +6,20 @@ go get -u github.com/jefferyjob/go-easy-utils/mapUtil ``` +## Import + +```go +import ( + "github.com/jefferyjob/go-easy-utils/mapUtil" +) +``` + ## Functions ```go // MapValueExists 判断map中的value是否存在 -func MapValueExists(m map[string]interface{}, value interface{}) bool +func MapValueExists[T comparable](m map[string]T, value T) bool // MapKeyExists 判断map中的key是否存在 -func MapKeyExists(m map[string]interface{}, key string) bool +func MapKeyExists(m map[string]any, key string) bool ``` \ No newline at end of file diff --git a/mapUtil/map_exists.go b/mapUtil/map_exists.go index 8cc77ae..4826ed4 100644 --- a/mapUtil/map_exists.go +++ b/mapUtil/map_exists.go @@ -1,7 +1,7 @@ package mapUtil // MapValueExists 判断map中的value是否存在 -func MapValueExists(m map[string]interface{}, value interface{}) bool { +func MapValueExists[T comparable](m map[string]T, value T) bool { for _, v := range m { if v == value { return true @@ -11,7 +11,7 @@ func MapValueExists(m map[string]interface{}, value interface{}) bool { } // MapKeyExists 判断map中的key是否存在 -func MapKeyExists(m map[string]interface{}, key string) bool { +func MapKeyExists(m map[string]any, key string) bool { _, exists := m[key] return exists } diff --git a/mapUtil/map_exists_test.go b/mapUtil/map_exists_test.go index efab1ab..6d721d7 100644 --- a/mapUtil/map_exists_test.go +++ b/mapUtil/map_exists_test.go @@ -3,27 +3,23 @@ package mapUtil import "testing" func TestMapValueExists(t *testing.T) { - m := map[string]interface{}{ + m := map[string]string{ "foo": "bar", - "baz": 123, - "qux": []int{1, 2, 3}, + "baz": "123", } if !MapValueExists(m, "bar") { t.Errorf("expected MapValueExists to return true for value 'bar'") } - if !MapValueExists(m, 123) { + if !MapValueExists(m, "123") { t.Errorf("expected MapValueExists to return true for value 123") } if MapValueExists(m, "non-existent") { t.Errorf("expected MapValueExists to return false for value 'non-existent'") } - //if MapValueExists(m, []int{1, 2, 3}) { - // t.Errorf("expected MapValueExists to return false for value []int{1, 2, 3}") - //} } func TestMapKeyExists(t *testing.T) { - m := map[string]interface{}{ + m := map[string]any{ "foo": "bar", "baz": 123, "qux": []int{1, 2, 3}, diff --git a/sliceUtil/README.md b/sliceUtil/README.md index 71e361f..400aa11 100644 --- a/sliceUtil/README.md +++ b/sliceUtil/README.md @@ -6,141 +6,35 @@ go get -u github.com/jefferyjob/go-easy-utils/sliceUtil ``` +## Import + +```go +import ( + "github.com/jefferyjob/go-easy-utils/sliceUtil" +) +``` + ## Functions ```go -// Chunk 将一个切片按指定大小分成多个切片,并返回一个包含这些切片的二维切片。 -func Chunk(slice []interface{}, size int) [][]interface{} +// Chunk 把slice分割为新的数组块 +func ChunkSlice[T any](slice []T, size int) [][]T -// ChunkStr 将一个 string 类型的切片切割成指定大小的多个子切片 -func ChunkStr(slice []string, size int) [][]string +// Column 获取slice中某个单一列的值 +func ColumnSlice[T any](slice []T, column string) []any -// ChunkInt 将一个 int 类型的切片切割成指定大小的多个子切片 -func ChunkInt(slice []int, size int) [][]int +// In 判断value是否在slice中 +func InSlice[T comparable](value T, slices []T) bool -// ChunkInt8 将一个 int8 类型的切片切割成指定大小的多个子切片 -func ChunkInt8(slice []int8, size int) [][]int8 +// Is 判断指定值i是否是slice类型 +func IsSlice(slice any) bool -// ChunkInt32 将一个 int32 类型的切片切割成指定大小的多个子切片 -func ChunkInt32(slice []int32, size int) [][]int32 +// Merge 将多个slice合并成一个slice +func MergeSlice[T any](slices ...[]T) []T -// ChunkInt64 将一个 int64 类型的切片切割成指定大小的多个子切片 -func ChunkInt64(slice []int64, size int) [][]int64 +// Sum 对slice中的元素求和 +func SumSlice[T Numeric](slice []T) T -// ChunkUint 将一个 uint 类型的切片切割成指定大小的多个子切片 -func ChunkUint(slice []uint, size int) [][]uint - -// ChunkUint8 将一个 uint8 类型的切片切割成指定大小的多个子切片 -func ChunkUint8(slice []uint8, size int) [][]uint8 - -// ChunkUint16 将一个 uint16 类型的切片切割成指定大小的多个子切片 -func ChunkUint16(slice []uint16, size int) [][]uint16 - -// ChunkUint32 将一个 uint32 类型的切片切分成固定大小的子切片 -func ChunkUint32(slice []uint32, size int) [][]uint32 - -// ChunkUint64 将一个 uint64 类型的切片切分成固定大小的子切片 -func ChunkUint64(slice []uint64, size int) [][]uint64 - -// InSlices 判断指定值value是否在指定的slice中存在 -func InSlices(value interface{}, slices []interface{}) bool - -// InStrSlices 判断指定值value是否在指定的slice中存在 -func InStrSlices(value string, slices []string) bool - -// InIntSlices 判断指定值value是否在指定的slice中存在 -func InIntSlices(value int, slices []int) bool - -// InInt8Slices 判断指定值value是否在指定的slice中存在 -func InInt8Slices(value int8, slices []int8) bool - -// InInt16Slices 判断指定值value是否在指定的slice中存在 -func InInt16Slices(value int16, slices []int16) bool - -// InInt32Slices 判断指定值value是否在指定的slice中存在 -func InInt32Slices(value int32, slices []int32) bool - -// InInt64Slices 判断指定值value是否在指定的slice中存在 -func InInt64Slices(value int64, slices []int64) bool - -// InUintSlices 判断指定值value是否在指定的slice中存在 -func InUintSlices(value uint, slices []uint) bool - -// InUint8Slices 判断指定值value是否在指定的slice中存在 -func InUint8Slices(value uint8, slices []uint8) bool - -// InUint16Slices 判断指定值value是否在指定的slice中存在 -func InUint16Slices(value uint16, slices []uint16) bool - -// InUint32Slices 判断指定值value是否在指定的slice中存在 -func InUint32Slices(value uint32, slices []uint32) bool - -// InUint64Slices 判断指定值value是否在指定的slice中存在 -func InUint64Slices(value uint64, slices []uint64) bool - -// MergeSlices 将多个slice合并成一个slice -func MergeSlices(slices ...[]interface{}) []interface{} - -// MergeStrSlices 将多个slice合并成一个slice -func MergeStrSlices(slices ...[]string) []string - -// MergeIntSlices 将多个slice合并成一个slice -func MergeIntSlices(slices ...[]int) []int - -// MergeInt8Slices 将多个slice合并成一个slice -func MergeInt8Slices(slices ...[]int8) []int8 - -// MergeInt16Slices 将多个slice合并成一个slice -func MergeInt16Slices(slices ...[]int16) []int16 - -// MergeInt32Slices 将多个slice合并成一个slice -func MergeInt32Slices(slices ...[]int32) []int32 - -// MergeInt64Slices 将多个slice合并成一个slice -func MergeInt64Slices(slices ...[]int64) []int64 - -// MergeUintSlices 将多个slice合并成一个slice -func MergeUintSlices(slices ...[]uint) []uint - -// MergeUint8Slices 将多个slice合并成一个slice -func MergeUint8Slices(slices ...[]uint8) []uint8 - -// MergeUint16Slices 将多个slice合并成一个slice -func MergeUint16Slices(slices ...[]uint16) []uint16 - -// MergeUint32Slices 将多个slice合并成一个slice -func MergeUint32Slices(slices ...[]uint32) []uint32 - -// MergeUint64Slices 将多个slice合并成一个slice -func MergeUint64Slices(slices ...[]uint64) []uint64 - -// SumIntSlice 对slice中的元素求和 -func SumIntSlice(slice []int) int - -// SumInt8Slice 对slice中的元素求和 -func SumInt8Slice(slice []int8) int8 - -// SumInt16Slice 对slice中的元素求和 -func SumInt16Slice(slice []int16) int16 - -// SumInt32Slice 对slice中的元素求和 -func SumInt32Slice(slice []int32) int32 - -// SumInt64Slice 对slice中的元素求和 -func SumInt64Slice(slice []int64) int64 - -// SumFloat32Slice 对slice中的元素求和 -func SumFloat32Slice(slice []float32) float32 - -// SumFloat64Slice 对slice中的元素求和 -func SumFloat64Slice(slice []float64) float64 - -// IsSlice 判断指定值i是否是slice类型 -func IsSlice(slice interface{}) bool - -// ColumnSlice 获取slice中某个单一列的值 -func ColumnSlice(slice interface{}, column string) ([]interface{}, error) - -// UniqueSlice 移除slice中重复的值 -func UniqueSlice(slice interface{}) interface{} +// Unique 移除slice中重复的值 +func UniqueSlice[T comparable](slice []T) []T ``` \ No newline at end of file diff --git a/sliceUtil/chunk_slice.go b/sliceUtil/chunk_slice.go index c05e782..0904128 100644 --- a/sliceUtil/chunk_slice.go +++ b/sliceUtil/chunk_slice.go @@ -1,138 +1,8 @@ package sliceUtil -// Chunk 将一个切片按指定大小分成多个切片,并返回一个包含这些切片的二维切片。 -func Chunk(slice []interface{}, size int) [][]interface{} { - var chunks [][]interface{} - for i := 0; i < len(slice); i += size { - end := i + size - if end > len(slice) { - end = len(slice) - } - chunks = append(chunks, slice[i:end]) - } - return chunks -} - -// ChunkStr 将一个 string 类型的切片切割成指定大小的多个子切片 -func ChunkStr(slice []string, size int) [][]string { - var chunks [][]string - for i := 0; i < len(slice); i += size { - end := i + size - if end > len(slice) { - end = len(slice) - } - chunks = append(chunks, slice[i:end]) - } - return chunks -} - -// ChunkInt 将一个 int 类型的切片切割成指定大小的多个子切片 -func ChunkInt(slice []int, size int) [][]int { - var chunks [][]int - for i := 0; i < len(slice); i += size { - end := i + size - if end > len(slice) { - end = len(slice) - } - chunks = append(chunks, slice[i:end]) - } - return chunks -} - -// ChunkInt8 将一个 int8 类型的切片切割成指定大小的多个子切片 -func ChunkInt8(slice []int8, size int) [][]int8 { - var chunks [][]int8 - for i := 0; i < len(slice); i += size { - end := i + size - if end > len(slice) { - end = len(slice) - } - chunks = append(chunks, slice[i:end]) - } - return chunks -} - -// ChunkInt32 将一个 int32 类型的切片切割成指定大小的多个子切片 -func ChunkInt32(slice []int32, size int) [][]int32 { - var chunks [][]int32 - for i := 0; i < len(slice); i += size { - end := i + size - if end > len(slice) { - end = len(slice) - } - chunks = append(chunks, slice[i:end]) - } - return chunks -} - -// ChunkInt64 将一个 int64 类型的切片切割成指定大小的多个子切片 -func ChunkInt64(slice []int64, size int) [][]int64 { - var chunks [][]int64 - for i := 0; i < len(slice); i += size { - end := i + size - if end > len(slice) { - end = len(slice) - } - chunks = append(chunks, slice[i:end]) - } - return chunks -} - -// ChunkUint 将一个 uint 类型的切片切割成指定大小的多个子切片 -func ChunkUint(slice []uint, size int) [][]uint { - var chunks [][]uint - for i := 0; i < len(slice); i += size { - end := i + size - if end > len(slice) { - end = len(slice) - } - chunks = append(chunks, slice[i:end]) - } - return chunks -} - -// ChunkUint8 将一个 uint8 类型的切片切割成指定大小的多个子切片 -func ChunkUint8(slice []uint8, size int) [][]uint8 { - var chunks [][]uint8 - for i := 0; i < len(slice); i += size { - end := i + size - if end > len(slice) { - end = len(slice) - } - chunks = append(chunks, slice[i:end]) - } - return chunks -} - -// ChunkUint16 将一个 uint16 类型的切片切割成指定大小的多个子切片 -func ChunkUint16(slice []uint16, size int) [][]uint16 { - var chunks [][]uint16 - for i := 0; i < len(slice); i += size { - end := i + size - if end > len(slice) { - end = len(slice) - } - chunks = append(chunks, slice[i:end]) - } - return chunks -} - -// ChunkUint32 将一个 uint32 类型的切片切分成固定大小的子切片 -func ChunkUint32(slice []uint32, size int) [][]uint32 { - var chunks [][]uint32 - for i := 0; i < len(slice); i += size { - end := i + size - if end > len(slice) { - end = len(slice) - } - chunks = append(chunks, slice[i:end]) - } - return chunks -} - -// ChunkUint64 将一个 uint64 类型的切片切分成固定大小的子切片 -func ChunkUint64(slice []uint64, size int) [][]uint64 { - var chunks [][]uint64 +// ChunkSlice 把slice分割为新的数组块 +func ChunkSlice[T any](slice []T, size int) [][]T { + var chunks [][]T for i := 0; i < len(slice); i += size { end := i + size if end > len(slice) { diff --git a/sliceUtil/chunk_slice_test.go b/sliceUtil/chunk_slice_test.go index 517c748..23edcfe 100644 --- a/sliceUtil/chunk_slice_test.go +++ b/sliceUtil/chunk_slice_test.go @@ -8,39 +8,39 @@ import ( func TestChunk(t *testing.T) { tests := []struct { name string - slice []interface{} + slice []any size int - want [][]interface{} + want [][]any }{ { name: "empty slice", - slice: []interface{}{}, + slice: []any{}, size: 3, - want: [][]interface{}{}, + want: [][]any{}, }, { name: "slice with less than chunk size", - slice: []interface{}{1, 2}, + slice: []any{1, 2}, size: 3, - want: [][]interface{}{{1, 2}}, + want: [][]any{{1, 2}}, }, { name: "slice with exact chunk size", - slice: []interface{}{1, 2, 3, 4, 5, 6}, + slice: []any{1, 2, 3, 4, 5, 6}, size: 3, - want: [][]interface{}{{1, 2, 3}, {4, 5, 6}}, + want: [][]any{{1, 2, 3}, {4, 5, 6}}, }, { name: "slice with more than chunk size", - slice: []interface{}{1, 2, 3, 4, 5, 6, 7}, + slice: []any{1, 2, 3, 4, 5, 6, 7}, size: 3, - want: [][]interface{}{{1, 2, 3}, {4, 5, 6}, {7}}, + want: [][]any{{1, 2, 3}, {4, 5, 6}, {7}}, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := Chunk(tt.slice, tt.size) + got := ChunkSlice(tt.slice, tt.size) if len(got) != len(tt.want) { t.Errorf("Chunk() = %+v, want %+v", got, tt.want) return @@ -59,7 +59,7 @@ func TestChunkStr(t *testing.T) { slice := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"} size := 3 expected := [][]string{{"a", "b", "c"}, {"d", "e", "f"}, {"g", "h", "i"}, {"j", "k"}} - result := ChunkStr(slice, size) + result := ChunkSlice(slice, size) if !reflect.DeepEqual(result, expected) { t.Errorf("ChunkStr(%v, %d) = %v; expected %v", slice, size, result, expected) } @@ -69,7 +69,7 @@ func TestChunkInt(t *testing.T) { slice := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} size := 4 expected := [][]int{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}} - result := ChunkInt(slice, size) + result := ChunkSlice(slice, size) if !reflect.DeepEqual(result, expected) { t.Errorf("ChunkInt(%v, %d) = %v; expected %v", slice, size, result, expected) } @@ -79,7 +79,7 @@ func TestChunkInt8(t *testing.T) { slice := []int8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} size := 4 expected := [][]int8{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}} - result := ChunkInt8(slice, size) + result := ChunkSlice(slice, size) if !reflect.DeepEqual(result, expected) { t.Errorf("ChunkInt8(%v, %d) = %v; expected %v", slice, size, result, expected) } @@ -89,7 +89,7 @@ func TestChunkInt32(t *testing.T) { slice := []int32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} size := 4 expected := [][]int32{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}} - result := ChunkInt32(slice, size) + result := ChunkSlice(slice, size) if !reflect.DeepEqual(result, expected) { t.Errorf("ChunkInt32(%v, %d) = %v; expected %v", slice, size, result, expected) } @@ -99,7 +99,7 @@ func TestChunkInt64(t *testing.T) { slice := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} size := 4 expected := [][]int64{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}} - result := ChunkInt64(slice, size) + result := ChunkSlice(slice, size) if !reflect.DeepEqual(result, expected) { t.Errorf("ChunkInt64(%v, %d) = %v; expected %v", slice, size, result, expected) } @@ -109,7 +109,7 @@ func TestChunkUint(t *testing.T) { slice := []uint{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} size := 4 expected := [][]uint{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}} - result := ChunkUint(slice, size) + result := ChunkSlice(slice, size) if !reflect.DeepEqual(result, expected) { t.Errorf("ChunkUint(%v, %d) = %v; expected %v", slice, size, result, expected) } @@ -119,7 +119,7 @@ func TestChunkUint8(t *testing.T) { slice := []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} size := 4 expected := [][]uint8{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}} - result := ChunkUint8(slice, size) + result := ChunkSlice(slice, size) if !reflect.DeepEqual(result, expected) { t.Errorf("ChunkUint8(%v, %d) = %v; expected %v", slice, size, result, expected) } @@ -129,7 +129,7 @@ func TestChunkUint16(t *testing.T) { slice := []uint16{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} size := 4 expected := [][]uint16{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}} - result := ChunkUint16(slice, size) + result := ChunkSlice(slice, size) if !reflect.DeepEqual(result, expected) { t.Errorf("ChunkUint16(%v, %d) = %v; expected %v", slice, size, result, expected) } @@ -139,7 +139,7 @@ func TestChunkUint32(t *testing.T) { slice := []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} size := 4 expected := [][]uint32{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}} - result := ChunkUint32(slice, size) + result := ChunkSlice(slice, size) if !reflect.DeepEqual(result, expected) { t.Errorf("ChunkUint32(%v, %d) = %v; expected %v", slice, size, result, expected) } @@ -149,7 +149,7 @@ func TestChunkUint64(t *testing.T) { slice := []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} size := 4 expected := [][]uint64{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11}} - result := ChunkUint64(slice, size) + result := ChunkSlice(slice, size) if !reflect.DeepEqual(result, expected) { t.Errorf("ChunkUint64(%v, %d) = %v; expected %v", slice, size, result, expected) } diff --git a/sliceUtil/column_slice.go b/sliceUtil/column_slice.go index cecc4b4..1a255c3 100644 --- a/sliceUtil/column_slice.go +++ b/sliceUtil/column_slice.go @@ -1,20 +1,14 @@ package sliceUtil import ( - "errors" "reflect" ) // ColumnSlice 获取slice中某个单一列的值 -func ColumnSlice(slice interface{}, column string) ([]interface{}, error) { +func ColumnSlice[T any](slice []T, column string) []any { // 获取切片的反射值 sliceValue := reflect.ValueOf(slice) - // 判断是否为切片 - if sliceValue.Kind() != reflect.Slice { - return nil, errors.New("not a slice") - } - // 获取切片的长度和元素类型 length := sliceValue.Len() elementType := sliceValue.Type().Elem() @@ -29,11 +23,11 @@ func ColumnSlice(slice interface{}, column string) ([]interface{}, error) { } } if fieldIndex == -1 { - return nil, errors.New("column not found") + return []any{} } // 构造返回值切片 - result := make([]interface{}, length) + result := make([]any, length) // 获取每个元素指定字段的值,并添加到返回值切片中 for i := 0; i < length; i++ { @@ -42,5 +36,5 @@ func ColumnSlice(slice interface{}, column string) ([]interface{}, error) { result[i] = fieldValue.Interface() } - return result, nil + return result } diff --git a/sliceUtil/column_slice_test.go b/sliceUtil/column_slice_test.go index 9cae0a3..da432c1 100644 --- a/sliceUtil/column_slice_test.go +++ b/sliceUtil/column_slice_test.go @@ -1,7 +1,6 @@ package sliceUtil import ( - "errors" "reflect" "testing" ) @@ -18,11 +17,7 @@ import ( // } // // // 获取年龄列 -// ages, err := ColumnSlice(people, "Age") -// if err != nil { -// fmt.Println(err) -// return -// } +// ages := ColumnSlice(people, "Age") // fmt.Println(ages) // 输出:[18 20 22] //} @@ -34,43 +29,30 @@ func TestColumnSlice(t *testing.T) { testCases := []struct { name string - input interface{} + input []Person column string - output []interface{} - err error + output []any }{ { name: "success", input: []Person{{"Alice", 18}, {"Bob", 20}, {"Charlie", 22}}, column: "Age", - output: []interface{}{18, 20, 22}, - err: nil, - }, - { - name: "not a slice", - input: Person{"Alice", 18}, - column: "Age", - output: nil, - err: errors.New("not a slice"), + output: []any{18, 20, 22}, }, { name: "column not found", input: []Person{{"Alice", 18}, {"Bob", 20}, {"Charlie", 22}}, column: "Gender", - output: nil, - err: errors.New("column not found"), + output: []any{}, }, } for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - output, err := ColumnSlice(tc.input, tc.column) + output := ColumnSlice(tc.input, tc.column) if !reflect.DeepEqual(output, tc.output) { t.Errorf("expected %v, but got %v", tc.output, output) } - if !reflect.DeepEqual(err, tc.err) { - t.Errorf("expected %v, but got %v", tc.err, err) - } }) } } diff --git a/sliceUtil/in_slice.go b/sliceUtil/in_slice.go index 1b3e084..23ef1be 100644 --- a/sliceUtil/in_slice.go +++ b/sliceUtil/in_slice.go @@ -1,117 +1,7 @@ package sliceUtil -// InSlices 判断指定值value是否在指定的slice中存在 -func InSlices(value interface{}, slices []interface{}) bool { - for _, v := range slices { - if value == v { - return true - } - } - return false -} - -// InStrSlices 判断指定值value是否在指定的slice中存在 -func InStrSlices(value string, slices []string) bool { - for _, v := range slices { - if value == v { - return true - } - } - return false -} - -// InIntSlices 判断指定值value是否在指定的slice中存在 -func InIntSlices(value int, slices []int) bool { - for _, v := range slices { - if value == v { - return true - } - } - return false -} - -// InInt8Slices 判断指定值value是否在指定的slice中存在 -func InInt8Slices(value int8, slices []int8) bool { - for _, v := range slices { - if value == v { - return true - } - } - return false -} - -// InInt16Slices 判断指定值value是否在指定的slice中存在 -func InInt16Slices(value int16, slices []int16) bool { - for _, v := range slices { - if value == v { - return true - } - } - return false -} - -// InInt32Slices 判断指定值value是否在指定的slice中存在 -func InInt32Slices(value int32, slices []int32) bool { - for _, v := range slices { - if value == v { - return true - } - } - return false -} - -// InInt64Slices 判断指定值value是否在指定的slice中存在 -func InInt64Slices(value int64, slices []int64) bool { - for _, v := range slices { - if value == v { - return true - } - } - return false -} - -// InUintSlices 判断指定值value是否在指定的slice中存在 -func InUintSlices(value uint, slices []uint) bool { - for _, v := range slices { - if value == v { - return true - } - } - return false -} - -// InUint8Slices 判断指定值value是否在指定的slice中存在 -func InUint8Slices(value uint8, slices []uint8) bool { - for _, v := range slices { - if value == v { - return true - } - } - return false -} - -// InUint16Slices 判断指定值value是否在指定的slice中存在 -func InUint16Slices(value uint16, slices []uint16) bool { - for _, v := range slices { - if value == v { - return true - } - } - return false -} - -// InUint32Slices 判断指定值value是否在指定的slice中存在 -func InUint32Slices(value uint32, slices []uint32) bool { - for _, v := range slices { - if value == v { - return true - } - } - return false -} - -// InUint64Slices 判断指定值value是否在指定的slice中存在 -func InUint64Slices(value uint64, slices []uint64) bool { +// InSlice 判断value是否在slice中 +func InSlice[T comparable](value T, slices []T) bool { for _, v := range slices { if value == v { return true diff --git a/sliceUtil/in_slice_test.go b/sliceUtil/in_slice_test.go index 088bff7..1df1382 100644 --- a/sliceUtil/in_slice_test.go +++ b/sliceUtil/in_slice_test.go @@ -4,138 +4,125 @@ import ( "testing" ) -func TestInSlices(t *testing.T) { - slices := []interface{}{1, "hello", 3.14} - if !InSlices(1, slices) { - t.Errorf("1 should be in slices %v", slices) - } - if !InSlices("hello", slices) { - t.Errorf("hello should be in slices %v", slices) - } - if InSlices(2, slices) { - t.Errorf("2 should not be in slices %v", slices) - } -} - func TestInStrSlices(t *testing.T) { slices := []string{"apple", "banana", "cherry"} - if !InStrSlices("apple", slices) { + if !InSlice("apple", slices) { t.Errorf("apple should be in slices %v", slices) } - if !InStrSlices("banana", slices) { + if !InSlice("banana", slices) { t.Errorf("banana should be in slices %v", slices) } - if InStrSlices("orange", slices) { + if InSlice("orange", slices) { t.Errorf("orange should not be in slices %v", slices) } } func TestInIntSlices(t *testing.T) { slices := []int{1, 2, 3, 4, 5} - if !InIntSlices(3, slices) { + if !InSlice(3, slices) { t.Errorf("Expected true, but got false") } - if InIntSlices(6, slices) { + if InSlice(6, slices) { t.Errorf("Expected false, but got true") } } func TestInInt8Slices(t *testing.T) { slices := []int8{1, 2, 3, 4, 5} - if !InInt8Slices(3, slices) { + if !InSlice(3, slices) { t.Errorf("Expected true, but got false") } - if InInt8Slices(6, slices) { + if InSlice(6, slices) { t.Errorf("Expected false, but got true") } } func TestInInt16Slices(t *testing.T) { slices := []int16{1, 2, 3, 4, 5} - if !InInt16Slices(3, slices) { + if !InSlice(3, slices) { t.Errorf("Expected true, but got false") } - if InInt16Slices(6, slices) { + if InSlice(6, slices) { t.Errorf("Expected false, but got true") } } func TestInInt32Slices(t *testing.T) { slices := []int32{1, 2, 3, 4, 5} - if !InInt32Slices(3, slices) { + if !InSlice(3, slices) { t.Errorf("Expected true, but got false") } - if InInt32Slices(6, slices) { + if InSlice(6, slices) { t.Errorf("Expected false, but got true") } } func TestInInt64Slices(t *testing.T) { slices := []int64{1, 2, 3, 4, 5} - if !InInt64Slices(3, slices) { + if !InSlice(3, slices) { t.Errorf("Expected true, but got false") } - if InInt64Slices(6, slices) { + if InSlice(6, slices) { t.Errorf("Expected false, but got true") } } func TestInUintSlices(t *testing.T) { slices := []uint{1, 2, 3, 4, 5} - if !InUintSlices(3, slices) { + if !InSlice(3, slices) { t.Errorf("Expected true, but got false") } - if InUintSlices(6, slices) { + if InSlice(6, slices) { t.Errorf("Expected false, but got true") } } func TestInUint8Slices(t *testing.T) { slices := []uint8{1, 2, 3, 4, 5} - if !InUint8Slices(3, slices) { + if !InSlice(3, slices) { t.Errorf("Expected true, but got false") } - if InUint8Slices(6, slices) { + if InSlice(6, slices) { t.Errorf("Expected false, but got true") } } func TestInUint16Slices(t *testing.T) { slices := []uint16{1, 2, 3, 4, 5} - if !InUint16Slices(3, slices) { + if !InSlice(3, slices) { t.Errorf("Expected true, but got false") } - if InUint16Slices(6, slices) { + if InSlice(6, slices) { t.Errorf("Expected false, but got true") } } func TestInUint32Slices(t *testing.T) { slices := []uint32{1, 2, 3, 4, 5} - if !InUint32Slices(3, slices) { + if !InSlice(3, slices) { t.Errorf("Expected true, but got false") } - if InUint32Slices(6, slices) { + if InSlice(6, slices) { t.Errorf("Expected false, but got true") } } func TestInUint64Slices(t *testing.T) { slices := []uint64{1, 2, 3, 4, 5} - if !InUint64Slices(3, slices) { + if !InSlice(3, slices) { t.Errorf("Expected true, but got false") } - if InUint64Slices(6, slices) { + if InSlice(6, slices) { t.Errorf("Expected false, but got true") } } diff --git a/sliceUtil/is_slice.go b/sliceUtil/is_slice.go index 7b44397..550199c 100644 --- a/sliceUtil/is_slice.go +++ b/sliceUtil/is_slice.go @@ -3,6 +3,6 @@ package sliceUtil import "reflect" // IsSlice 判断指定值i是否是slice类型 -func IsSlice(slice interface{}) bool { +func IsSlice(slice any) bool { return reflect.ValueOf(slice).Kind() == reflect.Slice } diff --git a/sliceUtil/is_slice_test.go b/sliceUtil/is_slice_test.go index f031f83..4312688 100644 --- a/sliceUtil/is_slice_test.go +++ b/sliceUtil/is_slice_test.go @@ -4,12 +4,12 @@ import "testing" func TestIsSlice(t *testing.T) { var tests = []struct { - input interface{} + input any want bool }{ {[]int{1, 1, 3}, true}, - {[]interface{}{1, 2, "a"}, true}, - {[]map[string]interface{}{ + {[]any{1, 2, "a"}, true}, + {[]map[string]any{ {"1": 1}, {"c": 89}, }, true}, diff --git a/sliceUtil/merge_slice.go b/sliceUtil/merge_slice.go index 34d6460..ca46374 100644 --- a/sliceUtil/merge_slice.go +++ b/sliceUtil/merge_slice.go @@ -1,129 +1,8 @@ package sliceUtil -// MergeSlices 将多个slice合并成一个slice -func MergeSlices(slices ...[]interface{}) []interface{} { - var newSlice []interface{} - for _, slice := range slices { - for _, v := range slice { - newSlice = append(newSlice, v) - } - } - return newSlice -} - -// MergeStrSlices 将多个slice合并成一个slice -func MergeStrSlices(slices ...[]string) []string { - var newSlice []string - for _, slice := range slices { - for _, v := range slice { - newSlice = append(newSlice, v) - } - } - return newSlice -} - -// MergeIntSlices 将多个slice合并成一个slice -func MergeIntSlices(slices ...[]int) []int { - var newSlice []int - for _, slice := range slices { - for _, v := range slice { - newSlice = append(newSlice, v) - } - } - return newSlice -} - -// MergeInt8Slices 将多个slice合并成一个slice -func MergeInt8Slices(slices ...[]int8) []int8 { - var newSlice []int8 - for _, slice := range slices { - for _, v := range slice { - newSlice = append(newSlice, v) - } - } - return newSlice -} - -// MergeInt16Slices 将多个slice合并成一个slice -func MergeInt16Slices(slices ...[]int16) []int16 { - var newSlice []int16 - for _, slice := range slices { - for _, v := range slice { - newSlice = append(newSlice, v) - } - } - return newSlice -} - -// MergeInt32Slices 将多个slice合并成一个slice -func MergeInt32Slices(slices ...[]int32) []int32 { - var newSlice []int32 - for _, slice := range slices { - for _, v := range slice { - newSlice = append(newSlice, v) - } - } - return newSlice -} - -// MergeInt64Slices 将多个slice合并成一个slice -func MergeInt64Slices(slices ...[]int64) []int64 { - var newSlice []int64 - for _, slice := range slices { - for _, v := range slice { - newSlice = append(newSlice, v) - } - } - return newSlice -} - -// MergeUintSlices 将多个slice合并成一个slice -func MergeUintSlices(slices ...[]uint) []uint { - var newSlice []uint - for _, slice := range slices { - for _, v := range slice { - newSlice = append(newSlice, v) - } - } - return newSlice -} - -// MergeUint8Slices 将多个slice合并成一个slice -func MergeUint8Slices(slices ...[]uint8) []uint8 { - var newSlice []uint8 - for _, slice := range slices { - for _, v := range slice { - newSlice = append(newSlice, v) - } - } - return newSlice -} - -// MergeUint16Slices 将多个slice合并成一个slice -func MergeUint16Slices(slices ...[]uint16) []uint16 { - var newSlice []uint16 - for _, slice := range slices { - for _, v := range slice { - newSlice = append(newSlice, v) - } - } - return newSlice -} - -// MergeUint32Slices 将多个slice合并成一个slice -func MergeUint32Slices(slices ...[]uint32) []uint32 { - var newSlice []uint32 - for _, slice := range slices { - for _, v := range slice { - newSlice = append(newSlice, v) - } - } - return newSlice -} - -// MergeUint64Slices 将多个slice合并成一个slice -func MergeUint64Slices(slices ...[]uint64) []uint64 { - var newSlice []uint64 +// MergeSlice 将多个slice合并成一个slice +func MergeSlice[T any](slices ...[]T) []T { + var newSlice []T for _, slice := range slices { for _, v := range slice { newSlice = append(newSlice, v) diff --git a/sliceUtil/merge_slice_test.go b/sliceUtil/merge_slice_test.go index 0afc43e..4a7e44f 100644 --- a/sliceUtil/merge_slice_test.go +++ b/sliceUtil/merge_slice_test.go @@ -7,11 +7,11 @@ import ( // 测试MergeSlices func TestMergeSlices(t *testing.T) { - slice1 := []interface{}{1, 2, 3} - slice2 := []interface{}{4, 5, 6} - slice3 := []interface{}{7, 8, 9} - expected := []interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9} - result := MergeSlices(slice1, slice2, slice3) + slice1 := []any{1, 2, 3} + slice2 := []any{4, 5, 6} + slice3 := []any{7, 8, 9} + expected := []any{1, 2, 3, 4, 5, 6, 7, 8, 9} + result := MergeSlice(slice1, slice2, slice3) if !reflect.DeepEqual(result, expected) { t.Errorf("Expected %v, but got %v", expected, result) } @@ -23,7 +23,7 @@ func TestMergeStrSlices(t *testing.T) { slice2 := []string{"d", "e", "f"} slice3 := []string{"g", "h", "i"} expected := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i"} - result := MergeStrSlices(slice1, slice2, slice3) + result := MergeSlice(slice1, slice2, slice3) if !reflect.DeepEqual(result, expected) { t.Errorf("Expected %v, but got %v", expected, result) } @@ -35,7 +35,7 @@ func TestMergeIntSlices(t *testing.T) { slice3 := []int{7, 8, 9} expected := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} - result := MergeIntSlices(slice1, slice2, slice3) + result := MergeSlice(slice1, slice2, slice3) if !reflect.DeepEqual(result, expected) { t.Errorf("Expected %v but got %v", expected, result) @@ -48,7 +48,7 @@ func TestMergeInt8Slices(t *testing.T) { slice3 := []int8{7, 8, 9} expected := []int8{1, 2, 3, 4, 5, 6, 7, 8, 9} - result := MergeInt8Slices(slice1, slice2, slice3) + result := MergeSlice(slice1, slice2, slice3) if !reflect.DeepEqual(result, expected) { t.Errorf("Expected %v but got %v", expected, result) @@ -61,7 +61,7 @@ func TestMergeInt16Slices(t *testing.T) { slice3 := []int16{7, 8, 9} expected := []int16{1, 2, 3, 4, 5, 6, 7, 8, 9} - result := MergeInt16Slices(slice1, slice2, slice3) + result := MergeSlice(slice1, slice2, slice3) if !reflect.DeepEqual(result, expected) { t.Errorf("Expected %v but got %v", expected, result) @@ -74,7 +74,7 @@ func TestMergeInt32Slices(t *testing.T) { slice3 := []int32{7, 8, 9} expected := []int32{1, 2, 3, 4, 5, 6, 7, 8, 9} - result := MergeInt32Slices(slice1, slice2, slice3) + result := MergeSlice(slice1, slice2, slice3) if !reflect.DeepEqual(result, expected) { t.Errorf("Expected %v but got %v", expected, result) @@ -87,7 +87,7 @@ func TestMergeInt64Slices(t *testing.T) { slice3 := []int64{7, 8, 9} expected := []int64{1, 2, 3, 4, 5, 6, 7, 8, 9} - result := MergeInt64Slices(slice1, slice2, slice3) + result := MergeSlice(slice1, slice2, slice3) if !reflect.DeepEqual(result, expected) { t.Errorf("Expected %v but got %v", expected, result) @@ -100,7 +100,7 @@ func TestMergeUintSlices(t *testing.T) { slice3 := []uint{7, 8, 9} expected := []uint{1, 2, 3, 4, 5, 6, 7, 8, 9} - result := MergeUintSlices(slice1, slice2, slice3) + result := MergeSlice(slice1, slice2, slice3) if !reflect.DeepEqual(result, expected) { t.Errorf("Expected %v but got %v", expected, result) @@ -113,7 +113,7 @@ func TestMergeUint8Slices(t *testing.T) { slice3 := []uint8{7, 8, 9} expected := []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9} - result := MergeUint8Slices(slice1, slice2, slice3) + result := MergeSlice(slice1, slice2, slice3) if !reflect.DeepEqual(result, expected) { t.Errorf("Expected %v but got %v", expected, result) @@ -126,7 +126,7 @@ func TestMergeUint16Slices(t *testing.T) { slice3 := []uint16{7, 8, 9} expected := []uint16{1, 2, 3, 4, 5, 6, 7, 8, 9} - result := MergeUint16Slices(slice1, slice2, slice3) + result := MergeSlice(slice1, slice2, slice3) if !reflect.DeepEqual(result, expected) { t.Errorf("Expected %v but got %v", expected, result) @@ -139,7 +139,7 @@ func TestMergeUint32Slices(t *testing.T) { slice3 := []uint32{7, 8, 9} expected := []uint32{1, 2, 3, 4, 5, 6, 7, 8, 9} - result := MergeUint32Slices(slice1, slice2, slice3) + result := MergeSlice(slice1, slice2, slice3) if !reflect.DeepEqual(result, expected) { t.Errorf("Expected %v but got %v", expected, result) @@ -152,7 +152,7 @@ func TestMergeUint64Slices(t *testing.T) { slice3 := []uint64{7, 8, 9} expected := []uint64{1, 2, 3, 4, 5, 6, 7, 8, 9} - result := MergeUint64Slices(slice1, slice2, slice3) + result := MergeSlice(slice1, slice2, slice3) if !reflect.DeepEqual(result, expected) { t.Errorf("Expected %v but got %v", expected, result) diff --git a/sliceUtil/sum_slice.go b/sliceUtil/sum_slice.go index a01bb91..26e4cfd 100644 --- a/sliceUtil/sum_slice.go +++ b/sliceUtil/sum_slice.go @@ -1,62 +1,14 @@ package sliceUtil -// SumIntSlice 对slice中的元素求和 -func SumIntSlice(slice []int) int { - var sum int - for _, v := range slice { - sum += v - } - return sum +type Numeric interface { + ~int | ~int8 | ~int16 | ~int32 | ~int64 | + ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | + ~float32 | ~float64 } -// SumInt8Slice 对slice中的元素求和 -func SumInt8Slice(slice []int8) int8 { - var sum int8 - for _, v := range slice { - sum += v - } - return sum -} - -// SumInt16Slice 对slice中的元素求和 -func SumInt16Slice(slice []int16) int16 { - var sum int16 - for _, v := range slice { - sum += v - } - return sum -} - -// SumInt32Slice 对slice中的元素求和 -func SumInt32Slice(slice []int32) int32 { - var sum int32 - for _, v := range slice { - sum += v - } - return sum -} - -// SumInt64Slice 对slice中的元素求和 -func SumInt64Slice(slice []int64) int64 { - var sum int64 - for _, v := range slice { - sum += v - } - return sum -} - -// SumFloat32Slice 对slice中的元素求和 -func SumFloat32Slice(slice []float32) float32 { - var sum float32 - for _, v := range slice { - sum += v - } - return sum -} - -// SumFloat64Slice 对slice中的元素求和 -func SumFloat64Slice(slice []float64) float64 { - var sum float64 +// SumSlice 对slice中的元素求和 +func SumSlice[T Numeric](slice []T) T { + var sum T for _, v := range slice { sum += v } diff --git a/sliceUtil/sum_slice_test.go b/sliceUtil/sum_slice_test.go index fad41ca..c5a707b 100644 --- a/sliceUtil/sum_slice_test.go +++ b/sliceUtil/sum_slice_test.go @@ -1,6 +1,8 @@ package sliceUtil -import "testing" +import ( + "testing" +) func TestSumIntSlice(t *testing.T) { testCases := []struct { @@ -27,7 +29,7 @@ func TestSumIntSlice(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - result := SumIntSlice(tc.input) + result := SumSlice(tc.input) if result != tc.expected { t.Errorf("Expected %d but got %d", tc.expected, result) } @@ -60,7 +62,7 @@ func TestSumInt8Slice(t *testing.T) { for _, tc := range testCases { t.Run(tc.name, func(t *testing.T) { - result := SumInt8Slice(tc.input) + result := SumSlice(tc.input) if result != tc.expected { t.Errorf("Expected %d but got %d", tc.expected, result) } @@ -82,7 +84,7 @@ func TestSumInt16Slice(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := SumInt16Slice(tt.slice) + got := SumSlice(tt.slice) if got != tt.want { t.Errorf("SumInt16Slice(%v) = %v, want %v", tt.slice, got, tt.want) } @@ -104,7 +106,7 @@ func TestSumInt32Slice(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := SumInt32Slice(tt.slice) + got := SumSlice(tt.slice) if got != tt.want { t.Errorf("SumInt32Slice(%v) = %v, want %v", tt.slice, got, tt.want) } @@ -126,7 +128,7 @@ func TestSumInt64Slice(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := SumInt64Slice(tt.slice) + got := SumSlice(tt.slice) if got != tt.want { t.Errorf("SumInt64Slice(%v) = %v, want %v", tt.slice, got, tt.want) } @@ -148,7 +150,7 @@ func TestSumFloat32Slice(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := SumFloat32Slice(tt.slice) + got := SumSlice(tt.slice) if got != tt.want { t.Errorf("SumFloat32Slice(%v) = %v, want %v", tt.slice, got, tt.want) } @@ -170,7 +172,7 @@ func TestSumFloat64Slice(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - got := SumFloat64Slice(tt.slice) + got := SumSlice(tt.slice) if got != tt.want { t.Errorf("SumFloat64Slice(%v) = %v, want %v", tt.slice, got, tt.want) } diff --git a/sliceUtil/unique_slice.go b/sliceUtil/unique_slice.go index 536e676..fec9cdc 100644 --- a/sliceUtil/unique_slice.go +++ b/sliceUtil/unique_slice.go @@ -1,185 +1,17 @@ package sliceUtil -import "reflect" - // UniqueSlice 移除slice中重复的值 -func UniqueSlice(slice interface{}) interface{} { - if reflect.ValueOf(slice).Kind() != reflect.Slice { +func UniqueSlice[T comparable](slice []T) []T { + if len(slice) == 0 { return slice } - - s := reflect.ValueOf(slice) - if s.Len() == 0 { - return slice - } - t := reflect.TypeOf(slice).Elem() - m := make(map[interface{}]bool) - for i := 0; i < s.Len(); i++ { - v := s.Index(i) - if _, ok := m[v.Interface()]; ok { - s = reflect.AppendSlice(s.Slice(0, i), s.Slice(i+1, s.Len())) - i-- - } else { - m[v.Interface()] = true + m := make(map[T]bool) + var result []T + for _, v := range slice { + if !m[v] { + m[v] = true + result = append(result, v) } } - result := reflect.MakeSlice(reflect.SliceOf(t), s.Len(), s.Len()) - reflect.Copy(result, s) - return result.Interface() + return result } - -//func UniqueStr(v []string) []string { -// m := make(map[string]bool) -// var result []string -// for _, v := range v { -// if _, ok := m[v]; !ok { -// m[v] = true -// result = append(result, v) -// } -// } -// return result -//} -// -//func UniqueInt(v []int) []int { -// m := make(map[int]bool) -// var result []int -// for _, v := range v { -// if _, ok := m[v]; !ok { -// m[v] = true -// result = append(result, v) -// } -// } -// return result -//} -// -//func UniqueInt8(v []int8) []int8 { -// m := make(map[int8]bool) -// var result []int8 -// for _, v := range v { -// if _, ok := m[v]; !ok { -// m[v] = true -// result = append(result, v) -// } -// } -// return result -//} -// -//func UniqueInt16(v []int16) []int16 { -// m := make(map[int16]bool) -// var result []int16 -// for _, v := range v { -// if _, ok := m[v]; !ok { -// m[v] = true -// result = append(result, v) -// } -// } -// return result -//} -// -//func UniqueInt32(v []int32) []int32 { -// m := make(map[int32]bool) -// var result []int32 -// for _, v := range v { -// if _, ok := m[v]; !ok { -// m[v] = true -// result = append(result, v) -// } -// } -// return result -//} -// -//func UniqueInt64(v []int64) []int64 { -// m := make(map[int64]bool) -// var result []int64 -// for _, v := range v { -// if _, ok := m[v]; !ok { -// m[v] = true -// result = append(result, v) -// } -// } -// return result -//} -// -//func UniqueFloat32(v []float32) []float32 { -// m := make(map[float32]bool) -// var result []float32 -// for _, v := range v { -// if _, ok := m[v]; !ok { -// m[v] = true -// result = append(result, v) -// } -// } -// return result -//} -// -//func UniqueFloat64(v []float64) []float64 { -// m := make(map[float64]bool) -// var result []float64 -// for _, v := range v { -// if _, ok := m[v]; !ok { -// m[v] = true -// result = append(result, v) -// } -// } -// return result -//} -// -//func UniqueUint(v []uint) []uint { -// m := make(map[uint]bool) -// var result []uint -// for _, v := range v { -// if _, ok := m[v]; !ok { -// m[v] = true -// result = append(result, v) -// } -// } -// return result -//} -// -//func UniqueUint8(v []uint8) []uint8 { -// m := make(map[uint8]bool) -// var result []uint8 -// for _, v := range v { -// if _, ok := m[v]; !ok { -// m[v] = true -// result = append(result, v) -// } -// } -// return result -//} -// -//func UniqueUint16(v []uint16) []uint16 { -// m := make(map[uint16]bool) -// var result []uint16 -// for _, v := range v { -// if _, ok := m[v]; !ok { -// m[v] = true -// result = append(result, v) -// } -// } -// return result -//} -// -//func UniqueUint32(v []uint32) []uint32 { -// m := make(map[uint32]bool) -// var result []uint32 -// for _, v := range v { -// if _, ok := m[v]; !ok { -// m[v] = true -// result = append(result, v) -// } -// } -// return result -//} -// -//func UniqueUint64(v []uint64) []uint64 { -// m := make(map[uint64]bool) -// var result []uint64 -// for _, v := range v { -// if _, ok := m[v]; !ok { -// m[v] = true -// result = append(result, v) -// } -// } -// return result -//} diff --git a/sliceUtil/unique_slice_test.go b/sliceUtil/unique_slice_test.go index aca7a21..66023ca 100644 --- a/sliceUtil/unique_slice_test.go +++ b/sliceUtil/unique_slice_test.go @@ -8,24 +8,17 @@ import ( func TestUniqueSlice(t *testing.T) { // 测试整型切片去重 s1 := []int{1, 2, 3, 2, 4, 4, 5} - s1 = UniqueSlice(s1).([]int) + s1 = UniqueSlice(s1) if !reflect.DeepEqual(s1, []int{1, 2, 3, 4, 5}) { t.Errorf("UniqueSlice(%v) = %v, expected %v", s1, s1, []int{1, 2, 3, 4, 5}) } // 测试字符串切片去重 s2 := []string{"a", "b", "c", "b", "d", "d", "e"} - s2 = UniqueSlice(s2).([]string) + s2 = UniqueSlice(s2) if !reflect.DeepEqual(s2, []string{"a", "b", "c", "d", "e"}) { t.Errorf("UniqueSlice(%v) = %v, expected %v", s2, s2, []string{"a", "b", "c", "d", "e"}) } - - // 测试 interface{} 类型切片去重 - s3 := []interface{}{1, 2, "3", 2, "4", "4", 5} - s3 = UniqueSlice(s3).([]interface{}) - if !reflect.DeepEqual(s3, []interface{}{1, 2, "3", "4", 5}) { - t.Errorf("UniqueSlice(%v) = %v, expected %v", s3, s3, []interface{}{1, 2, "3", "4", 5}) - } } func TestUniqueSliceInt(t *testing.T) { diff --git a/strUtil/README.md b/strUtil/README.md index 288b4a3..f3182fa 100644 --- a/strUtil/README.md +++ b/strUtil/README.md @@ -6,6 +6,14 @@ go get -u github.com/jefferyjob/go-easy-utils/strUtil ``` +## Import + +```go +import ( + "github.com/jefferyjob/go-easy-utils/strUtil" +) +``` + ## Functions ```go diff --git a/validUtil/README.md b/validUtil/README.md index 6b18b52..522fd1c 100644 --- a/validUtil/README.md +++ b/validUtil/README.md @@ -6,6 +6,14 @@ go get -u github.com/jefferyjob/go-easy-utils/validUtil ``` +## Import + +```go +import ( + "github.com/jefferyjob/go-easy-utils/validUtil" +) +``` + ## Functions ```go