mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
doc: add example and update docment for channel
This commit is contained in:
275
README.md
275
README.md
@@ -32,7 +32,7 @@ English | [简体中文](./README_zh-CN.md)
|
||||
|
||||
### Note:
|
||||
|
||||
1. <b>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.</b>
|
||||
1. <b>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.</b>
|
||||
|
||||
```go
|
||||
go get github.com/duke-git/lancet/v2 // will install latest version of v2.x.x
|
||||
@@ -73,7 +73,6 @@ func main() {
|
||||
|
||||
## Documentation
|
||||
|
||||
|
||||
### 1. Algorithm package implements some basic algorithm. eg. sort, search.
|
||||
|
||||
```go
|
||||
@@ -81,41 +80,42 @@ import "github.com/duke-git/lancet/v2/algorithm"
|
||||
```
|
||||
|
||||
#### Function list:
|
||||
- **<big>BubbleSort</big>** : sorts slice with bubble sort algorithm, will change the original slice.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BubbleSort)]
|
||||
[[play](https://go.dev/play/p/GNdv7Jg2Taj)]
|
||||
- **<big>CountSort</big>** : sorts slice with bubble sort algorithm, don't change original slice.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#CountSort)]
|
||||
[[play](https://go.dev/play/p/tB-Umgm0DrP)]
|
||||
- **<big>HeapSort</big>** : sorts slice with heap sort algorithm, will change the original slice.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#HeapSort)]
|
||||
[[play](https://go.dev/play/p/u6Iwa1VZS_f)]
|
||||
- **<big>InsertionSort</big>** : sorts slice with insertion sort algorithm, will change the original slice.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#InsertionSort)]
|
||||
[[play](https://go.dev/play/p/G5LJiWgJJW6)]
|
||||
- **<big>MergeSort</big>** : sorts slice with merge sort algorithm, will change the original slice.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#MergeSort)]
|
||||
[[play](https://go.dev/play/p/ydinn9YzUJn)]
|
||||
- **<big>QuickSort</big>** : sorts slice with quick sort algorithm, will change the original slice.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#QuickSort)]
|
||||
[[play](https://go.dev/play/p/7Y7c1Elk3ax)]
|
||||
- **<big>SelectionSort</big>** : sorts slice with selection sort algorithm, will change the original slice.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#SelectionSort)]
|
||||
[[play](https://go.dev/play/p/oXovbkekayS)]
|
||||
- **<big>ShellSort</big>** : sorts slice with shell sort algorithm, will change the original slice.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#ShellSort)]
|
||||
[[play](https://go.dev/play/p/3ibkszpJEu3)]
|
||||
- **<big>BinarySearch</big>** : 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/algorithm.md#BinarySearch)]
|
||||
[[play](https://go.dev/play/p/t6MeGiUSN47)]
|
||||
- **<big>BinaryIterativeSearch</big>** : returns the index of target within a sorted slice, use binary search (no recursive).
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BinaryIterativeSearch)]
|
||||
[[play](https://go.dev/play/p/Anozfr8ZLH3)]
|
||||
- **<big>LinearSearch</big>** : returns the index of target in slice base on equal function.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LinearSearch)]
|
||||
- **<big>LRUCache</big>** : implements memory cache with lru algorithm.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LRUCache)]
|
||||
[[play](https://go.dev/play/p/-EZjgOURufP)]
|
||||
|
||||
- **<big>BubbleSort</big>** : sorts slice with bubble sort algorithm, will change the original slice.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BubbleSort)]
|
||||
[[play](https://go.dev/play/p/GNdv7Jg2Taj)]
|
||||
- **<big>CountSort</big>** : sorts slice with bubble sort algorithm, don't change original slice.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#CountSort)]
|
||||
[[play](https://go.dev/play/p/tB-Umgm0DrP)]
|
||||
- **<big>HeapSort</big>** : sorts slice with heap sort algorithm, will change the original slice.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#HeapSort)]
|
||||
[[play](https://go.dev/play/p/u6Iwa1VZS_f)]
|
||||
- **<big>InsertionSort</big>** : sorts slice with insertion sort algorithm, will change the original slice.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#InsertionSort)]
|
||||
[[play](https://go.dev/play/p/G5LJiWgJJW6)]
|
||||
- **<big>MergeSort</big>** : sorts slice with merge sort algorithm, will change the original slice.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#MergeSort)]
|
||||
[[play](https://go.dev/play/p/ydinn9YzUJn)]
|
||||
- **<big>QuickSort</big>** : sorts slice with quick sort algorithm, will change the original slice.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#QuickSort)]
|
||||
[[play](https://go.dev/play/p/7Y7c1Elk3ax)]
|
||||
- **<big>SelectionSort</big>** : sorts slice with selection sort algorithm, will change the original slice.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#SelectionSort)]
|
||||
[[play](https://go.dev/play/p/oXovbkekayS)]
|
||||
- **<big>ShellSort</big>** : sorts slice with shell sort algorithm, will change the original slice.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#ShellSort)]
|
||||
[[play](https://go.dev/play/p/3ibkszpJEu3)]
|
||||
- **<big>BinarySearch</big>** : 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/algorithm.md#BinarySearch)]
|
||||
[[play](https://go.dev/play/p/t6MeGiUSN47)]
|
||||
- **<big>BinaryIterativeSearch</big>** : returns the index of target within a sorted slice, use binary search (no recursive).
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#BinaryIterativeSearch)]
|
||||
[[play](https://go.dev/play/p/Anozfr8ZLH3)]
|
||||
- **<big>LinearSearch</big>** : returns the index of target in slice base on equal function.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LinearSearch)]
|
||||
- **<big>LRUCache</big>** : implements memory cache with lru algorithm.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LRUCache)]
|
||||
[[play](https://go.dev/play/p/-EZjgOURufP)]
|
||||
|
||||
### 2. Concurrency package contain some functions to support concurrent programming. eg, goroutine, channel, async.
|
||||
|
||||
@@ -125,16 +125,26 @@ import "github.com/duke-git/lancet/v2/concurrency"
|
||||
|
||||
#### Function list:
|
||||
|
||||
- [NewChannel](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#NewChannel)
|
||||
- [Bridge](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Bridge)
|
||||
- [FanIn](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#FanIn)
|
||||
- [Generate](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Generate)
|
||||
- [Or](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Or)
|
||||
- [OrDone](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#OrDone)
|
||||
- [Repeat](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Repeat)
|
||||
- [RepeatFn](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#RepeatFn)
|
||||
- [Take](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Take)
|
||||
- [Tee](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Tee)
|
||||
- **<big>NewChannel</big>** : create a Channel pointer instance.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#NewChannel)]
|
||||
- **<big>Bridge</big>** : link multiply channels into one channel.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/Bridge.md#NewChannel)]
|
||||
- **<big>FanIn</big>** : merge multiple channels into one channel.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#FanIn)]
|
||||
- **<big>Generate</big>** : creates a channel, then put values into the channel.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Generate)]
|
||||
- **<big>Or</big>** : 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/concurrency.md#Or)]
|
||||
- **<big>OrDone</big>** : read a channel into another channel, will close until cancel context.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#OrDone)]
|
||||
- **<big>Repeat</big>** : create channel, put values into the channel repeatly until cancel the context.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Repeat)]
|
||||
- **<big>RepeatFn</big>** : create a channel, excutes fn repeatly, and put the result into the channel, until close context.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#RepeatFn)]
|
||||
- **<big>Take</big>** : create a channel whose values are taken from another channel with limit number.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Take)]
|
||||
- **<big>Tee</big>** : split one chanel into two channels, until cancel the context.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Tee)]
|
||||
|
||||
### 3. Condition package contains some functions for conditional judgment. eg. And, Or, TernaryOperator...
|
||||
|
||||
@@ -500,64 +510,64 @@ import "github.com/duke-git/lancet/v2/strutil"
|
||||
|
||||
#### Function list:
|
||||
|
||||
- **<big>After</big>** : returns the substring after the first occurrence of a specified string in the source string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#After)]
|
||||
[[play](https://go.dev/play/p/RbCOQqCDA7m)]
|
||||
- **<big>AfterLast</big>** : returns the substring after the last occurrence of a specified string in the source string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#AfterLast)]
|
||||
[[play](https://go.dev/play/p/1TegARrb8Yn)]
|
||||
- **<big>Before</big>** : returns the substring before the first occurrence of a specified string in the source string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Before)]
|
||||
[[play](https://go.dev/play/p/JAWTZDS4F5w)]
|
||||
- **<big>BeforeLast</big>** : returns the substring before the last occurrence of a specified string in the source string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#BeforeLast)]
|
||||
[[play](https://go.dev/play/p/pJfXXAoG_Te)]
|
||||
- **<big>CamelCase</big>** : coverts source string to its camelCase string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#CamelCase)]
|
||||
[[play](https://go.dev/play/p/9eXP3tn2tUy)]
|
||||
- **<big>Capitalize</big>** : converts the first character of source string to upper case and the remaining to lower case.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Capitalize)]
|
||||
[[play](https://go.dev/play/p/2OAjgbmAqHZ)]
|
||||
- **<big>IsString</big>** : checks if the parameter value data type is string or not.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#IsString)]
|
||||
[[play](https://go.dev/play/p/IOgq7oF9ERm)]
|
||||
- **<big>KebabCase</big>** : coverts string to kebab-case string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#KebabCase)]
|
||||
[[play](https://go.dev/play/p/dcZM9Oahw-Y)]
|
||||
- **<big>UpperKebabCase</big>** : coverts string to upper KEBAB-CASE string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperKebabCase)]
|
||||
[[play](https://go.dev/play/p/zDyKNneyQXk)]
|
||||
- **<big>LowerFirst</big>** : converts the first character of string to lower case.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#LowerFirst)]
|
||||
[[play](https://go.dev/play/p/CbzAyZmtJwL)]
|
||||
- **<big>UpperFirst</big>** : converts the first character of string to upper case.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperFirst)]
|
||||
[[play](https://go.dev/play/p/sBbBxRbs8MM)]
|
||||
- **<big>PadEnd</big>** : pads string with given characters on the right side if it's shorter than limit size. Padding characters are truncated if they exceed size.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadEnd)]
|
||||
[[play](https://go.dev/play/p/9xP8rN0vz--)]
|
||||
- **<big>PadStart</big>** : pads string with given characters on the left side if it's shorter than limit size. Padding characters are truncated if they exceed size.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadStart)]
|
||||
[[play](https://go.dev/play/p/xpTfzArDfvT)]
|
||||
- **<big>Reverse</big>** : returns string whose char order is reversed to the given string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Reverse)]
|
||||
[[play](https://go.dev/play/p/adfwalJiecD)]
|
||||
- **<big>SnakeCase</big>** : coverts string to snake_case string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SnakeCase)]
|
||||
[[play](https://go.dev/play/p/tgzQG11qBuN)]
|
||||
- **<big>UpperSnakeCase</big>** : coverts string to upper SNAKE_CASE string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperSnakeCase)]
|
||||
[[play](https://go.dev/play/p/4COPHpnLx38)]
|
||||
- **<big>SplitEx</big>** : split a given string which can control the result slice contains empty string or not.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SplitEx)]
|
||||
[[play](https://go.dev/play/p/Us-ySSbWh-3)]
|
||||
- **<big>Substring</big>** : returns a substring of the specified length starting at the specified offset position.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)]
|
||||
- **<big>Wrap</big>** : wrap a string with given string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap)]
|
||||
[[play](https://go.dev/play/p/KoZOlZDDt9y)]
|
||||
- **<big>Unwrap</big>** : unwrap a given string from anther string. will change source string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Unwrap)]
|
||||
[[play](https://go.dev/play/p/Ec2q4BzCpG-)]
|
||||
- **<big>After</big>** : returns the substring after the first occurrence of a specified string in the source string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#After)]
|
||||
[[play](https://go.dev/play/p/RbCOQqCDA7m)]
|
||||
- **<big>AfterLast</big>** : returns the substring after the last occurrence of a specified string in the source string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#AfterLast)]
|
||||
[[play](https://go.dev/play/p/1TegARrb8Yn)]
|
||||
- **<big>Before</big>** : returns the substring before the first occurrence of a specified string in the source string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Before)]
|
||||
[[play](https://go.dev/play/p/JAWTZDS4F5w)]
|
||||
- **<big>BeforeLast</big>** : returns the substring before the last occurrence of a specified string in the source string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#BeforeLast)]
|
||||
[[play](https://go.dev/play/p/pJfXXAoG_Te)]
|
||||
- **<big>CamelCase</big>** : coverts source string to its camelCase string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#CamelCase)]
|
||||
[[play](https://go.dev/play/p/9eXP3tn2tUy)]
|
||||
- **<big>Capitalize</big>** : converts the first character of source string to upper case and the remaining to lower case.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Capitalize)]
|
||||
[[play](https://go.dev/play/p/2OAjgbmAqHZ)]
|
||||
- **<big>IsString</big>** : checks if the parameter value data type is string or not.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#IsString)]
|
||||
[[play](https://go.dev/play/p/IOgq7oF9ERm)]
|
||||
- **<big>KebabCase</big>** : coverts string to kebab-case string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#KebabCase)]
|
||||
[[play](https://go.dev/play/p/dcZM9Oahw-Y)]
|
||||
- **<big>UpperKebabCase</big>** : coverts string to upper KEBAB-CASE string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperKebabCase)]
|
||||
[[play](https://go.dev/play/p/zDyKNneyQXk)]
|
||||
- **<big>LowerFirst</big>** : converts the first character of string to lower case.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#LowerFirst)]
|
||||
[[play](https://go.dev/play/p/CbzAyZmtJwL)]
|
||||
- **<big>UpperFirst</big>** : converts the first character of string to upper case.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperFirst)]
|
||||
[[play](https://go.dev/play/p/sBbBxRbs8MM)]
|
||||
- **<big>PadEnd</big>** : pads string with given characters on the right side if it's shorter than limit size. Padding characters are truncated if they exceed size.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadEnd)]
|
||||
[[play](https://go.dev/play/p/9xP8rN0vz--)]
|
||||
- **<big>PadStart</big>** : pads string with given characters on the left side if it's shorter than limit size. Padding characters are truncated if they exceed size.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadStart)]
|
||||
[[play](https://go.dev/play/p/xpTfzArDfvT)]
|
||||
- **<big>Reverse</big>** : returns string whose char order is reversed to the given string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Reverse)]
|
||||
[[play](https://go.dev/play/p/adfwalJiecD)]
|
||||
- **<big>SnakeCase</big>** : coverts string to snake_case string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SnakeCase)]
|
||||
[[play](https://go.dev/play/p/tgzQG11qBuN)]
|
||||
- **<big>UpperSnakeCase</big>** : coverts string to upper SNAKE_CASE string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperSnakeCase)]
|
||||
[[play](https://go.dev/play/p/4COPHpnLx38)]
|
||||
- **<big>SplitEx</big>** : split a given string which can control the result slice contains empty string or not.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SplitEx)]
|
||||
[[play](https://go.dev/play/p/Us-ySSbWh-3)]
|
||||
- **<big>Substring</big>** : returns a substring of the specified length starting at the specified offset position.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)]
|
||||
- **<big>Wrap</big>** : wrap a string with given string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap)]
|
||||
[[play](https://go.dev/play/p/KoZOlZDDt9y)]
|
||||
- **<big>Unwrap</big>** : unwrap a given string from anther string. will change source string.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Unwrap)]
|
||||
[[play](https://go.dev/play/p/Ec2q4BzCpG-)]
|
||||
|
||||
### 19. System package contain some functions about os, runtime, shell command.
|
||||
|
||||
@@ -566,33 +576,34 @@ import "github.com/duke-git/lancet/v2/system"
|
||||
```
|
||||
|
||||
#### Function list:
|
||||
- **<big>IsWindows</big>** : check if current os is windows.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#IsWindows)]
|
||||
[[play](https://go.dev/play/p/XzJULbzmf9m)]
|
||||
- **<big>IsLinux</big>** : check if current os is linux.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#IsLinux)]
|
||||
[[play](https://go.dev/play/p/zIflQgZNuxD)]
|
||||
- **<big>IsMac</big>** : check if current os is macos.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#IsMac)]
|
||||
[[play](https://go.dev/play/p/Mg4Hjtyq7Zc)]
|
||||
- **<big>GetOsEnv</big>** : get the value of the environment variable named by the key.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#GetOsEnv)]
|
||||
[[play](https://go.dev/play/p/D88OYVCyjO-)]
|
||||
- **<big>SetOsEnv</big>** : set the value of the environment variable named by the key.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#SetOsEnv)]
|
||||
[[play](https://go.dev/play/p/D88OYVCyjO-)]
|
||||
- **<big>RemoveOsEnv</big>** : remove a single environment variable.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#RemoveOsEnv)]
|
||||
[[play](https://go.dev/play/p/fqyq4b3xUFQ)]
|
||||
- **<big>CompareOsEnv</big>** : get env named by the key and compare it with passed env.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#CompareOsEnv)]
|
||||
[[play](https://go.dev/play/p/BciHrKYOHbp)]
|
||||
- **<big>ExecCommand</big>** : execute command, return the stdout and stderr string of command, and error if error occurs.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#ExecCommand)]
|
||||
[[play](https://go.dev/play/p/n-2fLyZef-4)]
|
||||
- **<big>GetOsBits</big>** : return current os bits (32 or 64).
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#GetOsBits)]
|
||||
[[play](https://go.dev/play/p/ml-_XH3gJbW)]
|
||||
|
||||
- **<big>IsWindows</big>** : check if current os is windows.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#IsWindows)]
|
||||
[[play](https://go.dev/play/p/XzJULbzmf9m)]
|
||||
- **<big>IsLinux</big>** : check if current os is linux.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#IsLinux)]
|
||||
[[play](https://go.dev/play/p/zIflQgZNuxD)]
|
||||
- **<big>IsMac</big>** : check if current os is macos.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#IsMac)]
|
||||
[[play](https://go.dev/play/p/Mg4Hjtyq7Zc)]
|
||||
- **<big>GetOsEnv</big>** : get the value of the environment variable named by the key.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#GetOsEnv)]
|
||||
[[play](https://go.dev/play/p/D88OYVCyjO-)]
|
||||
- **<big>SetOsEnv</big>** : set the value of the environment variable named by the key.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#SetOsEnv)]
|
||||
[[play](https://go.dev/play/p/D88OYVCyjO-)]
|
||||
- **<big>RemoveOsEnv</big>** : remove a single environment variable.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#RemoveOsEnv)]
|
||||
[[play](https://go.dev/play/p/fqyq4b3xUFQ)]
|
||||
- **<big>CompareOsEnv</big>** : get env named by the key and compare it with passed env.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#CompareOsEnv)]
|
||||
[[play](https://go.dev/play/p/BciHrKYOHbp)]
|
||||
- **<big>ExecCommand</big>** : execute command, return the stdout and stderr string of command, and error if error occurs.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#ExecCommand)]
|
||||
[[play](https://go.dev/play/p/n-2fLyZef-4)]
|
||||
- **<big>GetOsBits</big>** : return current os bits (32 or 64).
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system.md#GetOsBits)]
|
||||
[[play](https://go.dev/play/p/ml-_XH3gJbW)]
|
||||
|
||||
### 19. Validator package contains some functions for data validation.
|
||||
|
||||
|
||||
307
README_zh-CN.md
307
README_zh-CN.md
@@ -31,13 +31,13 @@
|
||||
|
||||
### Note:
|
||||
|
||||
1. <b>使用go1.18及以上版本的用户,建议安装v2.x.x。 因为v2.x.x应用go1.18的泛型重写了大部分函数。</b>
|
||||
1. <b>使用 go1.18 及以上版本的用户,建议安装 v2.x.x。 因为 v2.x.x 应用 go1.18 的泛型重写了大部分函数。</b>
|
||||
|
||||
```go
|
||||
go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x
|
||||
```
|
||||
|
||||
2. <b>使用go1.18以下版本的用户,必须安装v1.x.x。目前最新的v1版本是v1.3.5。</b>
|
||||
2. <b>使用 go1.18 以下版本的用户,必须安装 v1.x.x。目前最新的 v1 版本是 v1.3.5。</b>
|
||||
|
||||
```go
|
||||
go get github.com/duke-git/lancet@v1.3.5 // 使用go1.18以下版本, 必须安装v1.x.x版本
|
||||
@@ -72,50 +72,51 @@ func main() {
|
||||
|
||||
## 文档
|
||||
|
||||
### 1. algorithm包实现一些基本查找和排序算法。
|
||||
### 1. algorithm 包实现一些基本查找和排序算法。
|
||||
|
||||
```go
|
||||
import "github.com/duke-git/lancet/v2/algorithm"
|
||||
```
|
||||
|
||||
#### Function list:
|
||||
- **<big>BubbleSort</big>** : 使用冒泡排序算法对切片进行排序。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BubbleSort)]
|
||||
[[play](https://go.dev/play/p/GNdv7Jg2Taj)]
|
||||
- **<big>CountSort</big>** : 使用计数排序算法对切片进行排序。不改变原数据。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#CountSort)]
|
||||
[[play](https://go.dev/play/p/tB-Umgm0DrP)]
|
||||
- **<big>HeapSort</big>** : 使用堆排序算法对切片进行排序。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#HeapSort)]
|
||||
[[play](https://go.dev/play/p/u6Iwa1VZS_f)]
|
||||
- **<big>InsertionSort</big>** : 使用插入排序算法对切片进行排序。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#InsertionSort)]
|
||||
[[play](https://go.dev/play/p/G5LJiWgJJW6)]
|
||||
- **<big>MergeSort</big>** : 使用合并排序算法对切片进行排序。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#MergeSort)]
|
||||
[[play](https://go.dev/play/p/ydinn9YzUJn)]
|
||||
- **<big>QuickSort</big>** : 使用快速排序算法对切片进行排序。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#QuickSort)]
|
||||
[[play](https://go.dev/play/p/7Y7c1Elk3ax)]
|
||||
- **<big>SelectionSort</big>** : 使用选择排序算法对切片进行排序。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#SelectionSort)]
|
||||
[[play](https://go.dev/play/p/oXovbkekayS)]
|
||||
- **<big>ShellSort</big>** : 使用希尔排序算法对切片进行排序。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#ShellSort)]
|
||||
[[play](https://go.dev/play/p/3ibkszpJEu3)]
|
||||
- **<big>BinarySearch</big>** : 返回排序切片中目标值的索引,使用二分搜索(递归调用)。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BinarySearch)]
|
||||
[[play](https://go.dev/play/p/t6MeGiUSN47)]
|
||||
- **<big>BinaryIterativeSearch</big>** :返回排序切片中目标值的索引,使用二分搜索(非递归)。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BinaryIterativeSearch)]
|
||||
[[play](https://go.dev/play/p/Anozfr8ZLH3)]
|
||||
- **<big>LinearSearch</big>** : 基于传入的相等函数返回切片中目标值的索引。(线性查找)
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch)]
|
||||
- **<big>LRUCache</big>** : 应用lru算法实现内存缓存.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache)]
|
||||
[[play](https://go.dev/play/p/-EZjgOURufP)]
|
||||
|
||||
### 2. concurrency包含一些支持并发编程的功能。例如:goroutine, channel, async 等。
|
||||
- **<big>BubbleSort</big>** : 使用冒泡排序算法对切片进行排序。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BubbleSort)]
|
||||
[[play](https://go.dev/play/p/GNdv7Jg2Taj)]
|
||||
- **<big>CountSort</big>** : 使用计数排序算法对切片进行排序。不改变原数据。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#CountSort)]
|
||||
[[play](https://go.dev/play/p/tB-Umgm0DrP)]
|
||||
- **<big>HeapSort</big>** : 使用堆排序算法对切片进行排序。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#HeapSort)]
|
||||
[[play](https://go.dev/play/p/u6Iwa1VZS_f)]
|
||||
- **<big>InsertionSort</big>** : 使用插入排序算法对切片进行排序。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#InsertionSort)]
|
||||
[[play](https://go.dev/play/p/G5LJiWgJJW6)]
|
||||
- **<big>MergeSort</big>** : 使用合并排序算法对切片进行排序。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#MergeSort)]
|
||||
[[play](https://go.dev/play/p/ydinn9YzUJn)]
|
||||
- **<big>QuickSort</big>** : 使用快速排序算法对切片进行排序。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#QuickSort)]
|
||||
[[play](https://go.dev/play/p/7Y7c1Elk3ax)]
|
||||
- **<big>SelectionSort</big>** : 使用选择排序算法对切片进行排序。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#SelectionSort)]
|
||||
[[play](https://go.dev/play/p/oXovbkekayS)]
|
||||
- **<big>ShellSort</big>** : 使用希尔排序算法对切片进行排序。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#ShellSort)]
|
||||
[[play](https://go.dev/play/p/3ibkszpJEu3)]
|
||||
- **<big>BinarySearch</big>** : 返回排序切片中目标值的索引,使用二分搜索(递归调用)。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BinarySearch)]
|
||||
[[play](https://go.dev/play/p/t6MeGiUSN47)]
|
||||
- **<big>BinaryIterativeSearch</big>** :返回排序切片中目标值的索引,使用二分搜索(非递归)。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BinaryIterativeSearch)]
|
||||
[[play](https://go.dev/play/p/Anozfr8ZLH3)]
|
||||
- **<big>LinearSearch</big>** : 基于传入的相等函数返回切片中目标值的索引。(线性查找)
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch)]
|
||||
- **<big>LRUCache</big>** : 应用 lru 算法实现内存缓存.
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache)]
|
||||
[[play](https://go.dev/play/p/-EZjgOURufP)]
|
||||
|
||||
### 2. concurrency 包含一些支持并发编程的功能。例如:goroutine, channel, async 等。
|
||||
|
||||
```go
|
||||
import "github.com/duke-git/lancet/v2/concurrency"
|
||||
@@ -123,18 +124,28 @@ import "github.com/duke-git/lancet/v2/concurrency"
|
||||
|
||||
#### Function list:
|
||||
|
||||
- [NewChannel](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#NewChannel)
|
||||
- [Bridge](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Bridge)
|
||||
- [FanIn](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#FanIn)
|
||||
- [Generate](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Generate)
|
||||
- [Or](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Or)
|
||||
- [OrDone](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#OrDone)
|
||||
- [Repeat](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Repeat)
|
||||
- [RepeatFn](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#RepeatFn)
|
||||
- [Take](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Take)
|
||||
- [Tee](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Tee)
|
||||
- **<big>NewChannel</big>** : 返回一个 Channel 指针实例。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#NewChannel)]
|
||||
- **<big>Bridge</big>** : 将多个 channel 链接到一个 channel,直到取消上下文。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/Bridge.md#NewChannel)]
|
||||
- **<big>FanIn</big>** : 将多个 channel 合并为一个 channel,直到取消上下文。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#FanIn)]
|
||||
- **<big>Generate</big>** : 根据传入的值,生成channel。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Generate)]
|
||||
- **<big>Or</big>** : 将一个或多个channel读取到一个channel中,当任何读取channel关闭时将结束读取。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Or)]
|
||||
- **<big>OrDone</big>** : 将一个channel读入另一个channel,直到取消上下文。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#OrDone)]
|
||||
- **<big>Repeat</big>** : 返回一个channel,将参数`values`重复放入channel,直到取消上下文。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Repeat)]
|
||||
- **<big>RepeatFn</big>** : 返回一个channel,重复执行函数fn,并将结果放入返回的channel,直到取消上下文。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#RepeatFn)]
|
||||
- **<big>Take</big>** : 返回一个channel,其值从另一个channel获取,直到取消上下文。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Take)]
|
||||
- **<big>Tee</big>** : 将一个channel分成两个channel,直到取消上下文。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Tee)]
|
||||
|
||||
### 3. condition包含一些用于条件判断的函数。
|
||||
### 3. condition 包含一些用于条件判断的函数。
|
||||
|
||||
```go
|
||||
import "github.com/duke-git/lancet/v2/condition"
|
||||
@@ -175,7 +186,7 @@ import "github.com/duke-git/lancet/v2/convertor"
|
||||
- [EncodeByte](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#EncodeByte)
|
||||
- [DecodeByte](https://github.com/duke-git/lancet/blob/main/docs/convertor_zh-CN.md#DecodeByte)
|
||||
|
||||
### 5. cryptor 加密包支持数据加密和解密,获取md5,hash 值。支持base64, md5, hmac, aes, des, rsa。
|
||||
### 5. cryptor 加密包支持数据加密和解密,获取 md5,hash 值。支持 base64, md5, hmac, aes, des, rsa。
|
||||
|
||||
```go
|
||||
import "github.com/duke-git/lancet/v2/cryptor"
|
||||
@@ -330,7 +341,7 @@ import "github.com/duke-git/lancet/v2/function"
|
||||
- [Pipeline](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Pipeline)
|
||||
- [Watcher](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)
|
||||
|
||||
### 11. maputil包括一些操作map的函数.
|
||||
### 11. maputil 包括一些操作 map 的函数.
|
||||
|
||||
```go
|
||||
import "github.com/duke-git/lancet/v2/maputil"
|
||||
@@ -347,7 +358,7 @@ import "github.com/duke-git/lancet/v2/maputil"
|
||||
- [Values](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#Values)
|
||||
- [IsDisjoint](https://github.com/duke-git/lancet/blob/main/docs/maputil_zh-CN.md#IsDisjoint)
|
||||
|
||||
### 12. mathutil包实现了一些数学计算的函数。
|
||||
### 12. mathutil 包实现了一些数学计算的函数。
|
||||
|
||||
```go
|
||||
import "github.com/duke-git/lancet/v2/mathutil"
|
||||
@@ -368,7 +379,7 @@ import "github.com/duke-git/lancet/v2/mathutil"
|
||||
- [RoundToString](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToString)
|
||||
- [TruncRound](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#TruncRound)
|
||||
|
||||
### 13. netutil网络包支持获取ip地址,发送http请求。
|
||||
### 13. netutil 网络包支持获取 ip 地址,发送 http 请求。
|
||||
|
||||
```go
|
||||
import "github.com/duke-git/lancet/v2/netutil"
|
||||
@@ -397,7 +408,7 @@ import "github.com/duke-git/lancet/v2/netutil"
|
||||
- [HttpPatch<sup>deprecated</sup>](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#HttpPatch)
|
||||
- [ParseHttpResponse](https://github.com/duke-git/lancet/blob/main/docs/netutil_zh-CN.md#ParseHttpResponse)
|
||||
|
||||
### 14. random随机数生成器包,可以生成随机[]bytes, int, string。
|
||||
### 14. random 随机数生成器包,可以生成随机[]bytes, int, string。
|
||||
|
||||
```go
|
||||
import "github.com/duke-git/lancet/v2/random"
|
||||
@@ -414,7 +425,7 @@ import "github.com/duke-git/lancet/v2/random"
|
||||
- [RandNumeralOrLetter](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandNumeralOrLetter)
|
||||
- [UUIdV4](https://github.com/duke-git/lancet/blob/main/docs/random.md#UUIdV4)
|
||||
|
||||
### 15. retry重试执行函数直到函数运行成功或被context cancel。
|
||||
### 15. retry 重试执行函数直到函数运行成功或被 context cancel。
|
||||
|
||||
```go
|
||||
import "github.com/duke-git/lancet/v2/retry"
|
||||
@@ -428,7 +439,7 @@ import "github.com/duke-git/lancet/v2/retry"
|
||||
- [RetryDuration](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryDuration)
|
||||
- [RetryTimes](https://github.com/duke-git/lancet/blob/main/docs/retry_zh-CN.md#RetryTimes)
|
||||
|
||||
### 16. slice包含操作切片的方法集合。
|
||||
### 16. slice 包含操作切片的方法集合。
|
||||
|
||||
```go
|
||||
import "github.com/duke-git/lancet/v2/slice"
|
||||
@@ -488,109 +499,111 @@ import "github.com/duke-git/lancet/v2/slice"
|
||||
- [Without](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#Without)
|
||||
- [KeyBy](https://github.com/duke-git/lancet/blob/main/docs/slice_zh-CN.md#KeyBy)
|
||||
|
||||
### 17. strutil包含字符串处理的相关函数。
|
||||
### 17. strutil 包含字符串处理的相关函数。
|
||||
|
||||
```go
|
||||
import "github.com/duke-git/lancet/v2/strutil"
|
||||
```
|
||||
|
||||
#### 函数列表:
|
||||
- **<big>After</big>** : 返回源字符串中指定字符串首次出现时的位置之后的子字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#After)]
|
||||
[[play](https://go.dev/play/p/RbCOQqCDA7m)]
|
||||
- **<big>AfterLast</big>** : 返回源字符串中指定字符串最后一次出现时的位置之后的子字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#AfterLast)]
|
||||
[[play](https://go.dev/play/p/1TegARrb8Yn)]
|
||||
- **<big>Before</big>** : 返回源字符串中指定字符串第一次出现时的位置之前的子字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Before)]
|
||||
[[play](https://go.dev/play/p/JAWTZDS4F5w)]
|
||||
- **<big>BeforeLast</big>** : 返回源字符串中指定字符串最后一次出现时的位置之前的子字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#BeforeLast)]
|
||||
[[play](https://go.dev/play/p/pJfXXAoG_Te)]
|
||||
- **<big>CamelCase</big>** : 将字符串转换为CamelCase驼峰式字符串, 非字母和数字会被忽略。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#CamelCase)]
|
||||
[[play](https://go.dev/play/p/9eXP3tn2tUy)]
|
||||
- **<big>Capitalize</big>** : 将字符串的第一个字符转换为大写。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Capitalize)]
|
||||
[[play](https://go.dev/play/p/2OAjgbmAqHZ)]
|
||||
- **<big>IsString</big>** : 判断传入参数的数据类型是否为字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#IsString)]
|
||||
[[play](https://go.dev/play/p/IOgq7oF9ERm)]
|
||||
- **<big>KebabCase</big>** : 将字符串转换为kebab-case形式字符串, 非字母和数字会被忽略。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#KebabCase)]
|
||||
[[play](https://go.dev/play/p/dcZM9Oahw-Y)]
|
||||
- **<big>UpperKebabCase</big>** : 将字符串转换为大写KEBAB-CASE形式字符串, 非字母和数字会被忽略。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperKebabCase)]
|
||||
[[play](https://go.dev/play/p/zDyKNneyQXk)]
|
||||
- **<big>LowerFirst</big>** : 将字符串的第一个字符转换为小写形式。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#LowerFirst)]
|
||||
[[play](https://go.dev/play/p/CbzAyZmtJwL)]
|
||||
- **<big>UpperFirst</big>** : 将字符串的第一个字符转换为大写形式。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperFirst)]
|
||||
[[play](https://go.dev/play/p/sBbBxRbs8MM)]
|
||||
- **<big>PadEnd</big>** : 如果字符串短于限制大小,则在右侧用给定字符填充字符串。 如果填充字符超出大小,它们将被截断。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadEnd)]
|
||||
[[play](https://go.dev/play/p/9xP8rN0vz--)]
|
||||
- **<big>PadStart</big>** : 如果字符串短于限制大小,则在左侧用给定字符填充字符串。 如果填充字符超出大小,它们将被截断。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadStart)]
|
||||
[[play](https://go.dev/play/p/xpTfzArDfvT)]
|
||||
- **<big>Reverse</big>** : 返回字符顺序与给定字符串相反的字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Reverse)]
|
||||
[[play](https://go.dev/play/p/adfwalJiecD)]
|
||||
- **<big>SnakeCase</big>** : 将字符串转换为snake_case形式, 非字母和数字会被忽略。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SnakeCase)]
|
||||
[[play](https://go.dev/play/p/tgzQG11qBuN)]
|
||||
- **<big>UpperSnakeCase</big>** : 将字符串转换为大写SNAKE_CASE形式, 非字母和数字会被忽略。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperSnakeCase)]
|
||||
[[play](https://go.dev/play/p/4COPHpnLx38)]
|
||||
- **<big>SplitEx</big>** : 拆分给定的字符串可以控制结果切片是否包含空字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SplitEx)]
|
||||
[[play](https://go.dev/play/p/Us-ySSbWh-3)]
|
||||
- **<big>Substring</big>** : 根据指定的位置和长度截取子字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)]
|
||||
- **<big>Wrap</big>** : 用给定字符包裹传入的字符串
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Wrap)]
|
||||
[[play](https://go.dev/play/p/KoZOlZDDt9y)]
|
||||
- **<big>Unwrap</big>** : 从另一个字符串中解开一个给定的字符串。 将更改源字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Unwrap)]
|
||||
[[play](https://go.dev/play/p/Ec2q4BzCpG-)]
|
||||
|
||||
### 18. system包含os, runtime, shell command的相关函数。
|
||||
- **<big>After</big>** : 返回源字符串中指定字符串首次出现时的位置之后的子字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#After)]
|
||||
[[play](https://go.dev/play/p/RbCOQqCDA7m)]
|
||||
- **<big>AfterLast</big>** : 返回源字符串中指定字符串最后一次出现时的位置之后的子字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#AfterLast)]
|
||||
[[play](https://go.dev/play/p/1TegARrb8Yn)]
|
||||
- **<big>Before</big>** : 返回源字符串中指定字符串第一次出现时的位置之前的子字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Before)]
|
||||
[[play](https://go.dev/play/p/JAWTZDS4F5w)]
|
||||
- **<big>BeforeLast</big>** : 返回源字符串中指定字符串最后一次出现时的位置之前的子字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#BeforeLast)]
|
||||
[[play](https://go.dev/play/p/pJfXXAoG_Te)]
|
||||
- **<big>CamelCase</big>** : 将字符串转换为 CamelCase 驼峰式字符串, 非字母和数字会被忽略。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#CamelCase)]
|
||||
[[play](https://go.dev/play/p/9eXP3tn2tUy)]
|
||||
- **<big>Capitalize</big>** : 将字符串的第一个字符转换为大写。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Capitalize)]
|
||||
[[play](https://go.dev/play/p/2OAjgbmAqHZ)]
|
||||
- **<big>IsString</big>** : 判断传入参数的数据类型是否为字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#IsString)]
|
||||
[[play](https://go.dev/play/p/IOgq7oF9ERm)]
|
||||
- **<big>KebabCase</big>** : 将字符串转换为 kebab-case 形式字符串, 非字母和数字会被忽略。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#KebabCase)]
|
||||
[[play](https://go.dev/play/p/dcZM9Oahw-Y)]
|
||||
- **<big>UpperKebabCase</big>** : 将字符串转换为大写 KEBAB-CASE 形式字符串, 非字母和数字会被忽略。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperKebabCase)]
|
||||
[[play](https://go.dev/play/p/zDyKNneyQXk)]
|
||||
- **<big>LowerFirst</big>** : 将字符串的第一个字符转换为小写形式。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#LowerFirst)]
|
||||
[[play](https://go.dev/play/p/CbzAyZmtJwL)]
|
||||
- **<big>UpperFirst</big>** : 将字符串的第一个字符转换为大写形式。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperFirst)]
|
||||
[[play](https://go.dev/play/p/sBbBxRbs8MM)]
|
||||
- **<big>PadEnd</big>** : 如果字符串短于限制大小,则在右侧用给定字符填充字符串。 如果填充字符超出大小,它们将被截断。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadEnd)]
|
||||
[[play](https://go.dev/play/p/9xP8rN0vz--)]
|
||||
- **<big>PadStart</big>** : 如果字符串短于限制大小,则在左侧用给定字符填充字符串。 如果填充字符超出大小,它们将被截断。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadStart)]
|
||||
[[play](https://go.dev/play/p/xpTfzArDfvT)]
|
||||
- **<big>Reverse</big>** : 返回字符顺序与给定字符串相反的字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Reverse)]
|
||||
[[play](https://go.dev/play/p/adfwalJiecD)]
|
||||
- **<big>SnakeCase</big>** : 将字符串转换为 snake_case 形式, 非字母和数字会被忽略。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SnakeCase)]
|
||||
[[play](https://go.dev/play/p/tgzQG11qBuN)]
|
||||
- **<big>UpperSnakeCase</big>** : 将字符串转换为大写 SNAKE_CASE 形式, 非字母和数字会被忽略。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperSnakeCase)]
|
||||
[[play](https://go.dev/play/p/4COPHpnLx38)]
|
||||
- **<big>SplitEx</big>** : 拆分给定的字符串可以控制结果切片是否包含空字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SplitEx)]
|
||||
[[play](https://go.dev/play/p/Us-ySSbWh-3)]
|
||||
- **<big>Substring</big>** : 根据指定的位置和长度截取子字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)]
|
||||
- **<big>Wrap</big>** : 用给定字符包裹传入的字符串
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Wrap)]
|
||||
[[play](https://go.dev/play/p/KoZOlZDDt9y)]
|
||||
- **<big>Unwrap</big>** : 从另一个字符串中解开一个给定的字符串。 将更改源字符串。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Unwrap)]
|
||||
[[play](https://go.dev/play/p/Ec2q4BzCpG-)]
|
||||
|
||||
### 18. system 包含 os, runtime, shell command 的相关函数。
|
||||
|
||||
```go
|
||||
import "github.com/duke-git/lancet/v2/system"
|
||||
```
|
||||
|
||||
#### 函数列表:
|
||||
- **<big>IsWindows</big>** : 检查当前操作系统是否是windows。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#IsWindows)]
|
||||
[[play](https://go.dev/play/p/XzJULbzmf9m)]
|
||||
- **<big>IsLinux</big>** : 检查当前操作系统是否是linux。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#IsLinux)]
|
||||
[[play](https://go.dev/play/p/zIflQgZNuxD)]
|
||||
- **<big>IsMac</big>** : 检查当前操作系统是否是macos。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#IsMac)]
|
||||
[[play](https://go.dev/play/p/Mg4Hjtyq7Zc)]
|
||||
- **<big>GetOsEnv</big>** : 根据key获取对应的环境变量值
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#GetOsEnv)]
|
||||
[[play](https://go.dev/play/p/D88OYVCyjO-)]
|
||||
- **<big>SetOsEnv</big>** : 设置环境变量。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#SetOsEnv)]
|
||||
[[play](https://go.dev/play/p/D88OYVCyjO-)]
|
||||
- **<big>RemoveOsEnv</big>** : 删除环境变量。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#RemoveOsEnv)]
|
||||
[[play](https://go.dev/play/p/fqyq4b3xUFQ)]
|
||||
- **<big>CompareOsEnv</big>** : 换取环境变量并与传入值进行比较。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#CompareOsEnv)]
|
||||
[[play](https://go.dev/play/p/BciHrKYOHbp)]
|
||||
- **<big>ExecCommand</big>** : 执行shell命令。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#ExecCommand)]
|
||||
[[play](https://go.dev/play/p/n-2fLyZef-4)]
|
||||
- **<big>GetOsBits</big>** : 获取当前操作系统位数(32/64)。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#GetOsBits)]
|
||||
[[play](https://go.dev/play/p/ml-_XH3gJbW)]
|
||||
|
||||
### 19. validator验证器包,包含常用字符串格式验证函数。
|
||||
- **<big>IsWindows</big>** : 检查当前操作系统是否是 windows。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#IsWindows)]
|
||||
[[play](https://go.dev/play/p/XzJULbzmf9m)]
|
||||
- **<big>IsLinux</big>** : 检查当前操作系统是否是 linux。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#IsLinux)]
|
||||
[[play](https://go.dev/play/p/zIflQgZNuxD)]
|
||||
- **<big>IsMac</big>** : 检查当前操作系统是否是 macos。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#IsMac)]
|
||||
[[play](https://go.dev/play/p/Mg4Hjtyq7Zc)]
|
||||
- **<big>GetOsEnv</big>** : 根据 key 获取对应的环境变量值
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#GetOsEnv)]
|
||||
[[play](https://go.dev/play/p/D88OYVCyjO-)]
|
||||
- **<big>SetOsEnv</big>** : 设置环境变量。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#SetOsEnv)]
|
||||
[[play](https://go.dev/play/p/D88OYVCyjO-)]
|
||||
- **<big>RemoveOsEnv</big>** : 删除环境变量。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#RemoveOsEnv)]
|
||||
[[play](https://go.dev/play/p/fqyq4b3xUFQ)]
|
||||
- **<big>CompareOsEnv</big>** : 换取环境变量并与传入值进行比较。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#CompareOsEnv)]
|
||||
[[play](https://go.dev/play/p/BciHrKYOHbp)]
|
||||
- **<big>ExecCommand</big>** : 执行 shell 命令。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#ExecCommand)]
|
||||
[[play](https://go.dev/play/p/n-2fLyZef-4)]
|
||||
- **<big>GetOsBits</big>** : 获取当前操作系统位数(32/64)。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#GetOsBits)]
|
||||
[[play](https://go.dev/play/p/ml-_XH3gJbW)]
|
||||
|
||||
### 19. validator 验证器包,包含常用字符串格式验证函数。
|
||||
|
||||
```go
|
||||
import "github.com/duke-git/lancet/v2/validator"
|
||||
@@ -627,7 +640,7 @@ import "github.com/duke-git/lancet/v2/validator"
|
||||
- [IsZeroValue](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsZeroValue)
|
||||
- [IsGBK](https://github.com/duke-git/lancet/blob/main/docs/validator_zh-CN.md#IsGBK)
|
||||
|
||||
### 20. xerror包实现一些错误处理函数
|
||||
### 20. xerror 包实现一些错误处理函数
|
||||
|
||||
```go
|
||||
import "github.com/duke-git/lancet/v2/xerror"
|
||||
|
||||
@@ -19,7 +19,7 @@ func NewChannel[T any]() *Channel[T] {
|
||||
return &Channel[T]{}
|
||||
}
|
||||
|
||||
// Generate a data of type any channel, put param values into the channel.
|
||||
// Generate creates channel, then put values into the channel.
|
||||
// Play:
|
||||
func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T {
|
||||
dataStream := make(chan T)
|
||||
@@ -39,7 +39,7 @@ func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T {
|
||||
return dataStream
|
||||
}
|
||||
|
||||
// Repeat return a data of type any channel, put param `values` into the channel repeatly until cancel the context.
|
||||
// Repeat create channel, put values into the channel repeatly until cancel the context.
|
||||
// Play:
|
||||
func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T {
|
||||
dataStream := make(chan T)
|
||||
@@ -59,8 +59,8 @@ func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T {
|
||||
return dataStream
|
||||
}
|
||||
|
||||
// RepeatFn return a channel, excutes fn repeatly, and put the result into retruned channel
|
||||
// until close the `done` channel.
|
||||
// RepeatFn create a channel, excutes fn repeatly, and put the result into the channel
|
||||
// until close context.
|
||||
// Play:
|
||||
func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T {
|
||||
dataStream := make(chan T)
|
||||
@@ -78,7 +78,7 @@ func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T {
|
||||
return dataStream
|
||||
}
|
||||
|
||||
// Take return a channel whose values are taken from another channel.
|
||||
// Take create a channel whose values are taken from another channel with limit number.
|
||||
// Play:
|
||||
func (c *Channel[T]) Take(ctx context.Context, valueStream <-chan T, number int) <-chan T {
|
||||
takeStream := make(chan T)
|
||||
@@ -126,7 +126,7 @@ func (c *Channel[T]) FanIn(ctx context.Context, channels ...<-chan T) <-chan T {
|
||||
return out
|
||||
}
|
||||
|
||||
// Tee split one chanel into two channels.
|
||||
// Tee split one chanel into two channels, until cancel the context.
|
||||
// Play:
|
||||
func (c *Channel[T]) Tee(ctx context.Context, in <-chan T) (<-chan T, <-chan T) {
|
||||
out1 := make(chan T)
|
||||
|
||||
@@ -159,7 +159,7 @@ func main() {
|
||||
|
||||
### <span id="Repeat">Repeat</span>
|
||||
|
||||
<p>Return a channel, put param `values` into the channel repeatly until cancel the context.</p>
|
||||
<p>Create channel, put values into the channel repeatly until cancel the context.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
@@ -197,10 +197,47 @@ func main() {
|
||||
|
||||
|
||||
|
||||
### <span id="Generate">Generate</span>
|
||||
|
||||
<p>Creates a channel, then put values into the channel.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T
|
||||
```
|
||||
<b>Example:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/concurrency"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
c := concurrency.NewChannel[int]()
|
||||
intStream := c.Generate(ctx, 1, 2, 3)
|
||||
|
||||
fmt.Println(<-intStream)
|
||||
fmt.Println(<-intStream)
|
||||
fmt.Println(<-intStream)
|
||||
|
||||
// Output:
|
||||
// 1
|
||||
// 2
|
||||
// 3
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="RepeatFn">RepeatFn</span>
|
||||
|
||||
<p>Return a channel, excutes fn repeatly, and put the result into retruned channel until cancel context.</p>
|
||||
<p>Create a channel, excutes fn repeatly, and put the result into the channel, until close context.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
@@ -329,7 +366,7 @@ func main() {
|
||||
|
||||
### <span id="Take">Take</span>
|
||||
|
||||
<p>Return a channel whose values are tahken from another channel until cancel context.</p>
|
||||
<p>Create a channel whose values are taken from another channel with limit number.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
@@ -376,7 +413,7 @@ func main() {
|
||||
|
||||
### <span id="Tee">Tee</span>
|
||||
|
||||
<p>Split one channel into two channels until cancel context.</p>
|
||||
<p>Split one chanel into two channels, until cancel the context.</p>
|
||||
|
||||
<b>Signature:</b>
|
||||
|
||||
|
||||
@@ -157,6 +157,44 @@ func main() {
|
||||
```
|
||||
|
||||
|
||||
### <span id="Generate">Generate</span>
|
||||
|
||||
<p>根据传入的值,生成channel.</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T
|
||||
```
|
||||
<b>例子:</b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/v2/concurrency"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
c := concurrency.NewChannel[int]()
|
||||
intStream := c.Generate(ctx, 1, 2, 3)
|
||||
|
||||
fmt.Println(<-intStream)
|
||||
fmt.Println(<-intStream)
|
||||
fmt.Println(<-intStream)
|
||||
|
||||
// Output:
|
||||
// 1
|
||||
// 2
|
||||
// 3
|
||||
}
|
||||
```
|
||||
|
||||
### <span id="Repeat">Repeat</span>
|
||||
|
||||
<p>返回一个channel,将参数`values`重复放入channel,直到取消上下文。</p>
|
||||
@@ -289,7 +327,7 @@ func main() {
|
||||
|
||||
### <span id="OrDone">OrDone</span>
|
||||
|
||||
<p>将一个channel读入另一个channel,直到取消上下文。.</p>
|
||||
<p>将一个channel读入另一个channel,直到取消上下文。</p>
|
||||
|
||||
<b>函数签名:</b>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user