Lancet is a comprehensive, efficient, and reusable util function library of go. Inspired by the java apache common package and lodash.js.
English | 简体中文
- 👏 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.
- For users who use go1.18 and above, it is recommended to install lancet v2.x.x. Cause in v2.x.x all functions was rewriten 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. The latest of v1.x.x is v1.3.5.
go get github.com/duke-git/lancet@v1.3.5 // below go1.18, install latest version of v1.x.x
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"
Here takes the string function Reverse (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.Reverse(s)
fmt.Println(rs) //olleh
}
lancet API doc Thanks @UvDream for contributing.
import "github.com/duke-git/lancet/v2/algorithm"
- 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"
3. Condition package contains some functions for conditional judgment. eg. And, Or, TernaryOperator...
import "github.com/duke-git/lancet/v2/condition"
import "github.com/duke-git/lancet/v2/convertor"
- ColorHexToRGB
- ColorRGBToHex
- ToBool
- ToBytes
- ToChar
- ToChannel
- ToFloat
- ToInt
- ToJson
- ToMap
- ToPointer
- ToString
- StructToMap
- MapToSlice
- EncodeByte
- DecodeByte
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
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 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"
import heap "github.com/duke-git/lancet/v2/datastructure/heap"
import hashmap "github.com/duke-git/lancet/v2/datastructure/hashmap"
import "github.com/duke-git/lancet/v2/fileutil"
- ClearFile
- CreateFile
- CreateDir
- CopyFile
- FileMode
- MiMeType
- IsExist
- IsLink
- IsDir
- ListFileNames
- RemoveFile
- ReadFileToString
- ReadFileByLine
- Zip
- UnZip
import "github.com/duke-git/lancet/v2/formatter"
10. Function package can control the flow of function execution and support part of functional programming
import "github.com/duke-git/lancet/v2/function"
import "github.com/duke-git/lancet/v2/maputil"
import "github.com/duke-git/lancet/v2/mathutil"
- Average
- Exponent
- Fibonacci
- Factorial
- Max
- MaxBy
- Min
- MinBy
- Percent
- RoundToFloat
- RoundToString
- TruncRound
import "github.com/duke-git/lancet/v2/netutil"
- ConvertMapToQueryString
- EncodeUrl
- GetInternalIp
- GetIps
- GetMacAddrs
- GetPublicIpInfo
- GetRequestPublicIp
- IsPublicIP
- IsInternalIP
- HttpRequest
- HttpClient
- SendRequest
- DecodeResponse
- StructToUrlValues
- HttpGetdeprecated
- HttpDeletedeprecated
- HttpPostdeprecated
- HttpPutdeprecated
- HttpPatchdeprecated
- ParseHttpResponse
import "github.com/duke-git/lancet/v2/random"
15. 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"
import "github.com/duke-git/lancet/v2/slice"
- AppendIfAbsent
- Contain
- ContainSubSlice
- Chunk
- Compact
- Concat
- Count
- CountBy
- Difference
- DifferenceBy
- DifferenceWith
- DeleteAt
- Drop
- Equal
- EqualWith
- Every
- Filter
- Find
- FindLast
- Flatten
- FlattenDeep
- ForEach
- GroupBy
- GroupWith
- IntSlicedeprecated
- InterfaceSlicedeprecated
- Intersection
- InsertAt
- IndexOf
- LastIndexOf
- Map
- Merge
- Reverse
- Reduce
- Replace
- ReplaceAll
- Repeat
- Shuffle
- Sort
- SortBy
- SortByFielddeprecated
- Some
- StringSlicedeprecated
- SymmetricDifference
- ToSlice
- ToSlicePointer
- Unique
- UniqueBy
- Union
- UnionBy
- UpdateAt
- Without
- KeyBy
import "github.com/duke-git/lancet/v2/strutil"
- After
- AfterLast
- Before
- BeforeLast
- CamelCase
- Capitalize
- IsString
- KebabCase
- UpperKebabCase
- LowerFirst
- UpperFirst
- PadEnd
- PadStart
- Reverse
- SnakeCase
- UpperSnakeCase
- SplitEx
- Wrap
- Unwrap
import "github.com/duke-git/lancet/v2/system"
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
- IsGBK
import "github.com/duke-git/lancet/v2/xerror"
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.