介绍
这是一个基于 Go 语言开发的通用数据类型解决工具类,帮忙开发者在业务代码实现中解决常见的数据类型和数据操作。能够让您专一于您的业务代码的实现,而免去解决根本数据类型转换和验证的性能。该工具库无侵入式的设计能够让您的业务代码更容易浏览和优雅。
疾速开始
装置
应用 Go1.18
及以上版本的用户,倡议装置 v2.x.x
。 因为 v2.x.x
利用 Go1.18
的泛型重写了大部分函数
go get -u github.com/jefferyjob/go-easy-utils
应用 Go1.18
以下版本的用户,必须装置 v1.x.x
。目前最新的 v1
版本是 v1.1.0
go get github.com/jefferyjob/go-easy-utils@v1.1.0
应用Demo
package mainimport ( "fmt" "github.com/jefferyjob/go-easy-utils/sliceUtil")func main() { var slice = []string{"this", "is", "go", "easy", "utils"} chunkSlice := sliceUtil.ChunkSlice(slice, 2) fmt.Printf("%v", chunkSlice)}
性能列表
jsonUtil Json解决工具
// JsonToStruct 将 JSON 字符串解析为指定的构造体指针func JsonToStruct(jsonData string, result any) error
ValidUtil 验证工具
// IsTime 验证是否为工夫格局(HH:mm:ss)func IsTime(str string) bool// IsDate 验证是否为日期格局(yyyy-MM-dd)func IsDate(str string) bool// IsDateTime 验证是否为日期工夫格局(yyyy-MM-dd HH:mm:ss)func IsDateTime(str string) bool// IsIDCard 验证身份证号(18或15位)func IsIDCard(str string) bool// IsIDCard18 验证18位身份证号func IsIDCard18(id string) bool// IsIDCard15 验证15位身份证号func IsIDCard15(idCard string) bool// IsMobile 验证是否为手机号码func IsMobile(mobileNum string) bool // IsTelephone 验证是否为座机号码func IsTelephone(telephone string) bool // IsPostalCode 验证是否为邮编号码func IsPostalCode(str string) bool // IsDecimal 验证给定的字符串小数点后是否最多两位func IsDecimal(input string) bool // IsNumber 验证是否全副为数字func IsNumber(input string) bool// IsBankCardNo 验证是否为银行卡号func IsBankCardNo(str string) bool// IsAllChinese 验证给定的字符串全副为中文func IsAllChinese(input string) bool// IsContainChinese 验证给定的字符串蕴含中文func IsContainChinese(input string) bool// IsEmail 是否为emailfunc IsEmail(input string) bool// IsIPv4 是否为ipv4地址func IsIPv4(input string) bool// IsIPv6 是否为ipv6地址func IsIPv6(input string) bool// IsURL 是否为URL地址func IsURL(input string) bool// IsJSON 是否为Jsonfunc IsJSON(input string) bool// IsChineseName 验证是否为中文名func IsChineseName(name string) bool// IsEnglishName 验证是否为英文名func IsEnglishName(name string) bool// IsQQ 验证是否为QQ号func IsQQ(qq string) bool // IsWeChat 验证是否为微信号func IsWeChat(wechat string) bool// IsWeibo 验证是否为微博IDfunc IsWeibo(weibo string) bool// IsPassword 验证明码是否非法// 明码长度在6-20个字符之间,只蕴含数字、字母和下划线func IsPassword(password string) bool
strUtil 字符串工具
// StrToInt string转intfunc StrToInt(v string) int// StrToInt8 string转int8func StrToInt8(v string) int8// StrToInt16 string转int16func StrToInt16(v string) int16// StrToInt32 string转int32func StrToInt32(v string) int32// StrToInt64 string转int64func StrToInt64(v string) int64// StrToUint string转uintfunc StrToUint(v string) uint// StrToUint8 string转uint8func StrToUint8(v string) uint8// StrToUint16 string转uint16func StrToUint16(v string) uint16// StrToUint32 string转uint32func StrToUint32(v string) uint32// StrToUint64 string转uint64func StrToUint64(v string) uint64// StrToBytes 字符串转字节数组func StrToBytes(v string) []byte
sliceUtil 切片解决工具
// Chunk 把slice宰割为新的数组块func ChunkSlice(slice []T, size int) [][]T// Column 获取slice中某个繁多列的值func ColumnSlice(slice []T, column string) []any// In 判断value是否在slice中func InSlice(value T, slices []T) bool// Is 判断指定值i是否是slice类型func IsSlice(slice any) bool// Merge 将多个slice合并成一个slicefunc MergeSlice(slices ...[]T) []T// Sum 对slice中的元素求和func SumSlice(slice []T) T// Unique 移除slice中反复的值func UniqueSlice(slice []T) []T
mapUtil map类型解决
// MapKeyExists 判断map中的key是否存在func MapKeyExists((m map[T]T2, key T)) bool// MapValueExists 判断map中的value是否存在func MapValueExists(m map[T2]T, value T) bool
mathUtil
// Abs 返回一个数的绝对值func Abs(num T) T// Ceil 对float数据向上取整func Ceil(num T) int// Floor 对float数据向下取整func Floor(num T) int// Max 返回slice中最大值func Max(slice []T) T// Min 返回slice中最小值func Min(slice []T) T// Round 对float数据四舍五入func Round(num T) int
floatUtil 浮点型解决
// Float32ToStr float32转字符串func Float32ToStr(f float32) string// Float64ToStr float64转字符串func Float64ToStr(f float64) string// Float32ToFloat64 float32转float64func Float32ToFloat64(f float32) float64// Float64ToFloat32 float64转float32func Float64ToFloat32(f float64) float32
emoji表情包
// DecodeEmojiUnicode Emoji表情解码func DecodeEmojiUnicode(unicode string) string// EncodeEmojiUnicode Emoji表情编码func EncodeEmojiUnicode(emoji string) string
cryptoUtil 加密与解密
// HashSHA256 hash加密func HashSHA256(str string) string// Md5 MD5加密func Md5(string string) string
byteUtil 字节数组
// BytesToStr 字节数组转字符串func BytesToStr(data []byte) string
anyUtil 任意类型转换
// AnyToFloat32 将给定的值转换为float32func AnyToFloat32(i any) (float32, error)// AnyToFloat64 将给定的值转换为float64func AnyToFloat64(i any) (float64, error)// AnyToInt 将给定的值转换为 intfunc AnyToInt(i any) (int, error)// AnyToInt8 将给定的值转换为 int8func AnyToInt8(i any) (int8, error)// AnyToInt16 将给定的值转换为 int16func AnyToInt16(i any) (int16, error)// AnyToInt32 将给定的值转换为 int32func AnyToInt32(i any) (int32, error)// AnyToInt64 将给定的值转换为 int64func AnyToInt64(i any) (int64, error)// AnyToStr 任意类型数据转stringfunc AnyToStr(i any) string// AnyToUint 将给定的值转换为 uintfunc AnyToUint(i any) (uint, error)// AnyToUint8 将给定的值转换为 uint8func AnyToUint8(i any) (uint8, error)// AnyToUint16 将给定的值转换为 uint16func AnyToUint16(i any) (uint16, error)// AnyToUint32 将给定的值转换为 uint32func AnyToUint32(i any) (uint32, error)// AnyToUint64 将给定的值转换为 uint64func AnyToUint64(i any) (uint64, error)// AnyToBool 将给定的值转换为boolfunc AnyToBool(i any) bool