mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
33 KiB
33 KiB
lancet(柳叶刀)是一个全面、高效、可复用的go语言工具函数库。 lancet受到了java apache common包和lodash.js的启发。
简体中文 | English
特性
- 👏 全面、高效、可复用
- 💪 300+常用 go 工具函数,支持 string、slice、datetime、net、crypt...
- 💅 只依赖 go 标准库
- 🌍 所有导出函数单元测试覆盖率 100%
安装
Note:
- 对于使用 go1.18 及以上的用户,建议安装 v2.x.x。 因为 v2.x.x 用 go1.18 的泛型重写了大部分函数。
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
- 使用 go1.18 以下版本的用户,必须安装 v1.x.x。目前最新的 v1 版本是 v1.3.3。
go get github.com/duke-git/lancet@v1.3.3 // 使用go1.18以下版本, 必须安装v1.x.x版本
用法
lancet 是以包的结构组织代码的,使用时需要导入相应的包名。例如:如果使用字符串相关函数,需要导入 strutil 包:
import "github.com/duke-git/lancet/v2/strutil"
例子
此处以字符串工具函数 Reverse(逆序字符串)为例,需要导入 strutil 包:
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
s := "hello"
rs := strutil.Reverse(s)
fmt.Println(rs) //olleh
}
API 文档
lancet API doc 感谢@UvDream整理
1. algorithm 算法包实现一些基本算法。eg. sort, search.
import "github.com/duke-git/lancet/v2/algorithm"
Function list:
- BubbleSort
- CountSort
- HeapSort
- InsertionSort
- MergeSort
- QuickSort
- SelectionSort
- ShellSort
- BinarySearch
- BinaryIterativeSearch
- LinearSearch
- LRUCache
2. concurrency 并发包包含一些支持并发编程的功能。例如:goroutine, channel, async 等。
import "github.com/duke-git/lancet/v2/concurrency"
Function list:
3. condition 条件包含一些用于条件判断的函数。eg. And, Or, TernaryOperator...
import "github.com/duke-git/lancet/v2/condition"
Function list:
4. convertor 转换器包支持一些常见的数据类型转换。
import "github.com/duke-git/lancet/v2/convertor"
函数列表:
- ColorHexToRGB
- ColorRGBToHex
- ToBool
- ToBytes
- ToChar
- ToChannel
- ToFloat
- ToInt
- ToJson
- ToMap
- ToPointer
- ToString
- StructToMap
- MapToSlice
- EncodeByte
- DecodeByte
5. cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。
import "github.com/duke-git/lancet/v2/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
6. datetime 日期时间处理包,格式化日期,比较日期。
import "github.com/duke-git/lancet/v2/datetime"
函数列表:
- AddDay
- AddHour
- AddMinute
- 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
7. datastructure 包含一些普通的数据结构实现。例如:list, linklist, stack, queue, set, tree, graph.
import list "github.com/duke-git/lancet/v2/datastructure/list"
import link "github.com/duke-git/lancet/v2/datastructure/link"
import stack "github.com/duke-git/lancet/v2/datastructure/stack"
import queue "github.com/duke-git/lancet/v2/datastructure/queue"
import set "github.com/duke-git/lancet/v2/datastructure/set"
import tree "github.com/duke-git/lancet/v2/datastructure/tree"
import heap "github.com/duke-git/lancet/v2/datastructure/heap"
import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
Function list:
8. fileutil 包支持文件基本操作。
import "github.com/duke-git/lancet/v2/fileutil"
函数列表:
- ClearFile
- CreateFile
- CreateDir
- CopyFile
- FileMode
- MiMeType
- IsExist
- IsLink
- IsDir
- ListFileNames
- RemoveFile
- ReadFileToString
- ReadFileByLine
- Zip
- UnZip
9. formatter 格式化器包含一些数据格式化处理方法。
import "github.com/duke-git/lancet/v2/formatter"
函数列表:
10. function 函数包控制函数执行流程,包含部分函数式编程。
import "github.com/duke-git/lancet/v2/function"
函数列表:
11. maputil 包包括一些操作 map 的函数.
import "github.com/duke-git/lancet/v2/maputil"
函数列表:
12. mathutil 包实现了一些数学计算的函数。
import "github.com/duke-git/lancet/v2/mathutil"
Function list:
- Average
- Exponent
- Fibonacci
- Factorial
- Max
- MaxBy
- Min
- MinBy
- Percent
- RoundToFloat
- RoundToString
- TruncRound
13. netutil 网络包支持获取 ip 地址,发送 http 请求。
import "github.com/duke-git/lancet/v2/netutil"
函数列表:
14. random 随机数生成器包,可以生成随机[]bytes, int, string。
import "github.com/duke-git/lancet/v2/random"
函数列表:
15. retry 重试执行函数直到函数运行成功或被 context cancel。
import "github.com/duke-git/lancet/v2/retry"
函数列表:
16. slice 包包含操作切片的方法集合。
import "github.com/duke-git/lancet/v2/slice"
函数列表:
- AppendIfAbsent
- Contain
- ContainSubSlice
- Chunk
- Compact
- Concat
- Count
- Difference
- DifferenceBy
- DifferenceWith
- DeleteAt
- Drop
- Every
- Filter
- Find
- FindLast
- Flatten
- FlattenDeep
- ForEach
- GroupBy
- GroupWith
- IntSlice
- InterfaceSlice
- Intersection
- InsertAt
- IndexOf
- LastIndexOf
- Map
- Reverse
- Reduce
- Replace
- ReplaceAll
- Shuffle
- SortByField
- Some
- StringSlice
- SymmetricDifference
- ToSlice
- ToSlicePointer
- Unique
- UniqueBy
- Union
- UniqueBy
- UpdateAt
- Without
- KeyBy
17. strutil 包含处理字符串的相关函数。
import "github.com/duke-git/lancet/v2/strutil"
函数列表:
- After
- AfterLast
- Before
- BeforeLast
- CamelCase
- Capitalize
- IsString
- KebabCase
- LowerFirst
- UpperFirst
- PadEnd
- PadStart
- Reverse
- SnakeCase
- SplitEx
- Wrap
- Unwrap
18. system 包含 os, runtime, shell command 相关函数。
import "github.com/duke-git/lancet/v2/system"
函数列表:
19. validator 验证器包,包含常用字符串格式验证函数。
import "github.com/duke-git/lancet/v2/validator"
函数列表:
- ContainChinese
- ContainLetter
- ContainLower
- ContainUpper
- IsAlpha
- IsAllUpper
- IsAllLower
- IsBase64
- IsChineseMobile
- IsChineseIdNum
- IsChinesePhone
- IsCreditCard
- IsDns
- IsEmail
- IsEmptyString
- IsFloatStr
- IsNumberStr
- IsJSON
- IsRegexMatch
- IsIntStr
- IsIp
- IsIpV4
- IsIpV6
- IsStrongPassword
- IsUrl
- IsWeakPassword
- IsZeroValue
20. xerror 包实现一些错误处理函数
import "github.com/duke-git/lancet/v2/xerror"
函数列表:
如何贡献代码
非常感激任何的代码提交以使 lancet 的功能越来越强大。创建 pull request 时请遵守以下规则。
- Fork lancet 仓库。
- 创建自己的特性分支。
- 提交变更。
- Push 分支。
- 创建新的 pull request。
