From 8b04aa5f31d8e4262f23f1f99f062e4a9d8d1bc7 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Mon, 7 Feb 2022 16:35:01 +0800 Subject: [PATCH] release v1.2.3 update readme file --- README.md | 733 ++++++++++++++--------------------------- README_zh-CN.md | 741 ++++++++++++++---------------------------- docs/cryptor_zh-CN.md | 61 ++-- 3 files changed, 502 insertions(+), 1033 deletions(-) diff --git a/README.md b/README.md index 2a1ee30..fca3a7b 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@

![Go version](https://img.shields.io/badge/go-%3E%3D1.16-9cf) -[![Release](https://img.shields.io/badge/release-1.2.2-green.svg)](https://github.com/duke-git/lancet/releases) +[![Release](https://img.shields.io/badge/release-1.2.3-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) @@ -56,543 +56,282 @@ func main() { ``` ## API Documentation -### 1. convertor contains some functions for data convertion - -- Support conversion between commonly used data types. -- Usage: import "github.com/duke-git/lancet/convertor" +### Convertor package contains some functions for data convertion. ```go -package main +import "github.com/duke-git/lancet/convertor" +``` +#### Function list: +- [ColorHexToRGB](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ColorHexToRGB) +- [ColorRGBToHex](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ColorRGBToHex) +- [ToBool](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToBool) +- [ToBytes](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToBytes) +- [ToChar](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToChar) +- [ToInt](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToInt) +- [ToJson](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToJson) +- [ToString](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#ToString) +- [StructToMap](https://github.com/duke-git/lancet/blob/main/docs/convertor.md#StructToMap) + +### Cryptor package is for data encryption and decryption. +```go +import "github.com/duke-git/lancet/cryptor" +``` + +#### Function list: +- [AesEcbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesEcbEncrypt) +- [AesEcbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesEcbDecrypt) +- [AesCbcEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesCbcEncrypt) +- [AesCbcDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesCbcDecrypt) +- [AesCtrCrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesCtrCrypt) +- [AesCfbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesCfbEncrypt) +- [AesCfbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesCfbDecrypt) +- [AesOfbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesOfbEncrypt) +- [AesOfbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#AesOfbDecrypt) +- [Base64StdEncode](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Base64StdEncode) +- [Base64StdDecode](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Base64StdDecode) +- [DesEcbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesEcbEncrypt) +- [DesEcbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesEcbDecrypt) +- [DesCbcEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesCbcEncrypt) +- [DesCbcDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesCbcDecrypt) +- [DesCtrCrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesCtrCrypt) +- [DesCfbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesCfbEncrypt) +- [DesCfbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesCfbDecrypt) +- [DesOfbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesOfbEncrypt) +- [DesOfbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#DesOfbDecrypt) +- [HmacMd5](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacMd5) +- [HmacSha1](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha1) +- [HmacSha256](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha256) +- [HmacSha512](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#HmacSha512) +- [Md5String](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5String) +- [Md5File](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Md5File) +- [Sha1](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha1) +- [Sha256](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha256) +- [Sha512](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#Sha512) +- [GenerateRsaKey](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#GenerateRsaKey) +- [RsaEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#RsaEncrypt) +- [RsaDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor.md#RsaDecrypt) + +### Datetime package supports date and time format and compare. + + +```go +import "github.com/duke-git/lancet/datetime" +``` +#### Function list: +- [AddDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddDay) +- [AddHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddHour) +- [AddMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddMinute) +- [GetNowDate](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDate) +- [GetNowTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowTime) +- [GetNowDateTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDateTime) +- [GetZeroHourTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetZeroHourTimestamp) +- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNightTimestamp) +- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatTimeToStr) +- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatStrToTime) + +### Fileutil package implements some basic functions for file operations. + +```go +import "github.com/duke-git/lancet/fileutil" +``` + +#### Function list: + +- [ClearFile](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#ClearFile) +- [CreateFile](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#CreateFile) +- [CopyFile](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#CopyFile) +- [FileMode](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#FileMode) +- [MiMeType](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#MiMeType) +- [IsExist](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#IsExist) +- [IsLink](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#IsLink) +- [IsDir](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#IsDir) +- [ListFileNames](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#ListFileNames) +- [RemoveFile](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#RemoveFile) +- [ReadFileToString](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#ReadFileToString) +- [ReadFileByLine](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#ReadFileByLine) +- [Zip](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#Zip) +- [UnZip](https://github.com/duke-git/lancet/blob/main/docs/fileutil.md#UnZip) + +### Formatter contains some functions for data formatting. + +```go import ( - "fmt" - "github.com/duke-git/lancet/convertor" -) - -func main() { - s := "12.3" - f, err := convertor.ToFloat(s) - if err != nil { - fmt.Errorf("error is %s", err.Error()) - } - fmt.Println(f) // 12.3 -} -``` - -- Function list: - -```go -func ColorHexToRGB(colorHex string) (red, green, blue int) //convert color hex to color rgb -func ColorRGBToHex(red, green, blue int) string //convert color rgb to color hex -func ToBool(s string) (bool, error) //convert string to a boolean -func ToBytes(data interface{}) ([]byte, error) //convert interface to bytes -func ToChar(s string) []string //convert string to char slice -func ToFloat(value interface{}) (float64, error) //convert value to float64, if input is not a float return 0.0 and error -func ToInt(value interface{}) (int64, error) //convert value to int64, if input is not a numeric format return 0 and error -func ToJson(value interface{}) (string, error) //convert value to a valid json string -func ToString(value interface{}) string //convert value to string -func StructToMap(value interface{}) (map[string]interface{}, error) //convert struct to map, only convert exported field, tag `json` should be set -``` - -### 2. cryptor is for data encryption and decryption - -- Support md5, hmac, aes, des, rsa. -- Usage: import "github.com/duke-git/lancet/cryptor" - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/cryptor" -) - -func main() { - data := "hello" - key := "abcdefghijklmnop" - - encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key)) - decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key)) - fmt.Println(string(decrypted)) // hello -} -``` - -- Function list: - -```go -func AesEcbEncrypt(data, key []byte) []byte //AES ECB encrypt -func AesEcbDecrypt(encrypted, key []byte) []byte //AES ECB decrypt -func AesCbcEncrypt(data, key []byte) []byte //AES CBC encrypt -func AesCbcDecrypt(encrypted, key []byte) []byte //AES CBC decrypt -func AesCtrCrypt(data, key []byte) []byte //AES CTR encrypt / decrypt -func AesCfbEncrypt(data, key []byte) []byte //AES CFB encrypt -func AesCfbDecrypt(encrypted, key []byte) []byte //AES CFB decrypt -func AesOfbEncrypt(data, key []byte) []byte //AES OFB encrypt -func AesOfbDecrypt(data, key []byte) []byte //AES OFB decrypt -func Base64StdEncode(s string) string //base64 encode -func Base64StdDecode(s string) string //base64 decode -func DesEcbEncrypt(data, key []byte) []byte //DES ECB encrypt -func DesEcbDecrypt(encrypted, key []byte) []byte //DES ECB decrypt -func DesCbcEncrypt(data, key []byte) []byte //DES CBC encrypt -func DesCbcDecrypt(encrypted, key []byte) []byte //DES CBC decrypt -func DesCtrCrypt(data, key []byte) []byte //DES CTR encrypt/decrypt -func DesCfbEncrypt(data, key []byte) []byte //DES CFB encrypt -func DesCfbDecrypt(encrypted, key []byte) []byte //DES CFB decrypt -func DesOfbEncrypt(data, key []byte) []byte //DES OFB encrypt -func DesOfbDecrypt(data, key []byte) []byte //DES OFB decrypt -func HmacMd5(data, key string) string //get hmac md5 value -func HmacSha1(data, key string) string //get hmac sha1 value -func HmacSha256(data, key string) string //get hmac sha256 value -func HmacSha512(data, key string) string //get hmac sha512 value -func Md5String(s string) string //return the md5 value of string -func Md5File(filename string) (string, error) //return the md5 value of file -func Sha1(data string) string //get sha1 value -func Sha256(data string) string //getsha256 value -func Sha512(data string) string //get sha512 value -func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error //generate RSA pem file -func RsaEncrypt(data []byte, pubKeyFileName string) []byte //RSA encrypt -func RsaDecrypt(data []byte, privateKeyFileName string) []byte //RSA decrypt - -``` - -### 3. datetime parse and format datetime - -- Parse and format datetime -- Usage: import "github.com/duke-git/lancet/datetime" - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/datetime" -) - -func main() { - now := time.Now() - s := datetime.FormatTimeToStr(now, "yyyy-mm-dd hh:mm:ss") - fmt.Println(s) // 2021-11-24 11:16:55 -} -``` - -- Function list: - -```go -func AddDay(t time.Time, day int64) time.Time //add or sub days to time -func AddHour(t time.Time, hour int64) time.Time //add or sub hours to time -func AddMinute(t time.Time, minute int64) time.Time //add or sub minutes to time -func GetNowDate() string //get current date, format is yyyy-mm-dd -func GetNowTime() string //get current time, format is hh:mm:ss -func GetNowDateTime() string //get current date and time, format is yyyy-mm-dd hh:mm:ss -func GetZeroHourTimestamp() int64 //return timestamp of zero hour (timestamp of 00:00) -func GetNightTimestamp() int64 //return timestamp of zero hour (timestamp of 23:59) -func FormatTimeToStr(t time.Time, format string) string //convert time to string -func FormatStrToTime(str, format string) time.Time //convert string to time -``` - -### 4. fileutil basic functions for file operations - -- Basic functions for file operations. -- Usage: import "github.com/duke-git/lancet/fileutil" - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/fileutil" -) - -func main() { - fmt.Println(fileutil.IsDir("./")) // true -} -``` - -- Function list: - -```go -func ClearFile(path string) error //write empty string to path file -func CreateFile(path string) bool // create a file in path -func CopyFile(srcFilePath string, dstFilePath string) error //copy src file to dst file -func FileMode(path string) (fs.FileMode, error) //return file's mode and permission -func MiMeType(file interface{}) string //return file mime type, file should be string or *os.File -func IsExist(path string) bool //checks if a file or directory exists -func IsLink(path string) bool //checks if a file is symbol link or not -func IsDir(path string) bool //checks if the path is directy or not -func ListFileNames(path string) ([]string, error) //return all file names in the path -func RemoveFile(path string) error //remove the path file -func ReadFileToString(path string) (string, error) //return string of file content -func ReadFileByLine(path string)([]string, error) //read file content by line -func Zip(fpath string, destPath string) error //create zip file, fpath could be a single file or a directory -func UnZip(zipFile string, destPath string) error //unzip the file and save it to destPath -``` - -### 5. formatter is for data format - -- Contain some formatting function -- Usage: import "github.com/duke-git/lancet/formatter" - -```go -package main - -import ( - "fmt" "github.com/duke-git/lancet/formatter" ) - -func main() { - fmt.Println(formatter.Comma("12345", "")) // "12,345" - fmt.Println(formatter.Comma(12345.67, "¥")) // "¥12,345.67" -} ``` +#### Function list: +- [Comma](https://github.com/duke-git/lancet/blob/main/docs/formatter.md#Comma) -- Function list: +### Function package can control the flow of function execution and support part of functional programming ```go -func Comma(v interface{}, symbol string) string //add comma to number by every 3 numbers from right. ahead by symbol char +import "github.com/duke-git/lancet/function" ``` -### 6. function can control the function execution and support functional programming +#### Function list: +- [After](https://github.com/duke-git/lancet/blob/main/docs/function.md#After) +- [Before](https://github.com/duke-git/lancet/blob/main/docs/function.md#Before) +- [Curry](https://github.com/duke-git/lancet/blob/main/docs/function.md#Curry) +- [Compose](https://github.com/duke-git/lancet/blob/main/docs/function.md#Compose) +- [Debounced](https://github.com/duke-git/lancet/blob/main/docs/function.md#Debounced) +- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function.md#Delay) +- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function.md#Delay) +- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher) -- Control function execution and support functional programming. -- Usage: import "github.com/duke-git/lancet/function" +### Netutil package contains functions to get net information and send http request. ```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/function" -) - -func main() { - var print = func(s string) { - fmt.Println(s) - } - function.Delay(2*time.Second, print, "hello world") -} +import "github.com/duke-git/lancet/netutil" ``` -- Function list: +#### Function list: +- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ConvertMapToQueryString) +- [GetInternalIp](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetInternalIp) +- [GetPublicIpInfo](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#GetPublicIpInfo) +- [IsPublicIP](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#IsPublicIP) +- [HttpGet](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpGet) +- [HttpDelete](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpDelete) +- [HttpPost](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPost) +- [HttpPut](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPut) +- [HttpPatch](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#HttpPatch) +- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil.md#ParseHttpResponse) + +### Random package implements some basic functions to generate random int and string. ```go -func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value //creates a function that invokes func once it's called n or more times -func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value //creates a function that invokes func once it's called less than n times -func (f Fn) Curry(i interface{}) func(...interface{}) interface{} //make a curryed function -func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) interface{} //compose the functions from right to left -func Debounced(fn func(), duration time.Duration) func() //creates a debounced function that delays invoking fn until after wait duration have elapsed since the last time the debounced function was invoked. -func Delay(delay time.Duration, fn interface{}, args ...interface{}) //invoke function after delayed time -func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool //invoke function every duration time, until close the returned bool chan -func (w *Watcher) Start() //start the watch timer. -func (w *Watcher) Stop() //stop the watch timer -func (w *Watcher) Reset() {} //reset the watch timer. -func (w *Watcher) GetElapsedTime() time.Duration //return time duration from watcher start to end. +import "github.com/duke-git/lancet/random" ``` -### 7. netutil is for net process +#### Function list: +- [RandBytes](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandBytes) +- [RandInt](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandInt) +- [RandString](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandString) -- Ip and http request method. -- Usage: import "github.com/duke-git/lancet/netutil". -- The Http function params order:url, header, query string, body, httpclient. +### Retry package is for executing a function repeatedly until it was successful or canceled by the context. ```go -package main - -import ( - "fmt" - "io/ioutil" - "log" - "github.com/duke-git/lancet/netutil" -) - -func main() { - url := "https://gutendex.com/books?" - header := make(map[string]string) - header["Content-Type"] = "application/json" - queryParams := make(map[string]interface{}) - queryParams["ids"] = "1" - - resp, err := netutil.HttpGet(url, header, queryParams) - if err != nil { - log.Fatal(err) - } - - body, _ := ioutil.ReadAll(resp.Body) - fmt.Println("response: ", resp.StatusCode, string(body)) -} +import "github.com/duke-git/lancet/retry" ``` -- Function list: +#### Function list: +- [Context](https://github.com/duke-git/lancet/blob/main/docs/retry.md#Context) +- [Retry](https://github.com/duke-git/lancet/blob/main/docs/retry.md#Retry) +- [RetryFunc](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryFunc) +- [RetryDuration](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryDuration) +- [RetryTimes](https://github.com/duke-git/lancet/blob/main/docs/retry.md#RetryTimes) + +### Slice contains some functions to manipulate slice. ```go -func GetInternalIp() string //get internal ip -func GetPublicIpInfo() (*PublicIpInfo, error) //get public ip info: country, region, isp, city, lat, lon, ip -func IsPublicIP(IP net.IP) bool //判断ip是否为公共ip -func HttpGet(url string, params ...interface{}) (*http.Response, error) //http get request -func HttpPost(url string, params ...interface{}) (*http.Response, error) //http post request -func HttpPut(url string, params ...interface{}) (*http.Response, error) //http put request -func HttpDelete(url string, params ...interface{}) (*http.Response, error) //http delete request -func HttpPatch(url string, params ...interface{}) (*http.Response, error) //http patch request -func ConvertMapToQueryString(param map[string]interface{}) string //convert map to url query string -func ParseHttpResponse(resp *http.Response, obj interface{}) error //decode http response to specified interface +import "github.com/duke-git/lancet/slice" ``` -### 8. random is for rand string and int generation +#### Function list: +- [Contain](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Contain) +- [ContainSubSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ContainSubSlice) +- [Chunk](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Chunk) +- [Compact](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Compact) +- [Concat](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Concat) +- [Count](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Count) +- [Difference](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Difference) +- [DifferenceBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DifferenceBy) +- [DeleteByIndex](https://github.com/duke-git/lancet/blob/main/docs/slice.md#DeleteByIndex) +- [Drop](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Drop) +- [Every](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Every) +- [Filter](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Filter) +- [Find](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Find) +- [FindLast](https://github.com/duke-git/lancet/blob/main/docs/slice.md#FindLast) +- [FlattenDeep](#FlattenDeep) +- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ForEach) +- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice.md#GroupBy) +- [IntSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#IntSlice) +- [InterfaceSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#InterfaceSlice) +- [Intersection](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Intersection) +- [InsertByIndex](https://github.com/duke-git/lancet/blob/main/docs/slice.md#InsertByIndex) +- [Map](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Map) +- [ReverseSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#ReverseSlice) +- [Reduce](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Reduce) +- [Shuffle](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Shuffle) +- [SortByField](https://github.com/duke-git/lancet/blob/main/docs/slice.md#SortByField) +- [Some](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Some) +- [StringSlice](https://github.com/duke-git/lancet/blob/main/docs/slice.md#StringSlice) +- [Unique](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Unique) +- [Union](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Union) +- [UpdateByIndex](https://github.com/duke-git/lancet/blob/main/docs/slice.md#UpdateByIndex) +- [Without](https://github.com/duke-git/lancet/blob/main/docs/slice.md#Without) -- Generate random string and int. -- Usage: import "github.com/duke-git/lancet/random". +### Strutil package contains some functions to manipulate string. ```go -package main - -import ( - "fmt" - "io/ioutil" - "log" - "github.com/duke-git/lancet/random" -) - -func main() { - randStr := random.RandString(6) - fmt.Println(randStr) -} +import "github.com/duke-git/lancet/strutil" ``` -- Function list: +#### Function list: + +- [After](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#After) +- [AfterLast](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#AfterLast) +- [Before](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Before) +- [BeforeLast](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#BeforeLast) +- [CamelCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#CamelCase) +- [Capitalize](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Capitalize) +- [IsString](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#IsString) +- [KebabCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#KebabCase) +- [LowerFirst](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#LowerFirst) +- [UpperFirst](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperFirst) +- [PadEnd](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadEnd) +- [PadStart](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadStart) +- [ReverseStr](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#ReverseStr) +- [SnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SnakeCase) +- [Wrap](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap) +- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Unwrap) + +### System package contain some functions about os, runtime, shell command. ```go -func RandBytes(length int) []byte //generate random []byte -func RandInt(min, max int) int //generate random int -func RandString(length int) string //generate random string +import "github.com/duke-git/lancet/system" ``` -### 9. retry is for executing a function repeatedly until it was successful or canceled by the context. +#### Function list: +- [IsWindows](https://github.com/duke-git/lancet/blob/main/docs/system.md#IsWindows) +- [IsLinux](https://github.com/duke-git/lancet/blob/main/docs/system.md#IsLinux) +- [IsMac](https://github.com/duke-git/lancet/blob/main/docs/system.md#IsMac) +- [GetOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system.md#GetOsEnv) +- [SetOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system.md#SetOsEnv) +- [RemoveOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system.md#RemoveOsEnv) +- [CompareOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system.md#CompareOsEnv) +- [ExecCommand](https://github.com/duke-git/lancet/blob/main/docs/system.md#ExecCommand) -- Executes a function repeatedly until it was successful or canceled by the context. -- Default retry times is 5, default retry duration is 3 second. -- Usage: import "github.com/duke-git/lancet/retry". +### Validator package contains some functions for data validation. ```go -package main - -import ( - "fmt" - "io/ioutil" - "log" - "github.com/duke-git/lancet/retry" -) - -func main() { - var number int - increaseNumber := func() error { - number++ - if number == 3 { - return nil - } - return errors.New("error occurs") - } - - err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50)) - - fmt.Println(number) //3 -} +import "github.com/duke-git/lancet/validator" ``` +#### Function list: -- Function list: - -```go -type RetryFunc func() error //function that retry executes -func RetryTimes(n uint) //set times of retry -func RetryDuration(d time.Duration) //generate random string -func Context(ctx context.Context) //set retry context config -func Retry(retryFunc RetryFunc, opts ...Option) error //executes the retryFunc repeatedly until it was successful or canceled by the context -``` - -### 10. slice is for process slice - -- Contain function for process slice. -- Usage: import "github.com/duke-git/lancet/slice" -- Due to the unstable support of generic, most of the slice processing function parameter and return value is interface {}. After go generic is stable, the related functions will be refactored. - -```go -package main - -import ( - "fmt" - "io/ioutil" - "log" - "github.com/duke-git/lancet/slice" -) - -func main() { - nums := []int{1, 4, 3, 4, 6, 7, 3} - uniqueNums, _ := slice.IntSlice(slice.Unique(nums)) - fmt.Println(uniqueNums) //[1 4 3 6 7] -} -``` - -- Function list: - -```go -func Contain(slice interface{}, value interface{}) bool //check if the value is in the slice or not -func ContainSubSlice(slice interface{}, subslice interface{}) bool //check if the slice contain subslice or not -func Chunk(slice []interface{}, size int) [][]interface{} //creates an slice of elements split into groups the length of `size` -func Compact(slice interface{}) interface{} //creates an slice with all falsey values removed. The values false, nil, 0, and "" are falsey -func Concat(slice interface{}, values ...interface{}) interface{} //creates a new slice concatenating slice with any additional slices and/or values -func Difference(slice1, slice2 interface{}) interface{} //creates an slice of whose element not included in the other given slice -func DifferenceBy(slice interface{}, comparedSlice interface{}, iterateeFn interface{}) interface{} //it accepts iteratee which is invoked for each element of slice and values to generate the criterion by which they're compared. -func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error) //delete the element of slice from start index to end index - 1 -func Drop(slice interface{}, n int) interface{} //creates a slice with `n` elements dropped from the beginning when n > 0, or `n` elements dropped from the ending when n < 0 -func Every(slice, function interface{}) bool //return true if all of the values in the slice pass the predicate function, function signature should be func(index int, value interface{}) bool -func None(slice, function interface{}) bool // return true if all the values in the slice mismatch the criteria -func Filter(slice, function interface{}) interface{} //filter slice, function signature should be func(index int, value interface{}) bool -func Find(slice, function interface{}) (interface{}, bool) //iterates over elements of slice, returning the first one that passes a truth test on function.function signature should be func(index int, value interface{}) bool -func FlattenDeep(slice interface{}) interface{} //flattens slice recursive -func ForEach(slice, function interface{}) //iterates over elements of slice and invokes function for each element, function signature should be func(index int, value interface{}) -func IntSlice(slice interface{}) ([]int, error) //convert value to int slice -func InterfaceSlice(slice interface{}) []interface{} //convert value to interface{} slice -func Intersection(slices ...interface{}) interface{} //creates a slice of unique values that included by all slices. -func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //insert the element into slice at index. -func Map(slice, function interface{}) interface{} //map lisce, function signature should be func(index int, value interface{}) interface{} -func ReverseSlice(slice interface{}) //revere slice -func Reduce(slice, function, zero interface{}) interface{} //reduce slice, function signature should be func(index int, value1, value2 interface{}) interface{} -func Shuffle(slice interface{}) interface{} //creates an slice of shuffled values -func SortByField(slice interface{}, field string, sortType ...string) error //sort struct slice by field -func Some(slice, function interface{}) bool //return true if any of the values in the list pass the predicate function, function signature should be func(index int, value interface{}) bool -func StringSlice(slice interface{}) []string //convert value to string slice -func Unique(slice interface{}) interface{} //remove duplicate elements in slice -func Union(slices ...interface{}) interface{} //Union creates a slice of unique values, in order, from all given slices. using == for equality comparisons -func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //update the slice element at index. -func Without(slice interface{}, values ...interface{}) interface{} //creates a slice excluding all given values -func GroupBy(slice, function interface{}) (interface{}, interface{}) // groups slice into two categories -func Count(slice, function interface{}) int // Count iterates over elements of slice, returns a count of all matched elements -``` - -### 11. strutil is for processing string - -- Contain functions to precess string -- Usage: import "github.com/duke-git/lancet/strutil" - -```go -package main - -import ( - "fmt" - "io/ioutil" - "log" - "github.com/duke-git/lancet/strutil" -) - -func main() { - str := "Foo-Bar" - camelCaseStr := strutil.CamelCase(str) - fmt.Println(camelCaseStr) //fooBar -} -``` - -- Function list: - -```go -func After(s, char string) string //create substring in source string after position when char first appear -func AfterLast(s, char string) string //create substring in source string after position when char last appear -func Before(s, char string) string //create substring in source string before position when char first appear -func BeforeLast(s, char string) string //create substring in source string before position when char last appear -func CamelCase(s string) string //covert string to camelCase string. "foo bar" -> "fooBar" -func Capitalize(s string) string //convert the first character of a string to upper case, "fOO" -> "Foo" -func IsString(v interface{}) bool //check if the value data type is string or not -func KebabCase(s string) string //covert string to kebab-case, "foo_Bar" -> "foo-bar" -func LowerFirst(s string) string //convert the first character of string to lower case -func UpperFirst(s string) string //converts the first character of string to upper case -func PadEnd(source string, size int, padStr string) string //pads string on the right side if it's shorter than size -func PadStart(source string, size int, padStr string) string//pads string on the left side if it's shorter than size -func ReverseStr(s string) string //return string whose char order is reversed to the given string -func SnakeCase(s string) string //covert string to snake_case "fooBar" -> "foo_bar" -func Wrap(str string, wrapWith string) string //wrap a string with another string. -func Unwrap(str string, wrapToken string) string //unwrap a given string from anther string. will change str value -``` -### 12. system contain some functions about os, runtime, shell command. - -- Generate random string and int. -- Usage: import "github.com/duke-git/lancet/system". - -```go -package main - -import ( - "fmt" - "io/ioutil" - "log" - "github.com/duke-git/lancet/system" -) - -func main() { - envFoo := system.GetOsEnv("foo") - fmt.Println(envFoo) -} -``` - -- Function list: - -```go -func IsWindows() bool //check if current os is windows -func IsLinux() bool //check if current os is linux -func IsMac() bool //check if current os is macos -func GetOsEnv(key string) string //gets the value of the environment variable named by the key. -func SetOsEnv(key, value string) error //sets the value of the environment variable named by the key. -func RemoveOsEnv(key string) error //remove a single environment variable. -func CompareOsEnv(key, comparedEnv string) bool //gets env named by the key and compare it with comparedEnv -func ExecCommand(command string) (stdout, stderr string, err error) //use shell /bin/bash -c to execute command -``` - -### 13. validator is for data validation - -- Contain function for data validation. -- Usage: import "github.com/duke-git/lancet/validator". - -```go -package main - -import ( - "fmt" - "io/ioutil" - "log" - "github.com/duke-git/lancet/validator" -) - -func main() { - str := "Foo-Bar" - isAlpha := validator.IsAlpha(str) - fmt.Println(isAlpha) //false -} -``` - -- Function list: - -```go -func ContainChinese(s string) bool //check if the string contain mandarin chinese -func IsAlpha(s string) bool //checks if the string contains only letters (a-zA-Z) -func IsBase64(base64 string) bool //check if the string is base64 string -func IsAllUpper(str string) bool //check if the string is all upper case letters A-Z -func IsAllLower(str string) bool //check if the string is all lower case letters a-z -func ContainUpper(str string) bool //check if the string contain at least one upper case letter A-Z -func ContainLower(str string) bool //check if the string contain at least one lower case letter a-z -func ContainLetter(str string) bool //check if the string contain at least one letter -func IsJSON(str string) bool //checks if the string is valid JSON -func IsChineseMobile(mobileNum string) bool //check if the string is chinese mobile number -func IsChineseIdNum(id string) bool //check if the string is chinese id number -func IsChinesePhone(phone string) bool //check if the string is chinese phone number -func IsCreditCard(creditCart string) bool //check if the string is credit card -func IsDns(dns string) bool //check if the string is dns -func IsEmail(email string) bool //check if the string is a email address -func IsEmptyString(s string) bool //check if the string is empty -func IsFloatStr(s string) bool //check if the string can convert to a float -func IsNumberStr(s string) bool //check if the string can convert to a number -func IsRegexMatch(s, regex string) bool //check if the string match the regexp -func IsIntStr(s string) bool //check if the string can convert to a integer -func IsIp(ipstr string) bool //check if the string is a ip address -func IsIpV4(ipstr string) bool //check if the string is a ipv4 address -func IsIpV6(ipstr string) bool //check if the string is a ipv6 address -func IsStrongPassword(password string, length int) bool //check if the string is strong password (alpha(lower+upper) + number + special chars(!@#$%^&*()?><)) -func IsUrl(str string) bool //check if the string is url -func IsWeakPassword(password string) bool //check if the string is weak password(only letter or only number or letter + number) -``` +- [ContainChinese](https://github.com/duke-git/lancet/blob/main/docs/validator.md#ContainChinese) +- [ContainLetter](https://github.com/duke-git/lancet/blob/main/docs/validator.md#ContainLetter) +- [ContainLower](https://github.com/duke-git/lancet/blob/main/docs/validator.md#ContainLower) +- [ContainUpper](https://github.com/duke-git/lancet/blob/main/docs/validator.md#ContainUpper) +- [IsAlpha](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsAlpha) +- [IsAllUpper](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsAllUpper) +- [IsAllLower](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsAllLower) +- [IsBase64](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsBase64) +- [IsChineseMobile](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsChineseMobile) +- [IsChineseIdNum](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsChineseIdNum) +- [IsChinesePhone](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsChinesePhone) +- [IsCreditCard](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsCreditCard) +- [IsDns](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsDns) +- [IsEmail](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsEmail) +- [IsEmptyString](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsEmptyString) +- [IsFloatStr](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsFloatStr) +- [IsNumberStr](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsNumberStr) +- [IsJSON](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsJSON) +- [IsRegexMatch](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsRegexMatch) +- [IsIntStr](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsIntStr) +- [IsIp](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsIp) +- [IsIpV4](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsIpV4) +- [IsIpV6](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsIpV6) +- [IsStrongPassword](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsStrongPassword) +- [IsUrl](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsUrl) +- [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator.md#IsWeakPassword) \ No newline at end of file diff --git a/README_zh-CN.md b/README_zh-CN.md index 4a51389..2628885 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -4,7 +4,7 @@

![Go version](https://img.shields.io/badge/go-%3E%3D1.16-9cf) -[![Release](https://img.shields.io/badge/release-1.2.2-green.svg)](https://github.com/duke-git/lancet/releases) +[![Release](https://img.shields.io/badge/release-1.2.3-green.svg)](https://github.com/duke-git/lancet/releases) [![GoDoc](https://godoc.org/github.com//duke-git/lancet?status.svg)](https://pkg.go.dev/github.com/duke-git/lancet) [![Go Report Card](https://goreportcard.com/badge/github.com/duke-git/lancet)](https://goreportcard.com/report/github.com/duke-git/lancet) [![test](https://github.com/duke-git/lancet/actions/workflows/codecov.yml/badge.svg?branch=main&event=push)](https://github.com/duke-git/lancet/actions/workflows/codecov.yml) @@ -18,7 +18,7 @@ ## 特性 - 👏 全面、高效、可复用 -- 💪 160+常用go工具函数,支持string、slice、datetime、net、crypt... +- 💪 170+常用go工具函数,支持string、slice、datetime、net、crypt... - 💅 只依赖go标准库 - 🌍 所有导出函数单元测试覆盖率100% @@ -56,544 +56,287 @@ func main() { ``` ## API文档 -### 1. convertor数据转换包 - -- 转换函数支持常用数据类型之间的转换 -- 导入包:import "github.com/duke-git/lancet/convertor" +### convertor转换器包支持一些常见的数据类型转换。 ```go -package main +import "github.com/duke-git/lancet/convertor" +``` +#### 函数列表: +- [ColorHexToRGB](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ColorHexToRGB) +- [ColorRGBToHex](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ColorRGBToHex) +- [ToBool](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToBool) +- [ToBytes](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToBytes) +- [ToChar](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToChar) +- [ToInt](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToInt) +- [ToJson](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToJson) +- [ToString](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#ToString) +- [StructToMap](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#StructToMap) + +### cryptor加密包支持数据加密和解密,获取md5,hash值。支持base64, md5, hmac, aes, des, rsa。 +```go +import "github.com/duke-git/lancet/cryptor" +``` + +#### 函数列表: +- [AesEcbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesEcbEncrypt) +- [AesEcbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesEcbDecrypt) +- [AesCbcEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesCbcEncrypt) +- [AesCbcDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesCbcDecrypt) +- [AesCtrCrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesCtrCrypt) +- [AesCfbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesCfbEncrypt) +- [AesCfbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesCfbDecrypt) +- [AesOfbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesOfbEncrypt) +- [AesOfbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#AesOfbDecrypt) +- [Base64StdEncode](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Base64StdEncode) +- [Base64StdDecode](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Base64StdDecode) +- [DesEcbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesEcbEncrypt) +- [DesEcbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesEcbDecrypt) +- [DesCbcEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesCbcEncrypt) +- [DesCbcDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesCbcDecrypt) +- [DesCtrCrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesCtrCrypt) +- [DesCfbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesCfbEncrypt) +- [DesCfbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesCfbDecrypt) +- [DesOfbEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesOfbEncrypt) +- [DesOfbDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#DesOfbDecrypt) +- [HmacMd5](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacMd5) +- [HmacSha1](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha1) +- [HmacSha256](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha256) +- [HmacSha512](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#HmacSha512) +- [Md5String](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5String) +- [Md5File](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Md5File) +- [Sha1](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha1) +- [Sha256](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha256) +- [Sha512](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#Sha512) +- [GenerateRsaKey](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#GenerateRsaKey) +- [RsaEncrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#RsaEncrypt) +- [RsaDecrypt](https://github.com/duke-git/lancet/blob/main/docs/cryptor_zh-CN.md#RsaDecrypt) + +### datetime日期时间处理包,格式化日期,比较日期。 + + +```go +import "github.com/duke-git/lancet/datetime" +``` +#### 函数列表: +- [AddDay](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddDay) +- [AddHour](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddHour) +- [AddMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddMinute) +- [GetNowDate](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDate) +- [GetNowTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowTime) +- [GetNowDateTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDateTime) +- [GetZeroHourTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetZeroHourTimestamp) +- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNightTimestamp) +- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatTimeToStr) +- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatStrToTime) + +### fileutil包支持文件基本操作。 + +```go +import "github.com/duke-git/lancet/fileutil" +``` + +#### 函数列表: + +- [ClearFile](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#ClearFile) +- [CreateFile](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#CreateFile) +- [CopyFile](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#CopyFile) +- [FileMode](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#FileMode) +- [MiMeType](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#MiMeType) +- [IsExist](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#IsExist) +- [IsLink](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#IsLink) +- [IsDir](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#IsDir) +- [ListFileNames](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#ListFileNames) +- [RemoveFile](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#RemoveFile) +- [ReadFileToString](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#ReadFileToString) +- [ReadFileByLine](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#ReadFileByLine) +- [Zip](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#Zip) +- [UnZip](https://github.com/duke-git/lancet/blob/main/docs/fileutil_zh-CN.md#UnZip) + +### formatter格式化器包含一些数据格式化处理方法。 + +```go import ( - "fmt" - "github.com/duke-git/lancet/convertor" -) - -func main() { - s := "12.3" - f, err := convertor.ToFloat(s) - if err != nil { - fmt.Errorf("error is %s", err.Error()) - } - fmt.Println(f) // 12.3 -} -``` - -- 函数列表: - -```go -func ColorHexToRGB(colorHex string) (red, green, blue int) //颜色值16进制转rgb -func ColorRGBToHex(red, green, blue int) string //颜色值rgb转16进制 -func ToBool(s string) (bool, error) //字符串转成Bool -func ToBytes(data interface{}) ([]byte, error) //interface转成byte slice -func ToChar(s string) []string //字符串转成字符slice -func ToFloat(value interface{}) (float64, error) //interface转成float64 -func ToInt(value interface{}) (int64, error) //interface转成int64 -func ToJson(value interface{}) (string, error) //interface转成json string -func ToString(value interface{}) string //interface转成string -func StructToMap(value interface{}) (map[string]interface{}, error) //struct串转成map, 需要设置struct tag `json` -``` - -### 2. cryptor加解密包 - -- 加密函数支持md5, hmac, aes, des, rsa -- 导入包:import "github.com/duke-git/lancet/cryptor" - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/cryptor" -) - -func main() { - data := "hello" - key := "abcdefghijklmnop" - - encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key)) - decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key)) - fmt.Println(string(decrypted)) // hello -} -``` - -- 函数列表: - -```go -func AesEcbEncrypt(data, key []byte) []byte //AES ECB模式加密 -func AesEcbDecrypt(encrypted, key []byte) []byte //AES ECB模式解密 -func AesCbcEncrypt(data, key []byte) []byte //AES CBC模式加密 -func AesCbcDecrypt(encrypted, key []byte) []byte //AES CBC模式解密 -func AesCtrCrypt(data, key []byte) []byte //AES CTR模式加密/解密 -func AesCfbEncrypt(data, key []byte) []byte //AES CFB模式加密 -func AesCfbDecrypt(encrypted, key []byte) []byte //AES CFB模式解密 -func AesOfbEncrypt(data, key []byte) []byte //AES OFB模式加密 -func AesOfbDecrypt(data, key []byte) []byte //AES OFB模式解密 -func Base64StdEncode(s string) string //base64编码 -func Base64StdDecode(s string) string //base64解码 -func DesEcbEncrypt(data, key []byte) []byte //DES ECB模式加密 -func DesEcbDecrypt(encrypted, key []byte) []byte //DES ECB模式解密 -func DesCbcEncrypt(data, key []byte) []byte //DES CBC模式加密 -func DesCbcDecrypt(encrypted, key []byte) []byte //DES CBC模式解密 -func DesCtrCrypt(data, key []byte) []byte //DES CTR模式加密/解密 -func DesCfbEncrypt(data, key []byte) []byte //DES CFB模式加密 -func DesCfbDecrypt(encrypted, key []byte) []byte //DES CFB模式解密 -func DesOfbEncrypt(data, key []byte) []byte //DES OFB模式加密 -func DesOfbDecrypt(data, key []byte) []byte //DES OFB模式解密 -func HmacMd5(data, key string) string //获取hmac md5值 -func HmacSha1(data, key string) string //获取hmac sha1值 -func HmacSha256(data, key string) string //获取hmac sha256值 -func HmacSha512(data, key string) string //获取hmac sha512值 -func Md5String(s string) string //获取字符串md5值 -func Md5File(filename string) (string, error) //获取文件md5值 -func Sha1(data string) string //获取sha1值 -func Sha256(data string) string //获取sha256值 -func Sha512(data string) string //获取sha512值 -func GenerateRsaKey(keySize int, priKeyFile, pubKeyFile string) error //生成RSA私钥文件 -func RsaEncrypt(data []byte, pubKeyFileName string) []byte //RSA加密 -func RsaDecrypt(data []byte, privateKeyFileName string) []byte //RSA解密 - -``` - -### 3. datetime日期时间处理包 - -- 处理日期时间 -- 导入包:import "github.com/duke-git/lancet/datetime" - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/datetime" -) - -func main() { - now := time.Now() - s := datetime.FormatTimeToStr(now, "yyyy-mm-dd hh:mm:ss") - fmt.Println(s) // 2021-11-24 11:16:55 -} -``` - -- 函数列表: - -```go -func AddDay(t time.Time, day int64) time.Time //加减天数 -func AddHour(t time.Time, hour int64) time.Time //加减小时数 -func AddMinute(t time.Time, minute int64) time.Time //加减分钟数 -func GetNowDate() string //获取当天日期 格式yyyy-mm-dd -func GetNowTime() string //获取当前时间 格式hh:mm:ss -func GetNowDateTime() string //获取当前日期时间 格式yyyy-mm-dd hh:mm:ss -func GetZeroHourTimestamp() int64 //获取当天零时时间戳(00:00) -func GetNightTimestamp() int64 //获取当天23时时间戳(23:59) -func FormatTimeToStr(t time.Time, format string) string //时间格式化字符串 -func FormatStrToTime(str, format string) time.Time //字符串转换成时间 -``` - -### 4. fileutil文件处理包 - -- 文件处理常用函数 -- 导入包:import "github.com/duke-git/lancet/fileutil" - -```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/fileutil" -) - -func main() { - fmt.Println(fileutil.IsDir("./")) // true -} -``` - -- 函数列表: - -```go -func ClearFile(path string) error //清空文件内容 -func CreateFile(path string) bool //创建文件 -func FileMode(path string) (fs.FileMode, error) //返回文件mode信息 -func MiMeType(file interface{}) string //返回文件mime类型 -func IsExist(path string) bool //判断文件/目录是否存在 -func IsDir(path string) bool //判断是否为目录 -func IsLink(path string) bool //检查文件是否为符号链接文件 -func RemoveFile(path string) error //删除文件 -func CopyFile(srcFilePath string, dstFilePath string) error //复制文件 -func ListFileNames(path string) ([]string, error) //列出目录下所有文件名称 -func ReadFileToString(path string) (string, error) //读取文件内容为字符串 -func ReadFileByLine(path string)([]string, error) //按行读取文件内容 -func Zip(fpath string, destPath string) error //压缩文件fpath参数可以是文件或目录,destPath是压缩后目标文件 -func UnZip(zipFile string, destPath string) error //解压文件,并将文件存储在destPath目录中 -``` - -### 5. formatter格式化处理包 - -- 格式化相关处理函数 -- 导入包:import "github.com/duke-git/lancet/formatter" - -```go -package main - -import ( - "fmt" "github.com/duke-git/lancet/formatter" ) - -func main() { - fmt.Println(formatter.Comma("12345", "")) // "12,345" - fmt.Println(formatter.Comma(12345.67, "¥")) // "¥12,345.67" -} ``` +#### 函数列表: +- [Comma](https://github.com/duke-git/lancet/blob/main/docs/formatter_zh-CN.md#Comma) -- 函数列表: + +### function函数包控制函数执行流程,包含部分函数式编程。 ```go -func Comma(v interface{}, symbol string) string //用逗号每隔3位分割数字/字符串 +import "github.com/duke-git/lancet/function" ``` -### 6. function包可以控制函数执行,支持部分函数式编程 +#### 函数列表: +- [After](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#After) +- [Before](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Before) +- [Curry](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Curry) +- [Compose](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Compose) +- [Debounced](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Debounced) +- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay) +- [Delay](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay) +- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher) -- 控制函数执行,支持部分函数式编程 -- 导入包:import "github.com/duke-git/lancet/function" + +### netutil网络包支持获取ip地址,发送http请求。 ```go -package main - -import ( - "fmt" - "github.com/duke-git/lancet/function" -) - -func main() { - var print = func(s string) { - fmt.Println(s) - } - function.Delay(2*time.Second, print, "hello world") -} +import "github.com/duke-git/lancet/netutil" ``` -- Function list: +#### 函数列表: +- [ConvertMapToQueryString](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ConvertMapToQueryString) +- [GetInternalIp](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetInternalIp) +- [GetPublicIpInfo](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#GetPublicIpInfo) +- [IsPublicIP](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#IsPublicIP) +- [HttpGet](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpGet) +- [HttpDelete](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpDelete) +- [HttpPost](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPost) +- [HttpPut](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPut) +- [HttpPatch](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPatch) +- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ParseHttpResponse) + +### random随机数生成器包,可以生成随机[]bytes, int, string。 ```go -func After(n int, fn interface{}) func(args ...interface{}) []reflect.Value //创建一个函数, 只有在运行了n次之后才有效果 -func Before(n int, fn interface{}) func(args ...interface{}) []reflect.Value //创建一个函数,调用不超过n次。 当n已经达到时,最后一个函数调用的结果将被记住并返回 -func (f Fn) Curry(i interface{}) func(...interface{}) interface{} //函数柯里化 -func Compose(fnList ...func(...interface{}) interface{}) func(...interface{}) interface{} //从右至左组合函数 -func Delay(delay time.Duration, fn interface{}, args ...interface{}) //延迟调用函数 -func Debounced(fn func(), duration time.Duration) func() //go防抖函数,在duration时间内连续调用只会执行一次. -func Schedule(d time.Duration, fn interface{}, args ...interface{}) chan bool //每隔duration时间调用函数, 关闭返回通道可以停止调用 -func (w *Watcher) Start() //开时watcher -func (w *Watcher) Stop() //开时watcher -func (w *Watcher) Reset() {} //重置代码watcher -func (w *Watcher) GetElapsedTime() time.Duration //get code excution elapsed time. +import "github.com/duke-git/lancet/random" ``` -### 7. netutil网络处理包 +#### 函数列表: +- [RandBytes](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandBytes) +- [RandInt](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandInt) +- [RandString](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandString) -- 处理ip, http请求相关函数 -- 导入包:import "github.com/duke-git/lancet/netutil" -- http方法params参数顺序:header, query string, body, httpclient +### retry重试执行函数直到函数运行成功或被context cancel。 ```go -package main - -import ( - "fmt" - "io/ioutil" - "log" - "github.com/duke-git/lancet/netutil" -) - -func main() { - url := "https://gutendex.com/books?" - header := make(map[string]string) - header["Content-Type"] = "application/json" - queryParams := make(map[string]interface{}) - queryParams["ids"] = "1" - - resp, err := netutil.HttpGet(url, header, queryParams) - if err != nil { - log.Fatal(err) - } - - body, _ := ioutil.ReadAll(resp.Body) - fmt.Println("response: ", resp.StatusCode, string(body)) -} +import "github.com/duke-git/lancet/retry" ``` -- 函数列表: +#### 函数列表: +- [Context](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#Context) +- [Retry](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#Retry) +- [RetryFunc](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryFunc) +- [RetryDuration](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryDuration) +- [RetryTimes](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryTimes) + + +### slice包包含操作切片的方法集合。 ```go -func GetInternalIp() string //获取内部ip -func GetPublicIpInfo() (*PublicIpInfo, error) //获取公共ip信息: country, region, isp, city, lat, lon, ip -func IsPublicIP(IP net.IP) bool //判断ip是否为公共ip -func HttpGet(url string, params ...interface{}) (*http.Response, error) //http get请求 -func HttpPost(url string, params ...interface{}) (*http.Response, error) //http post请求 -func HttpPut(url string, params ...interface{}) (*http.Response, error) //http put请求 -func HttpDelete(url string, params ...interface{}) (*http.Response, error) //http delete请求 -func HttpPatch(url string, params ...interface{}) (*http.Response, error) //http patch请求 -func ConvertMapToQueryString(param map[string]interface{}) string //将map转换成url query string -func ParseHttpResponse(resp *http.Response, obj interface{}) error //将http响应解码成特定interface +import "github.com/duke-git/lancet/slice" ``` -### 8. random随机数处理包 +#### 函数列表: +- [Contain](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Contain) +- [ContainSubSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ContainSubSlice) +- [Chunk](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Chunk) +- [Compact](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Compact) +- [Concat](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Concat) +- [Count](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Count) +- [Difference](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Difference) +- [DifferenceBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#DifferenceBy) +- [DeleteByIndex](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#DeleteByIndex) +- [Drop](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Drop) +- [Every](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Every) +- [Filter](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Filter) +- [Find](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Find) +- [FindLast](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#FindLast) +- [FlattenDeep](#FlattenDeep) +- [ForEach](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ForEach) +- [GroupBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#GroupBy) +- [IntSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#IntSlice) +- [InterfaceSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#InterfaceSlice) +- [Intersection](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Intersection) +- [InsertByIndex](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#InsertByIndex) +- [Map](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Map) +- [ReverseSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#ReverseSlice) +- [Reduce](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Reduce) +- [Shuffle](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Shuffle) +- [SortByField](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#SortByField) +- [Some](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Some) +- [StringSlice](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#StringSlice) +- [Unique](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Unique) +- [Union](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Union) +- [UpdateByIndex](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#UpdateByIndex) +- [Without](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Without) -- 生成和处理随机数 -- 导入包:import "github.com/duke-git/lancet/random" + +### strutil包含处理字符串的相关函数。 ```go -package main - -import ( - "fmt" - "io/ioutil" - "log" - "github.com/duke-git/lancet/random" -) - -func main() { - randStr := random.RandString(6) - fmt.Println(randStr) -} +import "github.com/duke-git/lancet/strutil" ``` -- 函数列表: +#### 函数列表: + +- [After](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#After) +- [AfterLast](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#AfterLast) +- [Before](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Before) +- [BeforeLast](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#BeforeLast) +- [CamelCase](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#CamelCase) +- [Capitalize](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Capitalize) +- [IsString](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#IsString) +- [KebabCase](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#KebabCase) +- [LowerFirst](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#LowerFirst) +- [UpperFirst](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperFirst) +- [PadEnd](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadEnd) +- [PadStart](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadStart) +- [ReverseStr](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#ReverseStr) +- [SnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SnakeCase) +- [Wrap](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Wrap) +- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Unwrap) + + +### system包含os, runtime, shell command相关函数。 ```go -func RandBytes(length int) []byte //生成随机[]byte -func RandInt(min, max int) int //生成随机int -func RandString(length int) string //生成随机string +import "github.com/duke-git/lancet/system" ``` -### 9. retry重试执行函数 +#### 函数列表: +- [IsWindows](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#IsWindows) +- [IsLinux](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#IsLinux) +- [IsMac](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#IsMac) +- [GetOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#GetOsEnv) +- [SetOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#SetOsEnv) +- [RemoveOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#RemoveOsEnv) +- [CompareOsEnv](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#CompareOsEnv) +- [ExecCommand](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN.md#ExecCommand) -- 重试执行函数直到函数成功或被context停止 -- 默认重试次数5, 默认执行间隔3秒. -- Usage: import "github.com/duke-git/lancet/retry". +### validator验证器包,包含常用字符串格式验证函数。 ```go -package main - -import ( - "fmt" - "io/ioutil" - "log" - "github.com/duke-git/lancet/retry" -) - -func main() { - var number int - increaseNumber := func() error { - number++ - if number == 3 { - return nil - } - return errors.New("error occurs") - } - - err := retry.Retry(increaseNumber, retry.RetryDuration(time.Microsecond*50)) - - fmt.Println(number) //3 -} +import "github.com/duke-git/lancet/validator" ``` +#### 函数列表: -- Function list: - -```go -type RetryFunc func() error //要重试执行的函数 -func RetryTimes(n uint) //设置重试次数,默认5次 -func RetryDuration(d time.Duration) //设置重试间隔时间,默认3秒 -func Context(ctx context.Context) //context config -func Retry(retryFunc RetryFunc, opts ...Option) error //重试函数 -``` - -### 10. slice切片操作包 - -- 切片操作相关函数 -- 导入包:import "github.com/duke-git/lancet/slice" -- 由于go目前对范型支持不稳定,slice处理函数参数和返回值大部分为interface{}, 待范型特性稳定后,会重构相关函数 - -```go -package main - -import ( - "fmt" - "io/ioutil" - "log" - "github.com/duke-git/lancet/slice" -) - -func main() { - nums := []int{1, 4, 3, 4, 6, 7, 3} - uniqueNums, _ := slice.IntSlice(slice.Unique(nums)) - fmt.Println(uniqueNums) //[1 4 3 6 7] -} -``` - -- 函数列表: - -```go -func Contain(slice interface{}, value interface{}) bool //判断slice是否包含value -func ContainSubSlice(slice interface{}, subslice interface{}) bool //判断slice是否包含subslice -func Chunk(slice []interface{}, size int) [][]interface{} //均分slice -func Compact(slice interface{}) interface{} //去除slice中的false vule. false values are false, nil, 0, and "" -func Concat(slice interface{}, values ...interface{}) interface{} //连接values到slice中 -func Difference(slice1, slice2 interface{}) interface{} //返回切片,其元素在slice1中,不在slice2中 -func DifferenceBy(slice interface{}, comparedSlice interface{}, iterateeFn interface{}) interface{} //将slice 和comparedSlice中每个元素调用iterateeFn后作比较,如果不相等返回slice中的元素。 -func DeleteByIndex(slice interface{}, start int, end ...int) (interface{}, error) //删除切片中start到end位置的值 -func Drop(slice interface{}, n int) interface{} //创建一个新切片,当n大于0时删除原切片前n个元素,当n小于0时删除原切片后n个元素 -func Every(slice, function interface{}) bool //slice中所有元素都符合函数条件时返回true, 否则返回false. 函数签名:func(index int, value interface{}) bool -func None(slice, function interface{}) bool //slice中所有元素都不符合函数条件时返回true, 否则返回false. 函数签名:func(index int, value interface{}) bool -func Find(slice, function interface{}) (interface{}, bool)//查找slice中第一个符合条件的元素,函数签名:func(index int, value interface{}) bool -func Filter(slice, function interface{}) interface{} //过滤slice, 函数签名:func(index int, value interface{}) bool -func FlattenDeep(slice interface{}) interface{} //将slice递归为一维切片。 -func ForEach(slice, function interface{}) //遍历切片,在每个元素上执行函数,函数签名:func(index int, value interface{}) -func IntSlice(slice interface{}) ([]int, error) //转成int切片 -func InterfaceSlice(slice interface{}) []interface{} //转成interface{}切片 -func Intersection(slices ...interface{}) interface{} //slice交集,去重 -func InsertByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //在切片中index位置插入value -func Map(slice, function interface{}) interface{} //遍历切片, 函数签名:func(index int, value interface{}) interface{} -func ReverseSlice(slice interface{}) //反转切片 -func Reduce(slice, function, zero interface{}) interface{} //切片reduce操作, 函数签名:func(index int, value1, value2 interface{}) interface{} -func Shuffle(slice interface{}) interface{} //创建一个被打乱值的切片 -func Some(slice, function interface{}) bool //slice中任意一个元素都符合函数条件时返回true, 否则返回false. 函数签名:func(index int, value interface{}) bool -func SortByField(slice interface{}, field string, sortType ...string) error //对struct切片进行排序 -func StringSlice(slice interface{}) []string //转为string切片 -func Unique(slice interface{}) interface{} //去重切片 -func Union(slices ...interface{}) interface{} //slice并集, 去重 -func UpdateByIndex(slice interface{}, index int, value interface{}) (interface{}, error) //在切片中index位置更新value -func Without(slice interface{}, values ...interface{}) interface{} //slice去除values -func GroupBy(slice, function interface{}) (interface{}, interface{}) //根据函数function的逻辑分slice为两组slice -func Count(slice, function interface{}) int -``` - -### 11. strutil字符串处理包 - -- 字符串操作相关函数 -- 导入包:import "github.com/duke-git/lancet/strutil" - -```go -package main - -import ( - "fmt" - "io/ioutil" - "log" - "github.com/duke-git/lancet/strutil" -) - -func main() { - str := "Foo-Bar" - camelCaseStr := strutil.CamelCase(str) - fmt.Println(camelCaseStr) //fooBar -} -``` - -- 函数列表: - -```go -func After(s, char string) string //截取字符串中char第一次出现之后的字符串 -func AfterLast(s, char string) string //截取字符串中char最后一次出现之后的字符串 -func Before(s, char string) string //截取字符串中char第一次出现之前的字符串 -func BeforeLast(s, char string) string //截取字符串中char最后一次出现之前的字符串 -func CamelCase(s string) string //字符串转为cameCase, "foo bar" -> "fooBar" -func Capitalize(s string) string //字符串转为Capitalize, "fOO" -> "Foo" -func IsString(v interface{}) bool //判断是否是字符串 -func KebabCase(s string) string //字符串转为KebabCase, "foo_Bar" -> "foo-bar" -func UpperFirst(s string) string //字符串的第一个字母转为大写字母 -func LowerFirst(s string) string //字符串的第一个字母转为小写字母 -func PadEnd(source string, size int, padStr string) string //字符串末尾填充size个字符 -func PadStart(source string, size int, padStr string) string//字符串开头填充size个字符 -func ReverseStr(s string) string //字符串逆序 -func Wrap(str string, wrapWith string) string //包裹字符串 Wrap("abc", "*") -> *abc*. -func Unwrap(str string, wrapToken string) string //解包裹字符串 Wrap("*abc*", "*") -> abc. -func SnakeCase(s string) string //字符串转为SnakeCase, "fooBar" -> "foo_bar" -``` - -### 12. system系统包 - -- 包含一些操作系统,运行时,shell命令执行的函数. -- Usage: import "github.com/duke-git/lancet/system". - -```go -package main - -import ( - "fmt" - "io/ioutil" - "log" - "github.com/duke-git/lancet/system" -) - -func main() { - envFoo := system.GetOsEnv("foo") - fmt.Println(envFoo) -} -``` - -- Function list: - -```go -func IsWindows() bool //判断操作系统是windows -func IsLinux() bool //判断操作系统是linux -func IsMac() bool //判断操作系统是macos -func GetOsEnv(key string) string //获取名称为key的环境变量 -func SetOsEnv(key, value string) error //设置环境变量 -func RemoveOsEnv(key string) error //删除指定key的环境变量 -func CompareOsEnv(key, comparedEnv string) bool //获取名称为key的环境变量并和comparedEnv比较 -func ExecCommand(command string) (stdout, stderr string, err error) //执行shell命令(/bin/bash) -``` - -### 13. validator验证器包 - -- 数据校验相关函数 -- 导入包:import "github.com/duke-git/lancet/validator" - -```go -package main - -import ( - "fmt" - "io/ioutil" - "log" - "github.com/duke-git/lancet/validator" -) - -func main() { - str := "Foo-Bar" - isAlpha := validator.IsAlpha(str) - fmt.Println(isAlpha) //false -} -``` - -- 函数列表: - -```go -func ContainChinese(s string) bool //判断字符串中是否含有中文字符 -func IsAlpha(s string) bool //判断字符串是否只含有字母 -func IsBase64(base64 string) bool //判断字符串是base64 -func IsAllUpper(str string) bool //断字符串是否全是大写字母 -func IsAllLower(str string) bool //断字符串是否全是小写字母 -func ContainUpper(str string) bool //判断字符串是否包含大写字母 -func ContainLower(str string) bool //判断字符串是否包含小写字母 -func ContainLetter(str string) bool //判断字符串是否包含字母 -func IsJSON(str string) bool //判断字符串是否是JSON -func IsChineseMobile(mobileNum string) bool //判断字符串是否是手机号 -func IsChineseIdNum(id string) bool //判断字符串是否是身份证号 -func IsChinesePhone(phone string) bool //判断字符串是否是座机电话号码 -func IsCreditCard(creditCart string) bool //判断字符串是否是信用卡 -func IsDns(dns string) bool //判断字符串是否是DNS -func IsEmail(email string) bool //判断字符串是否是邮箱 -func IsEmptyString(s string) bool //判断字符串是否为空 -func IsFloatStr(s string) bool //判断字符串是否可以转成float -func IsNumberStr(s string) bool //判断字符串是否可以转成数字 -func IsRegexMatch(s, regex string) bool //判断字符串是否match正则表达式 -func IsIntStr(s string) bool //判断字符串是否可以转成整数 -func IsIp(ipstr string) bool //判断字符串是否是ip -func IsIpV4(ipstr string) bool //判断字符串是否是ipv4 -func IsIpV6(ipstr string) bool //判断字符串是否是ipv6 -func IsStrongPassword(password string, length int) bool //判断字符串是否是强密码(大小写字母+数字+特殊字符) -func IsUrl(str string) bool //判断字符串是否是url -func IsWeakPassword(password string) bool //判断字符串是否是弱密码(只有字母或数字) -``` +- [ContainChinese](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#ContainChinese) +- [ContainLetter](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#ContainLetter) +- [ContainLower](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#ContainLower) +- [ContainUpper](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#ContainUpper) +- [IsAlpha](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsAlpha) +- [IsAllUpper](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsAllUpper) +- [IsAllLower](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsAllLower) +- [IsBase64](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsBase64) +- [IsChineseMobile](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsChineseMobile) +- [IsChineseIdNum](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsChineseIdNum) +- [IsChinesePhone](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsChinesePhone) +- [IsCreditCard](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsCreditCard) +- [IsDns](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsDns) +- [IsEmail](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsEmail) +- [IsEmptyString](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsEmptyString) +- [IsFloatStr](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsFloatStr) +- [IsNumberStr](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsNumberStr) +- [IsJSON](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsJSON) +- [IsRegexMatch](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsRegexMatch) +- [IsIntStr](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsIntStr) +- [IsIp](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsIp) +- [IsIpV4](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsIpV4) +- [IsIpV6](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsIpV6) +- [IsStrongPassword](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsStrongPassword) +- [IsUrl](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsUrl) +- [IsWeakPassword](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsWeakPassword) \ No newline at end of file diff --git a/docs/cryptor_zh-CN.md b/docs/cryptor_zh-CN.md index bd52a26..6847bcb 100644 --- a/docs/cryptor_zh-CN.md +++ b/docs/cryptor_zh-CN.md @@ -22,45 +22,32 @@ import (
## 目录 +- [AesEcbEncrypt](#AesEcbEncrypt) +- [AesEcbDecrypt](#AesEcbDecrypt) +- [AesCbcEncrypt](#AesCbcEncrypt) +- [AesCbcDecrypt](#AesCbcDecrypt) +- [AesCtrCrypt](#AesCtrCrypt) +- [AesCfbEncrypt](#AesCfbEncrypt) +- [AesCfbDecrypt](#AesCfbDecrypt) +- [AesOfbEncrypt](#AesOfbEncrypt) +- [AesOfbDecrypt](#AesOfbDecrypt) +- [Base64StdEncode](#Base64StdEncode) +- [Base64StdDecode](#Base64StdDecode) -- [Cryptor](#cryptor) - - [源码:](#源码) - - [用法:](#用法) - - [目录](#目录) - - [文档](#文档) - - [AesEcbEncrypt](#aesecbencrypt) - - [AesEcbDecrypt](#aesecbdecrypt) - - [AesCbcEncrypt](#aescbcencrypt) - - [AesCbcDecrypt](#aescbcdecrypt) - - [AesCtrCrypt](#aesctrcrypt) - - [AesCfbEncrypt](#aescfbencrypt) - - [AesCfbDecrypt](#aescfbdecrypt) - - [AesOfbEncrypt](#aesofbencrypt) - - [AesOfbDecrypt](#aesofbdecrypt) - - [Base64StdEncode](#base64stdencode) - - [Base64StdDecode](#base64stddecode) - - [DesEcbEncrypt](#desecbencrypt) - - [DesEcbDecrypt](#desecbdecrypt) - - [DesCbcEncrypt](#descbcencrypt) - - [DesCbcDecrypt](#descbcdecrypt) - - [DesCtrCrypt](#desctrcrypt) - - [DesCfbEncrypt](#descfbencrypt) - - [DesCfbDecrypt](#descfbdecrypt) - - [DesOfbEncrypt](#desofbencrypt) - - [DesOfbDecrypt](#desofbdecrypt) - - [HmacMd5](#hmacmd5) - - [HmacSha1](#hmacsha1) - - [HmacSha256](#hmacsha256) - - [HmacSha512](#hmacsha512) - - [Md5String](#md5string) - - [Md5File](#md5file) - - [Sha1](#sha1) - - [Sha256](#sha256) - - [Sha512](#sha512) - - [GenerateRsaKey](#generatersakey) - - [RsaEncrypt](#rsaencrypt) - - [RsaDecrypt](#rsadecrypt) +- [DesEcbEncrypt](#DesEcbEncrypt) +- [DesEcbDecrypt](#DesEcbDecrypt) +- [DesCbcEncrypt](#DesCbcEncrypt) +- [DesCbcDecrypt](#DesCbcDecrypt) +- [DesCtrCrypt](#DesCtrCrypt) +- [DesCfbEncrypt](#DesCfbEncrypt) +- [DesCfbDecrypt](#DesCfbDecrypt) +- [DesOfbEncrypt](#DesOfbEncrypt) +- [DesOfbDecrypt](#DesOfbDecrypt) +- [HmacMd5](#HmacMd5) +- [HmacSha1](#HmacSha1) +- [HmacSha256](#HmacSha256) +- [HmacSha512](#HmacSha512) - [Md5String](#Md5String) - [Md5File](#Md5File) - [Sha1](#Sha1)