官方文档地址->:文档地址
一、概述
ZTool是在上个gotool的基础上延伸过来的,因为gotoo这个项目中间存在很多第三方库的依赖,然而第三方库停止更新,中间存在很多bug没有维护,造成工具包中间同样存在很多问题
最终经过考虑,停止维护gotool重新开一个工程,ztool不进行任何的第三方库依赖,进行开发。同时经过两年多的实际项目开发总结,将业务底层代码抽离,抽出了由,字符串、时间、随机id、http客户端、加密等工具集,同时我们也欢迎广大go爱好者进行pr,同时我们也开辟了pr通道
二、简介
ZTool是一个小巧而快捷工具库,通过方法封装,降低相关API的学习成本,提高工作效率,让使用GO语言开发编写代码更加优雅。
ZTool中的工具方法来自我们长达两年的多使用go语言进行实际项目开发总结而来,它涵盖了GO开发中常用的方法,它既是大型项目开发中解决小问题的利器,也是小型项目中的效率担当;
ZTool它节省了开发人员对项目中公用类和公用工具方法的封装时间,使开发专注于业务,同时可以最大限度的避免封装不完善带来的bug。
三、安装
- go开发环境安装
安装包下载地址:官方地址:https://go.dev/dl/
如果无法打开请使用备选地址:备选地址:https://go.dev/dl/
- go env来查看和验证go的环境信息,例如我的部分配置信息如下:
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/Janus/Library/Caches/go-build"
GOENV="/Users/Janus/Library/Application Support/go/env"
GOMODCACHE="/Users/Janus/gopath/pkg/mod"
GONOPROXY="github.com"
GONOSUMDB="github.com"
GOPATH="/Users/Janus/gopath"
GOPRIVATE="github.com"
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
<font color="red">注意:GO111MODULE必须是开启开启状态,若为开启请自行google或百度解决</font>
2、ztool工具包安装
- 版本要求
go >=1.15
- 版本检查
go version
- go get 安装
go get -u -b github.com/druidcaesa/ztool
- go mod安装
require github.com/druidcaesa/ztool
四、字符串操作工具
<font size="3">使用<font color="red"> ztool.StrUtils</font>调用对应的方法,ztool.StrUtils是字符串操作的核心类</font>
方法 | 参数说明 | 说明 |
---|---|---|
SnakeString | string | 驼峰转蛇形,返回类型为string |
CamelString | string | 蛇形转驼峰,返回类型为string |
HasBlank | string | 不可见字符串判空,返回bool类型 |
HasEmpty | string | 判断是否是空字符串(""),返回bool类型 |
RemoveSuffix | string | 删除文件后缀获取文件名,返回string |
RemovePrefix | string | 获取文件拓展名,返回string |
EncodeHexString | string | 字符串16进制编码,返回string |
DecodeHexString | string | 16进制转字符串,返回(string,error) |
五、时间操作工具
<font size="3">使用<font color="red"> ztool.DateUtils</font>调用对应的方法,ztool.DateUtils是时间用用的核心类</font>
ztool.DateUtils的返回类型是个struct以下是对结构体的说明,ztool.DateUtils可使用链式方法调用
type DateTime struct {
t time.Time //go的时间类型封装
weekStartDay common.Weekday //一周的开始时间设定
}
方法 | 参数说明 | 说明 |
---|---|---|
Now | 无参 | 获取当前时间,返回类型为DateTime结构体 |
Format | ...string可选参数 | 格式化时间,返回类型是string 字符串 |
SetTime | ...time.Time可选时间类型参数 | 设置时间,需要传入time.Time,返回类型为DateTime结构体 |
OperationDay | int 参数为int | 按照天对时间进行加减,参数可正负,返回类型为DateTime结构体 |
OperationMoon | int 参数为int | 按照月对时间进行加减,参数可正负,返回类型为DateTime结构体 |
OperationYears | int 参数为int | 按照年对时间进行加减,参数可正负,返回类型为DateTime结构体 |
StartOfHour | 无参 | 获取当前时间的开始时间,返回类型为DateTime结构体 |
StartTimeOfDay | 无参 | 获取当天的开时间,返回类型为DateTime结构体 |
StartOfMinute | 无参 | 获取分钟的开始,返回类型为DateTime结构体 |
StartOfWeek | 无参 | 获取一周的开时间,返回类型为DateTime结构体 |
StartOfMonth | 无参 | 获取当月的开始时间 ,返回类型为DateTime结构体 |
SetWeekStartDay | common.Monday | 设置一周的开始时间是那天 ,返回类型为DateTime结构体 |
StartOfQuarter | 无参 | 获取当前季度的开始时间 ,返回类型为DateTime结构体 |
StartOfYear | 无参 | 获取本年度开始时间 ,返回类型为DateTime结构体 |
EndOfMinute | 无参 | 获取当前分钟结束时间 ,返回类型为DateTime结构体 |
EndOfHour | 无参 | 获取当前小时的结束时间 ,返回类型为DateTime结构体 |
EndOfDay | 无参 | 获取今天的结束时间 ,返回类型为DateTime结构体 |
EndOfWeek | 无参 | 获取本周结束时间 ,返回类型为DateTime结构体 |
EndOfMonth | 无参 | 获取本月结束时间 ,返回类型为DateTime结构体 |
EndOfQuarter | 无参 | 获取本季度结束时间 ,返回类型为DateTime结构体 |
EndOfYear | 无参 | 获取本年度结束时间 ,返回类型为DateTime结构体 |
Parse | ...string | 字符串格式化时间 ,返回类型为DateTime结构体 |
六、加密操作工具
<font size="3">使用<font color="red"> ztool.EncryptionUtils</font>调用对应的方法,ztool.EncryptionUtils是加密工具的核心类</font>
方法 | 参数说明 | 说明 |
---|---|---|
AesEncrypt | string,...string | AES加密方法,返回类型为(string,error) |
AesDecrypt | string,...string | AES解密方法,返回类型为(string,error) |
DesDecrypt | string,...string | DES解密方法,返回类型为(string,error) |
DesDecrypt | string,...string | DES解密方法,返回类型为(string,error) |
Md5Encode | string | Md5签名方法,返回类型为string |
Md5Check | string,string | Md5Check,返回类型为boole |
七、Http客户端
<font size="3">使用<font color="red"> ztool.HttpUtils</font>调用对应的方法,ztool.HttpUtils是http客户端工具的核心类</font>
方法 | 参数说明 | 说明 |
---|---|---|
Get | string,...interface{} | 发送GET请求,返回类型为(string, error) |
Post | string,...interface{} | 发送POST请求,返回类型为(string, error) |
Put | string,...interface{} | 发送PUT请求,返回类型为(string, error) |
Delete | string,...interface{} | 发送DELETE请求,返回类型为(string, error) |
UploadFilePath | url, filename, filePath string | 文件上传,返回类型为(string, error) |
NewRequest | 无参 | 获取request,返回类型为request 结构体 |
- request结构体是自定义结构体,里面有原http的request的对应封装
// Request request subject
type Request struct {
//request client
cli *http.Client
//Timeout setting
timeout time.Duration
//request handler
headers map[string]string
//request parameters
data interface{}
//logger turn on
logger bool
//specifies the cookie jar
jar http.CookieJar
//For details of the agent, please refer to the official documentation
proxy func(*http.Request) (*url.URL, error)
//CheckRedirect specifies the policy for handling redirects
checkRedirect func(req *http.Request, via []*http.Request) error
//cookies setting
cookies map[string]string
//set request method
method string
//set request url
url string
tlsClientConfig *tls.Config
disableKeepAlive bool
transport *http.Transport
debug bool
}
八、加密工具
<font size="3">使用<font color="red"> ztool.IdUtils</font>调用对应的方法,ztool.IdUtils是ID生成工具的核心类</font>
方法 | 参数说明 | 说明 |
---|---|---|
RandomUUID | 无参 | 带有"-"的UUID,返回类型为(string,error) |
SimpleUUID | 无参 | 不带"-"的UUID,返回类型为(string,error) |
GenerateSnowflakeId | 无参 | SnowFlake算法的ID,返回类型为int |
九、身份验证
<font size="3">使用<font color="red"> ztool.IDCardUtils</font>,现在支持大陆15位、18位身份证</font>
方法 | 参数说明 | 说明 |
---|---|---|
Check | string | 全局校验身份证,返回类型为(bool, error) |
GetProvinceByIdCard | string | 获取身份证所在地,string |
IsValidCard | string | 身份证是否符合18或者15位,返回类型为(bool, error) |
Convert15To18 | string | 15位转18位,返回类型为string |
GetBirthByIdCard | string | 获取生日,返回类型为(string, error) |
GetAgeByIdCard | string | 获取年龄,返回类型为int |
GetYearByIdCard | string | 获取出生年,返回类型为string |
GetMonthByIdCard | string | 获取出生月,返回类型为string |
GetDayByIdCard | string | 获取出生日,返回类型为string |
GetSex | string | 获取性别,返回类型为int |