Feature/example test (#24)

example docs
This commit is contained in:
jeffery
2023-04-12 15:09:44 +08:00
committed by GitHub
parent ac72afe256
commit e9cc195c39
39 changed files with 662 additions and 28 deletions

View File

@@ -196,11 +196,11 @@ func UniqueSlice(slice []T) []T
### mapUtil map类型处理 ### mapUtil map类型处理
```go ```go
// MapValueExists 判断map中的value是否存在
func MapValueExists[T comparable](m map[string]T, value T) bool
// MapKeyExists 判断map中的key是否存在 // 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 ### mathUtil

View File

@@ -13,12 +13,6 @@ English | [简体中文](README.cn.md)
## Introduction ## 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. 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 ## Quick Start
**Install** **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` Users who use `Go1.18` below must install `v1.x.x`. The latest `v1` version is `v1.1.0`
```bash ```bash
go get -u github.com/jefferyjob/go-easy-utils go get github.com/jefferyjob/go-easy-utils@v1.1.0
``` ```
**Use Demo** **Use Demo**

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -0,0 +1,13 @@
package anyUtil
import "fmt"
func ExampleAnyToStr() {
a := 123
res := AnyToStr(a)
fmt.Println(res)
// Output:
// 123
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -0,0 +1,13 @@
package cryptoUtil
import "fmt"
func ExampleHashSHA256() {
str := "hello"
res := HashSHA256(str)
fmt.Println(res)
// Output:
// 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
}

View File

@@ -0,0 +1,13 @@
package cryptoUtil
import "fmt"
func ExampleMd5() {
str := "hello"
res := Md5(str)
fmt.Println(res)
// Output:
// 5d41402abc4b2a76b9719d911017c592
}

View File

@@ -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]
}

View File

@@ -10,16 +10,18 @@ func Float32ToStr(f float32) string {
return fmt.Sprintf("%v", f) 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转字符串 // Float64ToStr float64转字符串
func Float64ToStr(f float64) string { func Float64ToStr(f float64) string {
return strconv.FormatFloat(f, 'f', -1, 64) return strconv.FormatFloat(f, 'f', -1, 64)
} }
// Float32ToFloat64 float32转float64
func Float32ToFloat64(f float32) float64 {
return float64(f)
}
// Float64ToFloat32 float64转float32 // Float64ToFloat32 float64转float32
func Float64ToFloat32(f float64) float32 { func Float64ToFloat32(f float64) float32 {
return float32(f) return float32(f)

View File

@@ -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
}

View File

@@ -12,7 +12,7 @@ func Int8ToStr(v int8) string {
return strconv.Itoa(int(v)) return strconv.Itoa(int(v))
} }
// Int16ToString 将int16类型转换为string类型 // Int16ToStr 将int16类型转换为string类型
func Int16ToStr(v int16) string { func Int16ToStr(v int16) string {
return strconv.Itoa(int(v)) return strconv.Itoa(int(v))
} }

View File

@@ -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
}

View File

@@ -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}
}

View File

@@ -17,9 +17,9 @@ import (
## Functions ## Functions
```go ```go
// MapValueExists 判断map中的value是否存在
func MapValueExists(m map[string]T, value T) bool
// MapKeyExists 判断map中的key是否存在 // 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
``` ```

View File

@@ -1,7 +1,13 @@
package mapUtil 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是否存在 // 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 { for _, v := range m {
if v == value { if v == value {
return true return true
@@ -9,9 +15,3 @@ func MapValueExists[T comparable](m map[string]T, value T) bool {
} }
return false return false
} }
// MapKeyExists 判断map中的key是否存在
func MapKeyExists(m map[string]any, key string) bool {
_, exists := m[key]
return exists
}

View File

@@ -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
}

View File

@@ -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]]
}

View File

@@ -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]
}

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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]
}

View File

@@ -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
}

View File

@@ -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]
}

View File

@@ -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]
}

View File

@@ -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
}

View File

@@ -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
}