mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-07 22:22:29 +08:00
31 KiB
31 KiB
lancet(柳叶刀)是一个全面、高效、可复用的go语言工具函数库。 lancet受到了java apache common包和lodash.js的启发。
简体中文 | English
特性
- 👏 全面、高效、可复用
- 💪 400+常用 go 工具函数,支持 string、slice、datetime、net、crypt...
- 💅 只依赖 go 标准库
- 🌍 所有导出函数单元测试覆盖率 100%
安装
go get github.com/duke-git/lancet
用法
lancet 是以包的结构组织代码的,使用时需要导入相应的包名。例如:如果使用字符串相关函数,需要导入 strutil 包:
import "github.com/duke-git/lancet/strutil"
例子
此处以字符串工具函数 Reverse(逆序字符串)为例,需要导入 strutil 包:
package main
import (
"fmt"
"github.com/duke-git/lancet/strutil"
)
func main() {
s := "hello"
rs := strutil.Reverse(s)
fmt.Println(rs) //olleh
}
API 文档
1. compare包提供几个轻量级的类型比较函数。
import "github.com/duke-git/lancet/compare"
Function list:
2. convertor转换器包支持一些常见的数据类型转换。
import "github.com/duke-git/lancet/convertor"
函数列表:
- ColorHexToRGB
- ColorRGBToHex
- ToBool
- ToBytes
- ToChar
- ToChannel
- ToInt
- ToJson
- ToString
- StructToMap
- EncodeByte
- DecodeByte
- DeepClone
- CopyProperties
3. cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。
import "github.com/duke-git/lancet/cryptor"
函数列表:
- AesEcbEncrypt
- AesEcbDecrypt
- AesCbcEncrypt
- AesCbcDecrypt
- AesCtrCrypt
- AesCfbEncrypt
- AesCfbDecrypt
- AesOfbEncrypt
- AesOfbDecrypt
- Base64StdEncode
- Base64StdDecode
- DesEcbEncrypt
- DesEcbDecrypt
- DesCbcEncrypt
- DesCbcDecrypt
- DesCtrCrypt
- DesCfbEncrypt
- DesCfbDecrypt
- DesOfbEncrypt
- DesOfbDecrypt
- HmacMd5
- HmacSha1
- HmacSha256
- HmacSha512
- Md5String
- Md5File
- Sha1
- Sha256
- Sha512
- GenerateRsaKey
- RsaEncrypt
- RsaDecrypt
4. datetime 日期时间处理包,格式化日期,比较日期。
import "github.com/duke-git/lancet/datetime"
函数列表:
- AddDay
- AddHour
- AddMinute
- AddYear
- BeginOfMinute
- BeginOfHour
- BeginOfDay
- BeginOfWeek
- BeginOfMonth
- BeginOfYear
- EndOfMinute
- EndOfHour
- EndOfDay
- EndOfWeek
- EndOfMonth
- EndOfYear
- GetNowDate
- GetNowTime
- GetNowDateTime
- GetZeroHourTimestamp
- GetNightTimestamp
- FormatTimeToStr
- FormatStrToTime
- NewUnix
- NewUnixNow
- NewFormat
- NewISO8601
- ToUnix
- ToFormat
- ToFormatForTpl
- ToIso8601
- IsLeapYear
- BetweenSeconds
- DayOfYear
- IsWeekend
5. fileutil 包支持文件基本操作。
import "github.com/duke-git/lancet/fileutil"
函数列表:
- ClearFile
- CreateFile
- CreateDir
- CopyFile
- FileMode
- MiMeType
- IsExist
- IsLink
- IsDir
- ListFileNames
- RemoveFile
- ReadFileToString
- ReadFileByLine
- Zip
- UnZip
- CurrentPath
- IsZipFile
- FileSize
- MTime
- Sha
- ReadCsvFile
- WriteStringToFile
- WriteBytesToFile
6. formatter 格式化器包含一些数据格式化处理方法。
import "github.com/duke-git/lancet/formatter"
函数列表:
7. function 函数包控制函数执行流程,包含部分函数式编程。
import "github.com/duke-git/lancet/function"
函数列表:
8. mathutil 包实现了一些数学计算的函数。
import "github.com/duke-git/lancet/mathutil"
Function list:
- Exponent
- Fibonacci
- Factorial
- Percent
- RoundToFloat
- RoundToString
- TruncRound
- AngleToRadian
- RadianToAngle
- PointDistance
- IsPrime
- GCD
- LCM
- Cos
- Sin
9. netutil 网络包支持获取 ip 地址,发送 http 请求。
import "github.com/duke-git/lancet/netutil"
函数列表:
- ConvertMapToQueryString
- EncodeUrl
- GetInternalIp
- GetIps
- GetMacAddrs
- GetPublicIpInfo
- GetRequestPublicIp
- IsPublicIP
- IsInternalIP
- HttpGet
- HttpDelete
- HttpPost
- HttpPut
- HttpPatch
- ParseHttpResponse
- UploadFile
- DownloadFile
- IsPingConnected
- IsTelnetConnected
10. random 随机数生成器包,可以生成随机[]bytes, int, string。
import "github.com/duke-git/lancet/random"
函数列表:
11. retry 重试执行函数直到函数运行成功或被 context cancel。
import "github.com/duke-git/lancet/retry"
函数列表:
12. slice 包包含操作切片的方法集合。
import "github.com/duke-git/lancet/slice"
函数列表:
- AppendIfAbsent
- Contain
- ContainSubSlice
- Chunk
- Compact
- Concat
- Count
- Difference
- DifferenceBy
- DeleteByIndex
- Drop
- Every
- Equal
- EqualWith
- Filter
- Find
- FindLast
- FlattenDeep
- ForEach
- GroupBy
- IntSlice
- IndexOf
- LastIndexOf
- InterfaceSlice
- Intersection
- InsertByIndex
- Map
- ReverseSlice
- Reduce
- Shuffle
- SortByField
- Some
- StringSlice
- ToSlice
- ToSlicePointer
- Unique
- UniqueBy
- Union
- UpdateByIndex
- Without
13. strutil 包含处理字符串的相关函数。
import "github.com/duke-git/lancet/strutil"
函数列表:
- After
- AfterLast
- Before
- BeforeLast
- CamelCase
- Capitalize
- ContainsAll
- ContainsAny
- IsString
- KebabCase
- UpperKebabCase
- LowerFirst
- UpperFirst
- Pad
- PadEnd
- PadStart
- Reverse
- SnakeCase
- UpperSnakeCase
- SplitEx
- Wrap
- Unwrap
- SplitWords
- WordCount
- RemoveNonPrintable
- StringToBytes
- BytesToString
- IsBlank
- HasPrefixAny
- HasSuffixAny
- IndexOffset
- ReplaceWithMap
- Trim
- SplitAndTrim
- HideString
14. system 包含 os, runtime, shell command 相关函数。
import "github.com/duke-git/lancet/system"
函数列表:
15. validator 验证器包,包含常用字符串格式验证函数。
import "github.com/duke-git/lancet/validator"
函数列表:
- ContainChinese
- ContainLetter
- ContainLower
- ContainUpper
- IsAlpha
- IsAllUpper
- IsAllLower
- IsBase64
- IsChineseMobile
- IsChineseIdNum
- IsChinesePhone
- IsCreditCard
- IsDns
- IsEmail
- IsEmptyString
- IsInt
- IsFloat
- IsNumber
- IsIntStr
- IsFloatStr
- IsNumberStr
- IsJSON
- IsRegexMatch
- IsIp
- IsIpV4
- IsIpV6
- IsStrongPassword
- IsUrl
- IsWeakPassword
- IsZeroValue
- IsGBK
- IsASCII
- IsPrintable
如何贡献代码
非常感激任何的代码提交以使 lancet 的功能越来越强大。创建 pull request 时请遵守以下规则。
- Fork lancet 仓库。
- 创建自己的特性分支。
- 提交变更。
- Push 分支。
- 创建新的 pull request。
