A comprehensive, efficient, and reusable util function library of Go.
Lancet is a comprehensive, efficient, and reusable util function library of go. Inspired by the java apache common package and lodash.js.
##
Website | [简体中文](./README_zh-CN.md)
## Features
- Comprehensive, efficient and reusable.
- 700+ go util functions, support string, slice, datetime, net, crypt...
- Only depends on two kinds of libraries: go standard library and golang.org/x.
- Unit test for every exported function.
## Installation
### Note:
1.
For users who use go1.18 and above, it is recommended to install lancet v2.x.x. Cause in v2.x.x all functions were rewritten with generics of go1.18.
```go
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
```
2.
For users who use version below go1.18, you should install v1.x.x. The latest of v1.x.x is v1.4.6.
```go
go get github.com/duke-git/lancet // 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:
```go
import "github.com/duke-git/lancet/v2/strutil"
```
## Example
Here takes the string function Reverse (reverse order string) as an example, and the strutil package needs to be imported.
```go
package main
import (
"fmt"
"github.com/duke-git/lancet/v2/strutil"
)
func main() {
s := "hello"
rs := strutil.Reverse(s)
fmt.Println(rs) //olleh
}
```
## Documentation
### Index
- [Algorithm](#user-content-algorithm)
- [Compare](#user-content-compare)
- [Concurrency](#user-content-concurrency)
- [Condition](#user-content-condition)
- [Convertor](#user-content-convertor)
- [Cryptor](#user-content-cryptor)
- [Datetime](#user-content-datetime)
- [Datastructure](#user-content-datastructure)
- [EventBus](#user-content-eventbus)
- [Enum](#user-content-enum)
- [Fileutil](#user-content-fileutil)
- [Formatter](#user-content-formatter)
- [Function](#user-content-function)
- [Maputil](#user-content-maputil)
- [Mathutil](#user-content-mathutil)
- [Netutil](#user-content-netutil)
- [Pointer](#user-content-pointer)
- [Random](#user-content-random)
- [Retry](#user-content-retry)
- [Slice](#user-content-slice)
- [Stream](#user-content-stream)
- [Structs](#user-content-structs)
- [Strutil](#user-content-strutil)
- [System](#user-content-system)
- [Tuple](#user-content-tuple)
- [Validator](#user-content-validator)
- [Xerror](#user-content-xerror)
1. Algorithm package implements some basic algorithm. eg. sort, search. index
```go
import "github.com/duke-git/lancet/v2/algorithm"
```
#### Function list:
- **BubbleSort** : sorts slice with bubble sort algorithm, will change the original slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/algorithm.md#BubbleSort)]
[[play](https://go.dev/play/p/GNdv7Jg2Taj)]
- **CountSort** : sorts slice with bubble sort algorithm, don't change original slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/algorithm.md#CountSort)]
[[play](https://go.dev/play/p/tB-Umgm0DrP)]
- **HeapSort** : sorts slice with heap sort algorithm, will change the original slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/algorithm.md#HeapSort)]
[[play](https://go.dev/play/p/u6Iwa1VZS_f)]
- **InsertionSort** : sorts slice with insertion sort algorithm, will change the original slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/algorithm.md#InsertionSort)]
[[play](https://go.dev/play/p/G5LJiWgJJW6)]
- **MergeSort** : sorts slice with merge sort algorithm, will change the original slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/algorithm.md#MergeSort)]
[[play](https://go.dev/play/p/ydinn9YzUJn)]
- **QuickSort** : sorts slice with quick sort algorithm, will change the original slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/algorithm.md#QuickSort)]
[[play](https://go.dev/play/p/7Y7c1Elk3ax)]
- **SelectionSort** : sorts slice with selection sort algorithm, will change the original slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/algorithm.md#SelectionSort)]
[[play](https://go.dev/play/p/oXovbkekayS)]
- **ShellSort** : sorts slice with shell sort algorithm, will change the original slice.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/algorithm.md#ShellSort)]
[[play](https://go.dev/play/p/3ibkszpJEu3)]
- **BinarySearch** : returns the index of target within a sorted slice, use binary search (recursive call itself).
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/algorithm.md#BinarySearch)]
[[play](https://go.dev/play/p/t6MeGiUSN47)]
- **BinaryIterativeSearch** : returns the index of target within a sorted slice, use binary search (no recursive).
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/algorithm.md#BinaryIterativeSearch)]
[[play](https://go.dev/play/p/Anozfr8ZLH3)]
- **LinearSearch** : returns the index of target in slice base on equal function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/algorithm.md#LinearSearch)]
[[play](https://go.dev/play/p/IsS7rgn5s3x)]
- **LRUCache** : implements memory cache with lru algorithm.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/algorithm.md#LRUCache)]
[[play](https://go.dev/play/p/-EZjgOURufP)]
2. Compare package provides a lightweight comparison function on any type. index
```go
import "github.com/duke-git/lancet/v2/compare"
```
#### Function list:
- **Equal** : Checks if two values are equal or not. (check both type and value)
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/compare.md#Equal)]
[[play](https://go.dev/play/p/wmVxR-to4lz)]
- **EqualValue** : Checks if two values are equal or not. (check value only)
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/compare.md#EqualValue)]
[[play](https://go.dev/play/p/fxnna_LLD9u)]
- **LessThan** : Checks if value `left` less than value `right`.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/compare.md#LessThan)]
[[play](https://go.dev/play/p/cYh7FQQj0ne)]
- **GreaterThan** : Checks if value `left` greater than value `right`.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/compare.md#GreaterThan)]
[[play](https://go.dev/play/p/9-NYDFZmIMp)]
- **LessOrEqual** : Checks if value `left` less than or equal than value `right`.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/compare.md#LessOrEqual)]
[[play](https://go.dev/play/p/e4T_scwoQzp)]
- **GreaterOrEqual** : Checks if value `left` less greater or equal than value `right`.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/compare.md#GreaterOrEqual)]
[[play](https://go.dev/play/p/vx8mP0U8DFk)]
- **InDelta** : Checks if two values are equal or not within a delta.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/compare.md#InDelta)]
3. Concurrency package contain some functions to support concurrent programming. eg, goroutine, channel, async. index
```go
import "github.com/duke-git/lancet/v2/concurrency"
```
#### Function list:
- **NewChannel** : create a Channel pointer instance.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#NewChannel)]
[[play](https://go.dev/play/p/7aB4KyMMp9A)]
- **Bridge** : link multiply channels into one channel.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/Bridge.md#NewChannel)]
[[play](https://go.dev/play/p/qmWSy1NVF-Y)]
- **FanIn** : merge multiple channels into one channel.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#FanIn)]
[[play](https://go.dev/play/p/2VYFMexEvTm)]
- **Generate** : creates a channel, then put values into the channel.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#Generate)]
[[play](https://go.dev/play/p/7aB4KyMMp9A)]
- **Or** : read one or more channels into one channel, will close when any readin channel is closed.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#Or)]
[[play](https://go.dev/play/p/Wqz9rwioPww)]
- **OrDone** : read a channel into another channel, will close until cancel context.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#OrDone)]
[[play](https://go.dev/play/p/lm_GoS6aDjo)]
- **Repeat** : create channel, put values into the channel repeatedly until cancel the context.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#Repeat)]
[[play](https://go.dev/play/p/k5N_ALVmYjE)]
- **RepeatFn** : create a channel, executes fn repeatedly, and put the result into the channel, until close context.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#RepeatFn)]
[[play](https://go.dev/play/p/4J1zAWttP85)]
- **Take** : create a channel whose values are taken from another channel with limit number.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#Take)]
[[play](https://go.dev/play/p/9Utt-1pDr2J)]
- **Tee** : split one chanel into two channels, until cancel the context.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#Tee)]
[[play](https://go.dev/play/p/3TQPKnCirrP)]
- **NewKeyedLocker** : KeyedLocker is a simple implementation of a keyed locker that allows for non-blocking lock acquisition.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#NewKeyedLocker)]
[[play](https://go.dev/play/p/GzeyC33T5rw)]
- **Do** :acquires a lock for the specified key and executes the provided function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#Do)]
[[play](https://go.dev/play/p/GzeyC33T5rw)]
- **NewRWKeyedLocker** :RRWKeyedLocker is a read-write version of KeyedLocker.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#NewRWKeyedLocker)]
[[play](https://go.dev/play/p/ZrCr8sMo77T)]
- **RLock** : acquires a read lock for the specified key and executes the provided function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#RLock)]
[[play](https://go.dev/play/p/ZrCr8sMo77T)]
- **Lock** : acquires a write lock for the specified key and executes the provided function.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#Lock)]
[[play](https://go.dev/play/p/WgAcXbOPKGk)]
- **NewTryKeyedLocker** : TryKeyedLocker is a non-blocking version of KeyedLocker.
[[doc](https://github.com/duke-git/lancet/blob/