Lancet is a comprehensive, efficient, and reusable util function library of go. Inspired by the java apache common package and lodash.js.
English | 简体中文
Feature
👏 Comprehensive, efficient and reusable.💪 300+ go util functions, support string, slice, datetime, net, crypt...💅 Only depend on the go standard library.🌍 Unit test for every exported function.
Installation
Note:
- For users who use go1.18 and above, it is recommended to install lancet v2.x.x. Cause v2.x.x rewrite all functions with generics of go1.18.
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
- For users who use version below go1.18, you should install v1.x.x. now latest v1 is v1.3.0.
go get github.com/duke-git/lancet@v1.3.0 // below go1.18, install latest version of v1.x.x
Usage
Lancet organizes the code into package structure, and you need to import the corresponding package name when use it. For example, if you use string-related functions,import the strutil package like below:
import "github.com/duke-git/lancet/v2/strutil"
Example
Here takes the string function ReverseStr (reverse order string) as an example, and the strutil package needs to be imported.
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
s := "hello"
rs := strutil.ReverseStr(s)
fmt.Println(rs) //olleh
}
API Documentation
1. Algorithm package implements some basic 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 package contain some functions to support concurrent programming. eg, goroutine, channel, async.
import "github.com/duke-git/lancet/v2/concurrency"
Function list:
3. Convertor package contains some functions for data convertion.
import "github.com/duke-git/lancet/v2/convertor"
Function list:
4. Cryptor package is for data encryption and decryption.
import "github.com/duke-git/lancet/v2/cryptor"
Function list:
- 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
5. Datetime package supports date and time format and compare.
import "github.com/duke-git/lancet/v2/datetime"
Function list:
- 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
6. Datastructure package constains some common data structure. eg. 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"
Function list:
7. Fileutil package implements some basic functions for file operations.
import "github.com/duke-git/lancet/v2/fileutil"
Function list:
- ClearFile
- CreateFile
- CreateDir
- CopyFile
- FileMode
- MiMeType
- IsExist
- IsLink
- IsDir
- ListFileNames
- RemoveFile
- ReadFileToString
- ReadFileByLine
- Zip
- UnZip
8. Formatter contains some functions for data formatting.
import "github.com/duke-git/lancet/v2/formatter"
Function list:
9. Function package can control the flow of function execution and support part of functional programming
import "github.com/duke-git/lancet/v2/function"
Function list:
10. Maputil package includes some functions to manipulate map.
import "github.com/duke-git/lancet/v2/maputil"
Function list:
11. Mathutil package implements some functions for math calculation.
import "github.com/duke-git/lancet/v2/mathutil"
Function list:
- Average
- Exponent
- Fibonacci
- Factorial
- Max
- MaxBy
- Min
- MinBy
- Percent
- RoundToFloat
- RoundToString
- TruncRound
12. Netutil package contains functions to get net information and send http request.
import "github.com/duke-git/lancet/v2/netutil"
Function list:
- ConvertMapToQueryString
- GetInternalIp
- GetIps
- GetMacAddrs
- GetPublicIpInfo
- IsPublicIP
- HttpGet
- HttpDelete
- HttpPost
- HttpPut
- HttpPatch
- ParseHttpResponse
13. Random package implements some basic functions to generate random int and string.
import "github.com/duke-git/lancet/v2/random"
Function list:
14. Retry package is for executing a function repeatedly until it was successful or canceled by the context.
import "github.com/duke-git/lancet/v2/retry"
Function list:
15. Slice contains some functions to manipulate slice.
import "github.com/duke-git/lancet/v2/slice"
Function list:
- Contain
- ContainSubSlice
- Chunk
- Compact
- Concat
- Count
- Difference
- DifferenceBy
- DifferenceWith
- DeleteAt
- Drop
- Equal
- EqualWith
- Every
- Filter
- Find
- FindLast
- FlattenDeep
- ForEach
- GroupBy
- GroupWith
- IntSlice
- InterfaceSlice
- Intersection
- InsertAt
- IndexOf
- LastIndexOf
- Map
- Reverse
- Reduce
- Shuffle
- SortByField
- Some
- StringSlice
- SymmetricDifference
- Unique
- UniqueBy
- Union
- UpdateAt
- Without
16. Strutil package contains some functions to manipulate string.
import "github.com/duke-git/lancet/v2/strutil"
Function list:
- After
- AfterLast
- Before
- BeforeLast
- CamelCase
- Capitalize
- IsString
- KebabCase
- LowerFirst
- UpperFirst
- PadEnd
- PadStart
- ReverseStr
- SnakeCase
- SplitEx
- Wrap
- Unwrap
17. System package contain some functions about os, runtime, shell command.
import "github.com/duke-git/lancet/v2/system"
Function list:
18. Validator package contains some functions for data validation.
import "github.com/duke-git/lancet/v2/validator"
Function list:
- 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
19. xerror package implements helpers for errors.
import "github.com/duke-git/lancet/v2/xerror"
Function list:
How to Contribute
I really appreciate any code commits which make lancet lib powerful. Please follow the rules below to create your pull request.
- Fork the repository.
- Create your feature branch.
- Commit your changes.
- Push to the branch
- Create new pull request.