关于go:ZTool一款Go语言非常好用开发工具集

官网文档地址->:文档地址

一、概述

ZTool是在上个gotool的根底上延长过去的,因为gotoo这个我的项目两头存在很多第三方库的依赖,然而第三方库进行更新,两头存在很多bug没有保护,造成工具包两头同样存在很多问题
最终通过思考,进行保护gotool从新开一个工程,ztool不进行任何的第三方库依赖,进行开发。同时通过两年多的理论我的项目开发总结,将业务底层代码抽离,抽出了由,字符串、工夫、随机id、http客户端、加密等工具集,同时咱们也欢送宽广go爱好者进行pr,同时咱们也开拓了pr通道

二、简介

ZTool是一个玲珑而快捷工具库,通过办法封装,升高相干API的学习老本,进步工作效率,让应用GO语言开发编写代码更加优雅。

ZTool中的工具办法来自咱们长达两年的多应用go语言进行理论我的项目开发总结而来,它涵盖了GO开发中罕用的办法,它既是大型项目开发中解决小问题的利器,也是小型我的项目中的效率担当;

ZTool它节俭了开发人员对我的项目中专用类和专用工具办法的封装工夫,使开发专一于业务,同时能够最大限度的防止封装不欠缺带来的bug。

三、装置

  1. go开发环境装置
  • 安装包下载地址:官网地址:https://go.dev/dl/
  • 如果无奈关上请应用备选地址:备选地址:https://go.dev/dl/
  1. 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工具包装置

  1. 版本要求
go >=1.15 
  1. 版本查看
go version 
  1. go get 装置
go get -u -b github.com/druidcaesa/ztool
  1. 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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理