diff --git a/README.cn.md b/README.cn.md index 57335d3..8f7939f 100644 --- a/README.cn.md +++ b/README.cn.md @@ -196,11 +196,11 @@ func UniqueSlice(slice []T) []T ### mapUtil map类型处理 ```go -// MapValueExists 判断map中的value是否存在 -func MapValueExists[T comparable](m map[string]T, value T) bool - // MapKeyExists 判断map中的key是否存在 -func MapKeyExists(m map[string]any, key string) bool +func MapKeyExists((m map[T]T2, key T)) bool + +// MapValueExists 判断map中的value是否存在 +func MapValueExists(m map[T2]T, value T) bool ``` ### mathUtil diff --git a/README.md b/README.md index 298b404..a56996d 100644 --- a/README.md +++ b/README.md @@ -13,12 +13,6 @@ English | [简体中文](README.cn.md) ## Introduction 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** @@ -29,7 +23,7 @@ go get -u github.com/jefferyjob/go-easy-utils Users who use `Go1.18` below must install `v1.x.x`. The latest `v1` version is `v1.1.0` ```bash -go get -u github.com/jefferyjob/go-easy-utils +go get github.com/jefferyjob/go-easy-utils@v1.1.0 ``` **Use Demo** diff --git a/anyUtil/any_to_bool_example_test.go b/anyUtil/any_to_bool_example_test.go new file mode 100644 index 0000000..84e3134 --- /dev/null +++ b/anyUtil/any_to_bool_example_test.go @@ -0,0 +1,17 @@ +package anyUtil + +import "fmt" + +func ExampleAnyToBool() { + a1 := "" + a2 := "hello" + res1 := AnyToBool(a1) + res2 := AnyToBool(a2) + + fmt.Println(res1) + fmt.Println(res2) + + // Output: + // false + // true +} diff --git a/anyUtil/any_to_float_x_example_test.go b/anyUtil/any_to_float_x_example_test.go new file mode 100644 index 0000000..77cfe9e --- /dev/null +++ b/anyUtil/any_to_float_x_example_test.go @@ -0,0 +1,23 @@ +package anyUtil + +import "fmt" + +func ExampleAnyToFloat32() { + f := "3" + res, _ := AnyToFloat32(f) + + fmt.Printf("%T\n", res) + + // Output: + // float32 +} + +func ExampleAnyToFloat64() { + f := "5" + res, _ := AnyToFloat64(f) + + fmt.Printf("%T\n", res) + + // Output: + // float64 +} diff --git a/anyUtil/any_to_int_x_example_test.go b/anyUtil/any_to_int_x_example_test.go new file mode 100644 index 0000000..7a25b10 --- /dev/null +++ b/anyUtil/any_to_int_x_example_test.go @@ -0,0 +1,73 @@ +package anyUtil + +import "fmt" + +func ExampleAnyToInt() { + s1 := "100" + s2 := "-100" + res1, _ := AnyToInt(s1) + res2, _ := AnyToInt(s2) + + fmt.Printf("%d,%T\n", res1, res1) + fmt.Printf("%d,%T\n", res2, res2) + + // Output: + // 100,int + // -100,int +} + +func ExampleAnyToInt8() { + s1 := "100" + s2 := "-100" + res1, _ := AnyToInt8(s1) + res2, _ := AnyToInt8(s2) + + fmt.Printf("%d,%T\n", res1, res1) + fmt.Printf("%d,%T\n", res2, res2) + + // Output: + // 100,int8 + // -100,int8 +} + +func ExampleAnyToInt16() { + s1 := "100" + s2 := "-100" + res1, _ := AnyToInt16(s1) + res2, _ := AnyToInt16(s2) + + fmt.Printf("%d,%T\n", res1, res1) + fmt.Printf("%d,%T\n", res2, res2) + + // Output: + // 100,int16 + // -100,int16 +} + +func ExampleAnyToInt32() { + s1 := "100" + s2 := "-100" + res1, _ := AnyToInt32(s1) + res2, _ := AnyToInt32(s2) + + fmt.Printf("%d,%T\n", res1, res1) + fmt.Printf("%d,%T\n", res2, res2) + + // Output: + // 100,int32 + // -100,int32 +} + +func ExampleAnyToInt64() { + s1 := "100" + s2 := "-100" + res1, _ := AnyToInt64(s1) + res2, _ := AnyToInt64(s2) + + fmt.Printf("%d,%T\n", res1, res1) + fmt.Printf("%d,%T\n", res2, res2) + + // Output: + // 100,int64 + // -100,int64 +} diff --git a/anyUtil/any_to_string.go b/anyUtil/any_to_str.go similarity index 100% rename from anyUtil/any_to_string.go rename to anyUtil/any_to_str.go diff --git a/anyUtil/any_to_str_example_test.go b/anyUtil/any_to_str_example_test.go new file mode 100644 index 0000000..9be2668 --- /dev/null +++ b/anyUtil/any_to_str_example_test.go @@ -0,0 +1,13 @@ +package anyUtil + +import "fmt" + +func ExampleAnyToStr() { + a := 123 + res := AnyToStr(a) + + fmt.Println(res) + + // Output: + // 123 +} diff --git a/anyUtil/any_to_string_test.go b/anyUtil/any_to_str_test.go similarity index 100% rename from anyUtil/any_to_string_test.go rename to anyUtil/any_to_str_test.go diff --git a/anyUtil/any_to_uint_x_example_test.go b/anyUtil/any_to_uint_x_example_test.go new file mode 100644 index 0000000..725781c --- /dev/null +++ b/anyUtil/any_to_uint_x_example_test.go @@ -0,0 +1,53 @@ +package anyUtil + +import "fmt" + +func ExampleAnyToUint() { + s := "200" + res, _ := AnyToUint(s) + + fmt.Printf("%d,%T\n", res, res) + + // Output: + // 200,uint +} + +func ExampleAnyToUint8() { + s := "200" + res, _ := AnyToUint8(s) + + fmt.Printf("%d,%T\n", res, res) + + // Output: + // 200,uint8 +} + +func ExampleAnyToUint16() { + s := "200" + res, _ := AnyToUint16(s) + + fmt.Printf("%d,%T\n", res, res) + + // Output: + // 200,uint16 +} + +func ExampleAnyToUint32() { + s := "200" + res, _ := AnyToUint32(s) + + fmt.Printf("%d,%T\n", res, res) + + // Output: + // 200,uint32 +} + +func ExampleAnyToUint64() { + s := "200" + res, _ := AnyToUint64(s) + + fmt.Printf("%d,%T\n", res, res) + + // Output: + // 200,uint64 +} diff --git a/byteUtil/byte_to.go b/byteUtil/byte_to_str.go similarity index 100% rename from byteUtil/byte_to.go rename to byteUtil/byte_to_str.go diff --git a/byteUtil/byte_to_str_example_test.go b/byteUtil/byte_to_str_example_test.go new file mode 100644 index 0000000..e9dc4f5 --- /dev/null +++ b/byteUtil/byte_to_str_example_test.go @@ -0,0 +1,13 @@ +package byteUtil + +import "fmt" + +func ExampleBytesToStr() { + b := []byte{'h', 'e', 'l', 'l', 'o'} + res := BytesToStr(b) + + fmt.Println(res) + + // Output: + // hello +} diff --git a/byteUtil/byte_to_test.go b/byteUtil/byte_to_str_test.go similarity index 100% rename from byteUtil/byte_to_test.go rename to byteUtil/byte_to_str_test.go diff --git a/cryptoUtil/hash_example_test.go b/cryptoUtil/hash_example_test.go new file mode 100644 index 0000000..f6239fd --- /dev/null +++ b/cryptoUtil/hash_example_test.go @@ -0,0 +1,13 @@ +package cryptoUtil + +import "fmt" + +func ExampleHashSHA256() { + str := "hello" + res := HashSHA256(str) + + fmt.Println(res) + + // Output: + // 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 +} diff --git a/cryptoUtil/md5_example_test.go b/cryptoUtil/md5_example_test.go new file mode 100644 index 0000000..0aeb4a7 --- /dev/null +++ b/cryptoUtil/md5_example_test.go @@ -0,0 +1,13 @@ +package cryptoUtil + +import "fmt" + +func ExampleMd5() { + str := "hello" + res := Md5(str) + + fmt.Println(res) + + // Output: + // 5d41402abc4b2a76b9719d911017c592 +} diff --git a/emojiUtil/emoji_example_test.go b/emojiUtil/emoji_example_test.go new file mode 100644 index 0000000..453953b --- /dev/null +++ b/emojiUtil/emoji_example_test.go @@ -0,0 +1,23 @@ +package emojiUtil + +import "fmt" + +func ExampleEncodeEmojiUnicode() { + e := "[\\u1F60E]" + res := DecodeEmojiUnicode(e) + + fmt.Println(res) + + // Output: + // 😎 +} + +func ExampleDecodeEmojiUnicode() { + e := "😂" + res := EncodeEmojiUnicode(e) + + fmt.Println(res) + + // Output: + // [\u1f602] +} diff --git a/floatUtil/float_x_to_x.go b/floatUtil/float_x_to_x.go index 5ed4559..bebeba9 100644 --- a/floatUtil/float_x_to_x.go +++ b/floatUtil/float_x_to_x.go @@ -10,16 +10,18 @@ func Float32ToStr(f float32) string { return fmt.Sprintf("%v", f) } +// Float32ToFloat64 float32转float64 +func Float32ToFloat64(f float32) float64 { + str := fmt.Sprintf("%f", f) + v, _ := strconv.ParseFloat(str, 64) + return v +} + // Float64ToStr float64转字符串 func Float64ToStr(f float64) string { return strconv.FormatFloat(f, 'f', -1, 64) } -// Float32ToFloat64 float32转float64 -func Float32ToFloat64(f float32) float64 { - return float64(f) -} - // Float64ToFloat32 float64转float32 func Float64ToFloat32(f float64) float32 { return float32(f) diff --git a/floatUtil/float_x_to_x_example_test.go b/floatUtil/float_x_to_x_example_test.go new file mode 100644 index 0000000..25487fa --- /dev/null +++ b/floatUtil/float_x_to_x_example_test.go @@ -0,0 +1,43 @@ +package floatUtil + +import "fmt" + +func ExampleFloat32ToStr() { + var f float32 = 3.1 + res := Float32ToStr(f) + + fmt.Println(res) + + // Output: + // 3.1 +} + +func ExampleFloat32ToFloat64() { + var f float32 = 3.1 + res := Float32ToFloat64(f) + + fmt.Println(res) + + // Output: + // 3.1 +} + +func ExampleFloat64ToStr() { + f := 3.14 + res := Float64ToStr(f) + + fmt.Println(res) + + // Output: + // 3.14 +} + +func ExampleFloat64ToFloat32() { + f := 3.14 + res := Float64ToFloat32(f) + + fmt.Println(res) + + // Output: + // 3.14 +} diff --git a/intUtil/int_x_to_str.go b/intUtil/int_x_to_str.go index 5069fcc..277eff1 100644 --- a/intUtil/int_x_to_str.go +++ b/intUtil/int_x_to_str.go @@ -12,7 +12,7 @@ func Int8ToStr(v int8) string { return strconv.Itoa(int(v)) } -// Int16ToString 将int16类型转换为string类型 +// Int16ToStr 将int16类型转换为string类型 func Int16ToStr(v int16) string { return strconv.Itoa(int(v)) } diff --git a/intUtil/int_x_to_str_example_test.go b/intUtil/int_x_to_str_example_test.go new file mode 100644 index 0000000..30996f9 --- /dev/null +++ b/intUtil/int_x_to_str_example_test.go @@ -0,0 +1,53 @@ +package intUtil + +import "fmt" + +func ExampleIntToStr() { + i := 123 + res := IntToStr(i) + + fmt.Println(res) + + // Output: + // 123 +} + +func ExampleInt8ToStr() { + i := 123 + res := IntToStr(i) + + fmt.Println(res) + + // Output: + // 123 +} + +func ExampleInt16ToStr() { + i := 123 + res := IntToStr(i) + + fmt.Println(res) + + // Output: + // 123 +} + +func ExampleInt32ToStr() { + i := 123 + res := IntToStr(i) + + fmt.Println(res) + + // Output: + // 123 +} + +func ExampleInt64ToStr() { + i := 123 + res := IntToStr(i) + + fmt.Println(res) + + // Output: + // 123 +} diff --git a/jsonUtil/json_to_struct_example_test.go b/jsonUtil/json_to_struct_example_test.go new file mode 100644 index 0000000..8813013 --- /dev/null +++ b/jsonUtil/json_to_struct_example_test.go @@ -0,0 +1,26 @@ +package jsonUtil + +import "fmt" + +func ExampleJsonToStruct() { + jsonData := `{ + "name": "make", + "age": "22", + "is_use": "1" + }` + + var people struct { + Name string `json:"name,omitempty"` + Age int `json:"age"` + IsUse bool `json:"is_use"` + } + + if err := JsonToStruct(jsonData, &people); err != nil { + fmt.Println(err) + return + } + fmt.Printf("%+v", people) + + // Output: + // {Name:make Age:22 IsUse:true} +} diff --git a/mapUtil/README.md b/mapUtil/README.md index c9e5d8a..dee675b 100644 --- a/mapUtil/README.md +++ b/mapUtil/README.md @@ -17,9 +17,9 @@ import ( ## Functions ```go -// MapValueExists 判断map中的value是否存在 -func MapValueExists(m map[string]T, value T) bool - // MapKeyExists 判断map中的key是否存在 -func MapKeyExists(m map[string]any, key string) bool +func MapKeyExists((m map[T]T2, key T)) bool + +// MapValueExists 判断map中的value是否存在 +func MapValueExists(m map[T2]T, value T) bool ``` \ No newline at end of file diff --git a/mapUtil/map_exists.go b/mapUtil/map_exists.go index 4826ed4..9751196 100644 --- a/mapUtil/map_exists.go +++ b/mapUtil/map_exists.go @@ -1,7 +1,13 @@ package mapUtil +// MapKeyExists 判断map中的key是否存在 +func MapKeyExists[T comparable, T2 any](m map[T]T2, key T) bool { + _, exists := m[key] + return exists +} + // MapValueExists 判断map中的value是否存在 -func MapValueExists[T comparable](m map[string]T, value T) bool { +func MapValueExists[T comparable, T2 comparable](m map[T2]T, value T) bool { for _, v := range m { if v == value { return true @@ -9,9 +15,3 @@ func MapValueExists[T comparable](m map[string]T, value T) bool { } return false } - -// MapKeyExists 判断map中的key是否存在 -func MapKeyExists(m map[string]any, key string) bool { - _, exists := m[key] - return exists -} diff --git a/mapUtil/map_exists_example_test.go b/mapUtil/map_exists_example_test.go new file mode 100644 index 0000000..441c854 --- /dev/null +++ b/mapUtil/map_exists_example_test.go @@ -0,0 +1,29 @@ +package mapUtil + +import "fmt" + +func ExampleMapKeyExists() { + m := map[string]string{ + "foo": "bar", + "baz": "123", + } + res := MapKeyExists(m, "foo") + + fmt.Println(res) + + // Output: + // true +} + +func ExampleMapValueExists() { + m := map[string]string{ + "foo": "bar", + "baz": "123", + } + res := MapValueExists(m, "123") + + fmt.Println(res) + + // Output: + // true +} diff --git a/sliceUtil/chunk_slice_example_test.go b/sliceUtil/chunk_slice_example_test.go new file mode 100644 index 0000000..9497147 --- /dev/null +++ b/sliceUtil/chunk_slice_example_test.go @@ -0,0 +1,13 @@ +package sliceUtil + +import "fmt" + +func ExampleChunkSlice() { + slice := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"} + res := ChunkSlice(slice, 3) + + fmt.Printf("%+v", res) + + // Output: + // [[a b c] [d e f] [g h i] [j k]] +} diff --git a/sliceUtil/column_slice_example_test.go b/sliceUtil/column_slice_example_test.go new file mode 100644 index 0000000..4742525 --- /dev/null +++ b/sliceUtil/column_slice_example_test.go @@ -0,0 +1,22 @@ +package sliceUtil + +import "fmt" + +func ExampleColumnSlice() { + type Person struct { + Name string + Age int + } + people := []Person{ + {"Alice", 18}, + {"Bob", 20}, + {"Charlie", 22}, + } + + res := ColumnSlice(people, "Age") + + fmt.Printf("%+v", res) + + // Output: + // [18 20 22] +} diff --git a/sliceUtil/in_slice_example_test.go b/sliceUtil/in_slice_example_test.go new file mode 100644 index 0000000..5cd7cd4 --- /dev/null +++ b/sliceUtil/in_slice_example_test.go @@ -0,0 +1,13 @@ +package sliceUtil + +import "fmt" + +func ExampleInSlice() { + slice := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"} + res := InSlice("c", slice) + + fmt.Println(res) + + // Output: + // true +} diff --git a/sliceUtil/is_slice_example_test.go b/sliceUtil/is_slice_example_test.go new file mode 100644 index 0000000..7a34359 --- /dev/null +++ b/sliceUtil/is_slice_example_test.go @@ -0,0 +1,18 @@ +package sliceUtil + +import "fmt" + +func ExampleIsSlice() { + slice1 := []int{1, 2, 3} + slice2 := make(chan int) + + res1 := IsSlice(slice1) + res2 := IsSlice(slice2) + + fmt.Println(res1) + fmt.Println(res2) + + // Output: + // true + // false +} diff --git a/sliceUtil/merge_slice_example_test.go b/sliceUtil/merge_slice_example_test.go new file mode 100644 index 0000000..cb249c7 --- /dev/null +++ b/sliceUtil/merge_slice_example_test.go @@ -0,0 +1,15 @@ +package sliceUtil + +import "fmt" + +func ExampleMergeSlice() { + slice1 := []int{1, 2, 3} + slice2 := []int{4, 5, 6} + slice3 := []int{7, 8, 9} + res := MergeSlice(slice1, slice2, slice3) + + fmt.Printf("%+v", res) + + // Output: + // [1 2 3 4 5 6 7 8 9] +} diff --git a/sliceUtil/sum_slice_example_test.go b/sliceUtil/sum_slice_example_test.go new file mode 100644 index 0000000..22d6771 --- /dev/null +++ b/sliceUtil/sum_slice_example_test.go @@ -0,0 +1,13 @@ +package sliceUtil + +import "fmt" + +func ExampleSumSlice() { + slice := []int{1, 2, 3, 4, 5} + res := SumSlice(slice) + + fmt.Println(res) + + // Output: + // 15 +} diff --git a/sliceUtil/unique_slice_example_test.go b/sliceUtil/unique_slice_example_test.go new file mode 100644 index 0000000..51cb89a --- /dev/null +++ b/sliceUtil/unique_slice_example_test.go @@ -0,0 +1,13 @@ +package sliceUtil + +import "fmt" + +func ExampleUniqueSlice() { + slice := []int{1, 2, 3, 2, 4, 4, 5} + res := UniqueSlice(slice) + + fmt.Printf("%+v", res) + + // Output: + // [1 2 3 4 5] +} diff --git a/strUtil/string_to_byte.go b/strUtil/str_to_byte.go similarity index 100% rename from strUtil/string_to_byte.go rename to strUtil/str_to_byte.go diff --git a/strUtil/str_to_byte_example_test.go b/strUtil/str_to_byte_example_test.go new file mode 100644 index 0000000..7565a0b --- /dev/null +++ b/strUtil/str_to_byte_example_test.go @@ -0,0 +1,13 @@ +package strUtil + +import "fmt" + +func ExampleStrToBytes() { + s := "hello" + res := StrToBytes(s) + + fmt.Printf("%v", res) + + // Output: + // [104 101 108 108 111] +} diff --git a/strUtil/string_to_byte_test.go b/strUtil/str_to_byte_test.go similarity index 100% rename from strUtil/string_to_byte_test.go rename to strUtil/str_to_byte_test.go diff --git a/strUtil/string_to_int_x.go b/strUtil/str_to_int_x.go similarity index 100% rename from strUtil/string_to_int_x.go rename to strUtil/str_to_int_x.go diff --git a/strUtil/str_to_int_x_example_test.go b/strUtil/str_to_int_x_example_test.go new file mode 100644 index 0000000..f5afafd --- /dev/null +++ b/strUtil/str_to_int_x_example_test.go @@ -0,0 +1,73 @@ +package strUtil + +import "fmt" + +func ExampleStrToInt() { + s1 := "100" + s2 := "-100" + res1 := StrToInt(s1) + res2 := StrToInt(s2) + + fmt.Printf("%d,%T\n", res1, res1) + fmt.Printf("%d,%T\n", res2, res2) + + // Output: + // 100,int + // -100,int +} + +func ExampleStrToInt8() { + s1 := "100" + s2 := "-100" + res1 := StrToInt8(s1) + res2 := StrToInt8(s2) + + fmt.Printf("%d,%T\n", res1, res1) + fmt.Printf("%d,%T\n", res2, res2) + + // Output: + // 100,int8 + // -100,int8 +} + +func ExampleStrToInt16() { + s1 := "100" + s2 := "-100" + res1 := StrToInt16(s1) + res2 := StrToInt16(s2) + + fmt.Printf("%d,%T\n", res1, res1) + fmt.Printf("%d,%T\n", res2, res2) + + // Output: + // 100,int16 + // -100,int16 +} + +func ExampleStrToInt32() { + s1 := "100" + s2 := "-100" + res1 := StrToInt32(s1) + res2 := StrToInt32(s2) + + fmt.Printf("%d,%T\n", res1, res1) + fmt.Printf("%d,%T\n", res2, res2) + + // Output: + // 100,int32 + // -100,int32 +} + +func ExampleStrToInt64() { + s1 := "100" + s2 := "-100" + res1 := StrToInt64(s1) + res2 := StrToInt64(s2) + + fmt.Printf("%d,%T\n", res1, res1) + fmt.Printf("%d,%T\n", res2, res2) + + // Output: + // 100,int64 + // -100,int64 +} diff --git a/strUtil/string_to_int_x_test.go b/strUtil/str_to_int_x_test.go similarity index 100% rename from strUtil/string_to_int_x_test.go rename to strUtil/str_to_int_x_test.go diff --git a/strUtil/string_to_uint_x.go b/strUtil/str_to_uint_x.go similarity index 100% rename from strUtil/string_to_uint_x.go rename to strUtil/str_to_uint_x.go diff --git a/strUtil/str_to_uint_x_example_test.go b/strUtil/str_to_uint_x_example_test.go new file mode 100644 index 0000000..86b5b0a --- /dev/null +++ b/strUtil/str_to_uint_x_example_test.go @@ -0,0 +1,53 @@ +package strUtil + +import "fmt" + +func ExampleStrToUint() { + s := "200" + res := StrToUint(s) + + fmt.Printf("%d,%T\n", res, res) + + // Output: + // 200,uint +} + +func ExampleStrToUint8() { + s := "200" + res := StrToUint8(s) + + fmt.Printf("%d,%T\n", res, res) + + // Output: + // 200,uint8 +} + +func ExampleStrToUint16() { + s := "200" + res := StrToUint16(s) + + fmt.Printf("%d,%T\n", res, res) + + // Output: + // 200,uint16 +} + +func ExampleStrToUint32() { + s := "200" + res := StrToUint32(s) + + fmt.Printf("%d,%T\n", res, res) + + // Output: + // 200,uint32 +} + +func ExampleStrToUint64() { + s := "200" + res := StrToUint64(s) + + fmt.Printf("%d,%T\n", res, res) + + // Output: + // 200,uint64 +} diff --git a/strUtil/string_to_uint_x_test.go b/strUtil/str_to_uint_x_test.go similarity index 100% rename from strUtil/string_to_uint_x_test.go rename to strUtil/str_to_uint_x_test.go