From a6ba1028c530ec41932eb6c8504c0cf813ead2a1 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Sat, 31 Dec 2022 13:35:44 +0800 Subject: [PATCH] doc: add example and update docment for channel --- README.md | 275 ++++++++++++++++++---------------- README_zh-CN.md | 307 ++++++++++++++++++++------------------ concurrency/channel.go | 12 +- docs/concurrency.md | 45 +++++- docs/concurrency_zh-CN.md | 40 ++++- 5 files changed, 389 insertions(+), 290 deletions(-) diff --git a/README.md b/README.md index 6bd47c1..02c03a0 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ English | [简体中文](./README_zh-CN.md) ### 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 was rewriten with generics of go1.18. +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 was rewriten with generics of go1.18. ```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: -- **BubbleSort** : 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)] -- **CountSort** : 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)] -- **HeapSort** : 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)] -- **InsertionSort** : 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)] -- **MergeSort** : 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)] -- **QuickSort** : 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)] -- **SelectionSort** : 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)] -- **ShellSort** : 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)] -- **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/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/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/algorithm.md#LinearSearch)] -- **LRUCache** : 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)] + +- **BubbleSort** : 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)] +- **CountSort** : 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)] +- **HeapSort** : 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)] +- **InsertionSort** : 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)] +- **MergeSort** : 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)] +- **QuickSort** : 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)] +- **SelectionSort** : 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)] +- **ShellSort** : 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)] +- **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/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/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/algorithm.md#LinearSearch)] +- **LRUCache** : 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) +- **NewChannel** : create a Channel pointer instance. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#NewChannel)] +- **Bridge** : link multiply channels into one channel. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/Bridge.md#NewChannel)] +- **FanIn** : merge multiple channels into one channel. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#FanIn)] +- **Generate** : creates a channel, then put values into the channel. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Generate)] +- **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/concurrency.md#Or)] +- **OrDone** : read a channel into another channel, will close until cancel context. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#OrDone)] +- **Repeat** : 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)] +- **RepeatFn** : 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)] +- **Take** : 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)] +- **Tee** : 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: -- **After** : 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)] -- **AfterLast** : 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)] -- **Before** : 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)] -- **BeforeLast** : 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)] -- **CamelCase** : 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)] -- **Capitalize** : 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)] -- **IsString** : 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)] -- **KebabCase** : 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)] -- **UpperKebabCase** : 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)] -- **LowerFirst** : 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)] -- **UpperFirst** : 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)] -- **PadEnd** : 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--)] -- **PadStart** : 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)] -- **Reverse** : 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)] -- **SnakeCase** : 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)] -- **UpperSnakeCase** : 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)] -- **SplitEx** : 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)] -- **Substring** : 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)] -- **Wrap** : 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)] -- **Unwrap** : 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-)] +- **After** : 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)] +- **AfterLast** : 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)] +- **Before** : 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)] +- **BeforeLast** : 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)] +- **CamelCase** : 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)] +- **Capitalize** : 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)] +- **IsString** : 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)] +- **KebabCase** : 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)] +- **UpperKebabCase** : 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)] +- **LowerFirst** : 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)] +- **UpperFirst** : 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)] +- **PadEnd** : 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--)] +- **PadStart** : 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)] +- **Reverse** : 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)] +- **SnakeCase** : 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)] +- **UpperSnakeCase** : 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)] +- **SplitEx** : 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)] +- **Substring** : 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)] +- **Wrap** : 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)] +- **Unwrap** : 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: -- **IsWindows** : 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)] -- **IsLinux** : 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)] -- **IsMac** : 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)] -- **GetOsEnv** : 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-)] -- **SetOsEnv** : 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-)] -- **RemoveOsEnv** : 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)] -- **CompareOsEnv** : 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)] -- **ExecCommand** : 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)] -- **GetOsBits** : 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)] + +- **IsWindows** : 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)] +- **IsLinux** : 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)] +- **IsMac** : 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)] +- **GetOsEnv** : 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-)] +- **SetOsEnv** : 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-)] +- **RemoveOsEnv** : 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)] +- **CompareOsEnv** : 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)] +- **ExecCommand** : 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)] +- **GetOsBits** : 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. diff --git a/README_zh-CN.md b/README_zh-CN.md index 66ccc74..fa57ec1 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -31,13 +31,13 @@ ### Note: -1. 使用go1.18及以上版本的用户,建议安装v2.x.x。 因为v2.x.x应用go1.18的泛型重写了大部分函数。 +1. 使用 go1.18 及以上版本的用户,建议安装 v2.x.x。 因为 v2.x.x 应用 go1.18 的泛型重写了大部分函数。 ```go go get github.com/duke-git/lancet/v2 //安装v2最新版本v2.x.x ``` -2. 使用go1.18以下版本的用户,必须安装v1.x.x。目前最新的v1版本是v1.3.5。 +2. 使用 go1.18 以下版本的用户,必须安装 v1.x.x。目前最新的 v1 版本是 v1.3.5。 ```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: -- **BubbleSort** : 使用冒泡排序算法对切片进行排序。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BubbleSort)] -[[play](https://go.dev/play/p/GNdv7Jg2Taj)] -- **CountSort** : 使用计数排序算法对切片进行排序。不改变原数据。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#CountSort)] -[[play](https://go.dev/play/p/tB-Umgm0DrP)] -- **HeapSort** : 使用堆排序算法对切片进行排序。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#HeapSort)] -[[play](https://go.dev/play/p/u6Iwa1VZS_f)] -- **InsertionSort** : 使用插入排序算法对切片进行排序。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#InsertionSort)] -[[play](https://go.dev/play/p/G5LJiWgJJW6)] -- **MergeSort** : 使用合并排序算法对切片进行排序。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#MergeSort)] -[[play](https://go.dev/play/p/ydinn9YzUJn)] -- **QuickSort** : 使用快速排序算法对切片进行排序。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#QuickSort)] -[[play](https://go.dev/play/p/7Y7c1Elk3ax)] -- **SelectionSort** : 使用选择排序算法对切片进行排序。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#SelectionSort)] -[[play](https://go.dev/play/p/oXovbkekayS)] -- **ShellSort** : 使用希尔排序算法对切片进行排序。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#ShellSort)] -[[play](https://go.dev/play/p/3ibkszpJEu3)] -- **BinarySearch** : 返回排序切片中目标值的索引,使用二分搜索(递归调用)。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BinarySearch)] -[[play](https://go.dev/play/p/t6MeGiUSN47)] -- **BinaryIterativeSearch** :返回排序切片中目标值的索引,使用二分搜索(非递归)。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BinaryIterativeSearch)] -[[play](https://go.dev/play/p/Anozfr8ZLH3)] -- **LinearSearch** : 基于传入的相等函数返回切片中目标值的索引。(线性查找) -[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch)] -- **LRUCache** : 应用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 等。 +- **BubbleSort** : 使用冒泡排序算法对切片进行排序。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BubbleSort)] + [[play](https://go.dev/play/p/GNdv7Jg2Taj)] +- **CountSort** : 使用计数排序算法对切片进行排序。不改变原数据。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#CountSort)] + [[play](https://go.dev/play/p/tB-Umgm0DrP)] +- **HeapSort** : 使用堆排序算法对切片进行排序。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#HeapSort)] + [[play](https://go.dev/play/p/u6Iwa1VZS_f)] +- **InsertionSort** : 使用插入排序算法对切片进行排序。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#InsertionSort)] + [[play](https://go.dev/play/p/G5LJiWgJJW6)] +- **MergeSort** : 使用合并排序算法对切片进行排序。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#MergeSort)] + [[play](https://go.dev/play/p/ydinn9YzUJn)] +- **QuickSort** : 使用快速排序算法对切片进行排序。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#QuickSort)] + [[play](https://go.dev/play/p/7Y7c1Elk3ax)] +- **SelectionSort** : 使用选择排序算法对切片进行排序。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#SelectionSort)] + [[play](https://go.dev/play/p/oXovbkekayS)] +- **ShellSort** : 使用希尔排序算法对切片进行排序。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#ShellSort)] + [[play](https://go.dev/play/p/3ibkszpJEu3)] +- **BinarySearch** : 返回排序切片中目标值的索引,使用二分搜索(递归调用)。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BinarySearch)] + [[play](https://go.dev/play/p/t6MeGiUSN47)] +- **BinaryIterativeSearch** :返回排序切片中目标值的索引,使用二分搜索(非递归)。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#BinaryIterativeSearch)] + [[play](https://go.dev/play/p/Anozfr8ZLH3)] +- **LinearSearch** : 基于传入的相等函数返回切片中目标值的索引。(线性查找) + [[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch)] +- **LRUCache** : 应用 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) +- **NewChannel** : 返回一个 Channel 指针实例。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#NewChannel)] +- **Bridge** : 将多个 channel 链接到一个 channel,直到取消上下文。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/Bridge.md#NewChannel)] +- **FanIn** : 将多个 channel 合并为一个 channel,直到取消上下文。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#FanIn)] +- **Generate** : 根据传入的值,生成channel。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Generate)] +- **Or** : 将一个或多个channel读取到一个channel中,当任何读取channel关闭时将结束读取。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Or)] +- **OrDone** : 将一个channel读入另一个channel,直到取消上下文。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#OrDone)] +- **Repeat** : 返回一个channel,将参数`values`重复放入channel,直到取消上下文。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Repeat)] +- **RepeatFn** : 返回一个channel,重复执行函数fn,并将结果放入返回的channel,直到取消上下文。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#RepeatFn)] +- **Take** : 返回一个channel,其值从另一个channel获取,直到取消上下文。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Take)] +- **Tee** : 将一个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" - [HttpPatchdeprecated](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" ``` #### 函数列表: -- **After** : 返回源字符串中指定字符串首次出现时的位置之后的子字符串。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#After)] -[[play](https://go.dev/play/p/RbCOQqCDA7m)] -- **AfterLast** : 返回源字符串中指定字符串最后一次出现时的位置之后的子字符串。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#AfterLast)] -[[play](https://go.dev/play/p/1TegARrb8Yn)] -- **Before** : 返回源字符串中指定字符串第一次出现时的位置之前的子字符串。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Before)] -[[play](https://go.dev/play/p/JAWTZDS4F5w)] -- **BeforeLast** : 返回源字符串中指定字符串最后一次出现时的位置之前的子字符串。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#BeforeLast)] -[[play](https://go.dev/play/p/pJfXXAoG_Te)] -- **CamelCase** : 将字符串转换为CamelCase驼峰式字符串, 非字母和数字会被忽略。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#CamelCase)] -[[play](https://go.dev/play/p/9eXP3tn2tUy)] -- **Capitalize** : 将字符串的第一个字符转换为大写。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Capitalize)] -[[play](https://go.dev/play/p/2OAjgbmAqHZ)] -- **IsString** : 判断传入参数的数据类型是否为字符串。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#IsString)] -[[play](https://go.dev/play/p/IOgq7oF9ERm)] -- **KebabCase** : 将字符串转换为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)] -- **UpperKebabCase** : 将字符串转换为大写KEBAB-CASE形式字符串, 非字母和数字会被忽略。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperKebabCase)] -[[play](https://go.dev/play/p/zDyKNneyQXk)] -- **LowerFirst** : 将字符串的第一个字符转换为小写形式。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#LowerFirst)] -[[play](https://go.dev/play/p/CbzAyZmtJwL)] -- **UpperFirst** : 将字符串的第一个字符转换为大写形式。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperFirst)] -[[play](https://go.dev/play/p/sBbBxRbs8MM)] -- **PadEnd** : 如果字符串短于限制大小,则在右侧用给定字符填充字符串。 如果填充字符超出大小,它们将被截断。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadEnd)] -[[play](https://go.dev/play/p/9xP8rN0vz--)] -- **PadStart** : 如果字符串短于限制大小,则在左侧用给定字符填充字符串。 如果填充字符超出大小,它们将被截断。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadStart)] -[[play](https://go.dev/play/p/xpTfzArDfvT)] -- **Reverse** : 返回字符顺序与给定字符串相反的字符串。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Reverse)] -[[play](https://go.dev/play/p/adfwalJiecD)] -- **SnakeCase** : 将字符串转换为snake_case形式, 非字母和数字会被忽略。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SnakeCase)] -[[play](https://go.dev/play/p/tgzQG11qBuN)] -- **UpperSnakeCase** : 将字符串转换为大写SNAKE_CASE形式, 非字母和数字会被忽略。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperSnakeCase)] -[[play](https://go.dev/play/p/4COPHpnLx38)] -- **SplitEx** : 拆分给定的字符串可以控制结果切片是否包含空字符串。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SplitEx)] -[[play](https://go.dev/play/p/Us-ySSbWh-3)] -- **Substring** : 根据指定的位置和长度截取子字符串。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)] -- **Wrap** : 用给定字符包裹传入的字符串 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Wrap)] -[[play](https://go.dev/play/p/KoZOlZDDt9y)] -- **Unwrap** : 从另一个字符串中解开一个给定的字符串。 将更改源字符串。 -[[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的相关函数。 +- **After** : 返回源字符串中指定字符串首次出现时的位置之后的子字符串。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#After)] + [[play](https://go.dev/play/p/RbCOQqCDA7m)] +- **AfterLast** : 返回源字符串中指定字符串最后一次出现时的位置之后的子字符串。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#AfterLast)] + [[play](https://go.dev/play/p/1TegARrb8Yn)] +- **Before** : 返回源字符串中指定字符串第一次出现时的位置之前的子字符串。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Before)] + [[play](https://go.dev/play/p/JAWTZDS4F5w)] +- **BeforeLast** : 返回源字符串中指定字符串最后一次出现时的位置之前的子字符串。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#BeforeLast)] + [[play](https://go.dev/play/p/pJfXXAoG_Te)] +- **CamelCase** : 将字符串转换为 CamelCase 驼峰式字符串, 非字母和数字会被忽略。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#CamelCase)] + [[play](https://go.dev/play/p/9eXP3tn2tUy)] +- **Capitalize** : 将字符串的第一个字符转换为大写。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Capitalize)] + [[play](https://go.dev/play/p/2OAjgbmAqHZ)] +- **IsString** : 判断传入参数的数据类型是否为字符串。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#IsString)] + [[play](https://go.dev/play/p/IOgq7oF9ERm)] +- **KebabCase** : 将字符串转换为 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)] +- **UpperKebabCase** : 将字符串转换为大写 KEBAB-CASE 形式字符串, 非字母和数字会被忽略。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperKebabCase)] + [[play](https://go.dev/play/p/zDyKNneyQXk)] +- **LowerFirst** : 将字符串的第一个字符转换为小写形式。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#LowerFirst)] + [[play](https://go.dev/play/p/CbzAyZmtJwL)] +- **UpperFirst** : 将字符串的第一个字符转换为大写形式。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperFirst)] + [[play](https://go.dev/play/p/sBbBxRbs8MM)] +- **PadEnd** : 如果字符串短于限制大小,则在右侧用给定字符填充字符串。 如果填充字符超出大小,它们将被截断。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadEnd)] + [[play](https://go.dev/play/p/9xP8rN0vz--)] +- **PadStart** : 如果字符串短于限制大小,则在左侧用给定字符填充字符串。 如果填充字符超出大小,它们将被截断。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#PadStart)] + [[play](https://go.dev/play/p/xpTfzArDfvT)] +- **Reverse** : 返回字符顺序与给定字符串相反的字符串。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Reverse)] + [[play](https://go.dev/play/p/adfwalJiecD)] +- **SnakeCase** : 将字符串转换为 snake_case 形式, 非字母和数字会被忽略。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SnakeCase)] + [[play](https://go.dev/play/p/tgzQG11qBuN)] +- **UpperSnakeCase** : 将字符串转换为大写 SNAKE_CASE 形式, 非字母和数字会被忽略。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#UpperSnakeCase)] + [[play](https://go.dev/play/p/4COPHpnLx38)] +- **SplitEx** : 拆分给定的字符串可以控制结果切片是否包含空字符串。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#SplitEx)] + [[play](https://go.dev/play/p/Us-ySSbWh-3)] +- **Substring** : 根据指定的位置和长度截取子字符串。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)] +- **Wrap** : 用给定字符包裹传入的字符串 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Wrap)] + [[play](https://go.dev/play/p/KoZOlZDDt9y)] +- **Unwrap** : 从另一个字符串中解开一个给定的字符串。 将更改源字符串。 + [[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" ``` #### 函数列表: -- **IsWindows** : 检查当前操作系统是否是windows。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#IsWindows)] -[[play](https://go.dev/play/p/XzJULbzmf9m)] -- **IsLinux** : 检查当前操作系统是否是linux。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#IsLinux)] -[[play](https://go.dev/play/p/zIflQgZNuxD)] -- **IsMac** : 检查当前操作系统是否是macos。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#IsMac)] -[[play](https://go.dev/play/p/Mg4Hjtyq7Zc)] -- **GetOsEnv** : 根据key获取对应的环境变量值 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#GetOsEnv)] -[[play](https://go.dev/play/p/D88OYVCyjO-)] -- **SetOsEnv** : 设置环境变量。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#SetOsEnv)] -[[play](https://go.dev/play/p/D88OYVCyjO-)] -- **RemoveOsEnv** : 删除环境变量。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#RemoveOsEnv)] -[[play](https://go.dev/play/p/fqyq4b3xUFQ)] -- **CompareOsEnv** : 换取环境变量并与传入值进行比较。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#CompareOsEnv)] -[[play](https://go.dev/play/p/BciHrKYOHbp)] -- **ExecCommand** : 执行shell命令。 -[[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#ExecCommand)] -[[play](https://go.dev/play/p/n-2fLyZef-4)] -- **GetOsBits** : 获取当前操作系统位数(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验证器包,包含常用字符串格式验证函数。 +- **IsWindows** : 检查当前操作系统是否是 windows。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#IsWindows)] + [[play](https://go.dev/play/p/XzJULbzmf9m)] +- **IsLinux** : 检查当前操作系统是否是 linux。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#IsLinux)] + [[play](https://go.dev/play/p/zIflQgZNuxD)] +- **IsMac** : 检查当前操作系统是否是 macos。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#IsMac)] + [[play](https://go.dev/play/p/Mg4Hjtyq7Zc)] +- **GetOsEnv** : 根据 key 获取对应的环境变量值 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#GetOsEnv)] + [[play](https://go.dev/play/p/D88OYVCyjO-)] +- **SetOsEnv** : 设置环境变量。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#SetOsEnv)] + [[play](https://go.dev/play/p/D88OYVCyjO-)] +- **RemoveOsEnv** : 删除环境变量。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#RemoveOsEnv)] + [[play](https://go.dev/play/p/fqyq4b3xUFQ)] +- **CompareOsEnv** : 换取环境变量并与传入值进行比较。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#CompareOsEnv)] + [[play](https://go.dev/play/p/BciHrKYOHbp)] +- **ExecCommand** : 执行 shell 命令。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/system_zh-CN#ExecCommand)] + [[play](https://go.dev/play/p/n-2fLyZef-4)] +- **GetOsBits** : 获取当前操作系统位数(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" diff --git a/concurrency/channel.go b/concurrency/channel.go index c803f12..c235018 100644 --- a/concurrency/channel.go +++ b/concurrency/channel.go @@ -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) diff --git a/docs/concurrency.md b/docs/concurrency.md index 724c9e1..73acc61 100644 --- a/docs/concurrency.md +++ b/docs/concurrency.md @@ -159,7 +159,7 @@ func main() { ### Repeat -

