乐趣区

关于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
退出移动版