官方文檔位址->: 文檔位址
一、概述
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 |