Return a channel, put param `values` into the channel repeatly until cancel the context.

+

Create channel, put values into the channel repeatly until cancel the context.

Signature: @@ -197,10 +197,47 @@ func main() { +### Generate + +

Creates a channel, then put values into the channel.

+ +Signature: + +```go +func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T +``` +Example: + +```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 +} +``` ### RepeatFn -

Return a channel, excutes fn repeatly, and put the result into retruned channel until cancel context.

+

Create a channel, excutes fn repeatly, and put the result into the channel, until close context.

Signature: @@ -329,7 +366,7 @@ func main() { ### Take -

Return a channel whose values are tahken from another channel until cancel context.

+

Create a channel whose values are taken from another channel with limit number.

Signature: @@ -376,7 +413,7 @@ func main() { ### Tee -

Split one channel into two channels until cancel context.

+

Split one chanel into two channels, until cancel the context.

Signature: diff --git a/docs/concurrency_zh-CN.md b/docs/concurrency_zh-CN.md index 941d40e..7b9caa9 100644 --- a/docs/concurrency_zh-CN.md +++ b/docs/concurrency_zh-CN.md @@ -157,6 +157,44 @@ func main() { ``` +### Generate + +

根据传入的值,生成channel.

+ +函数签名: + +```go +func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T +``` +例子: + +```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 +} +``` + ### Repeat

返回一个channel,将参数`values`重复放入channel,直到取消上下文。

@@ -289,7 +327,7 @@ func main() { ### OrDone -

将一个channel读入另一个channel,直到取消上下文。.

+

将一个channel读入另一个channel,直到取消上下文。

函数签名: