mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-14 01:32:27 +08:00
Compare commits
6 Commits
v2.1.13
...
c260ce493d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c260ce493d | ||
|
|
9ffe96d3ef | ||
|
|
d4bba76dc8 | ||
|
|
adaa3ebc43 | ||
|
|
61d38ae3b8 | ||
|
|
c85d910044 |
19
README.md
19
README.md
@@ -1,5 +1,5 @@
|
|||||||
<div align=center>
|
<div align=center>
|
||||||
<a href="https://uvdream.github.io/lancet-docs/en/"><img src="./logo.png" width="200" height="200"/></a>
|
<img src="./logo.png" width="200" height="200"/>
|
||||||
|
|
||||||
<br/>
|
<br/>
|
||||||
|
|
||||||
@@ -113,6 +113,7 @@ import "github.com/duke-git/lancet/v2/algorithm"
|
|||||||
[[play](https://go.dev/play/p/Anozfr8ZLH3)]
|
[[play](https://go.dev/play/p/Anozfr8ZLH3)]
|
||||||
- **<big>LinearSearch</big>** : returns the index of target in slice base on equal function.
|
- **<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)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LinearSearch)]
|
||||||
|
[[play](https://go.dev/play/p/IsS7rgn5s3x)]
|
||||||
- **<big>LRUCache</big>** : implements memory cache with lru algorithm.
|
- **<big>LRUCache</big>** : implements memory cache with lru algorithm.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LRUCache)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm.md#LRUCache)]
|
||||||
[[play](https://go.dev/play/p/-EZjgOURufP)]
|
[[play](https://go.dev/play/p/-EZjgOURufP)]
|
||||||
@@ -127,24 +128,34 @@ import "github.com/duke-git/lancet/v2/concurrency"
|
|||||||
|
|
||||||
- **<big>NewChannel</big>** : create a Channel pointer instance.
|
- **<big>NewChannel</big>** : create a Channel pointer instance.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#NewChannel)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#NewChannel)]
|
||||||
|
[[play](https://go.dev/play/p/7aB4KyMMp9A)]
|
||||||
- **<big>Bridge</big>** : link multiply channels into one channel.
|
- **<big>Bridge</big>** : link multiply channels into one channel.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/Bridge.md#NewChannel)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/Bridge.md#NewChannel)]
|
||||||
|
[[play](https://go.dev/play/p/qmWSy1NVF-Y)]
|
||||||
- **<big>FanIn</big>** : merge multiple channels into one channel.
|
- **<big>FanIn</big>** : merge multiple channels into one channel.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#FanIn)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#FanIn)]
|
||||||
|
[[play](https://go.dev/play/p/2VYFMexEvTm)]
|
||||||
- **<big>Generate</big>** : creates a channel, then put values into the channel.
|
- **<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)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Generate)]
|
||||||
|
[[play](https://go.dev/play/p/7aB4KyMMp9A)]
|
||||||
- **<big>Or</big>** : read one or more channels into one channel, will close when any readin channel is closed.
|
- **<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)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Or)]
|
||||||
|
[[play](https://go.dev/play/p/Wqz9rwioPww)]
|
||||||
- **<big>OrDone</big>** : read a channel into another channel, will close until cancel context.
|
- **<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)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#OrDone)]
|
||||||
|
[[play](https://go.dev/play/p/lm_GoS6aDjo)]
|
||||||
- **<big>Repeat</big>** : create channel, put values into the channel repeatly until cancel the context.
|
- **<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)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Repeat)]
|
||||||
|
[[play](https://go.dev/play/p/k5N_ALVmYjE)]
|
||||||
- **<big>RepeatFn</big>** : create a channel, excutes fn repeatly, and put the result into the channel, until close context.
|
- **<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)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#RepeatFn)]
|
||||||
|
[[play](https://go.dev/play/p/4J1zAWttP85)]
|
||||||
- **<big>Take</big>** : create a channel whose values are taken from another channel with limit number.
|
- **<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)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Take)]
|
||||||
|
[[play](https://go.dev/play/p/9Utt-1pDr2J)]
|
||||||
- **<big>Tee</big>** : split one chanel into two channels, until cancel the context.
|
- **<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)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency.md#Tee)]
|
||||||
|
[[play](https://go.dev/play/p/3TQPKnCirrP)]
|
||||||
|
|
||||||
### 3. Condition package contains some functions for conditional judgment. eg. And, Or, TernaryOperator...
|
### 3. Condition package contains some functions for conditional judgment. eg. And, Or, TernaryOperator...
|
||||||
|
|
||||||
@@ -556,8 +567,10 @@ import "github.com/duke-git/lancet/v2/function"
|
|||||||
[[play](https://go.dev/play/p/0HqUDIFZ3IL)]
|
[[play](https://go.dev/play/p/0HqUDIFZ3IL)]
|
||||||
- **<big>CurryFn</big>** : make a curry function.
|
- **<big>CurryFn</big>** : make a curry function.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#CurryFn)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#CurryFn)]
|
||||||
|
[[play](https://go.dev/play/p/5HopfDwANKX)]
|
||||||
- **<big>Compose</big>** : compose the functions from right to left.
|
- **<big>Compose</big>** : compose the functions from right to left.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Compose)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Compose)]
|
||||||
|
[[play](https://go.dev/play/p/KKfugD4PKYF)]
|
||||||
- **<big>Delay</big>** : call the function after delayed time.
|
- **<big>Delay</big>** : call the function after delayed time.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Delay)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Delay)]
|
||||||
[[play](https://go.dev/play/p/Ivtc2ZE-Tye)]
|
[[play](https://go.dev/play/p/Ivtc2ZE-Tye)]
|
||||||
@@ -572,6 +585,8 @@ import "github.com/duke-git/lancet/v2/function"
|
|||||||
[[play](https://go.dev/play/p/mPdUVvj6HD6)]
|
[[play](https://go.dev/play/p/mPdUVvj6HD6)]
|
||||||
- **<big>Watcher</big>** : Watcher is used for record code excution time. can start/stop/reset the watch timer. get the elapsed time of function execution.
|
- **<big>Watcher</big>** : Watcher is used for record code excution time. can start/stop/reset the watch timer. get the elapsed time of function execution.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function.md#Watcher)]
|
||||||
|
[[play](https://go.dev/play/p/l2yrOpCLd1I)]
|
||||||
|
|
||||||
|
|
||||||
### 11. Maputil package includes some functions to manipulate map.
|
### 11. Maputil package includes some functions to manipulate map.
|
||||||
|
|
||||||
@@ -640,6 +655,7 @@ import "github.com/duke-git/lancet/v2/mathutil"
|
|||||||
[[play](https://go.dev/play/p/N9qgYg_Ho6f)]
|
[[play](https://go.dev/play/p/N9qgYg_Ho6f)]
|
||||||
- **<big>Percent</big>** : calculate the percentage of value to total.
|
- **<big>Percent</big>** : calculate the percentage of value to total.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Percent)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#Percent)]
|
||||||
|
[[play](https://go.dev/play/p/QQM9B13coSP)]
|
||||||
- **<big>RoundToFloat</big>** : round up to n decimal places for float64.
|
- **<big>RoundToFloat</big>** : round up to n decimal places for float64.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToFloat)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil.md#RoundToFloat)]
|
||||||
[[play](https://go.dev/play/p/ghyb528JRJL)]
|
[[play](https://go.dev/play/p/ghyb528JRJL)]
|
||||||
@@ -1000,6 +1016,7 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
[[play](https://go.dev/play/p/Us-ySSbWh-3)]
|
[[play](https://go.dev/play/p/Us-ySSbWh-3)]
|
||||||
- **<big>Substring</big>** : returns a substring of the specific length starting at the specific offset position.
|
- **<big>Substring</big>** : returns a substring of the specific length starting at the specific offset position.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)]
|
||||||
|
[[play](https://go.dev/play/p/q3sM6ehnPDp)]
|
||||||
- **<big>Wrap</big>** : wrap a string with given string.
|
- **<big>Wrap</big>** : wrap a string with given string.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap)]
|
||||||
[[play](https://go.dev/play/p/KoZOlZDDt9y)]
|
[[play](https://go.dev/play/p/KoZOlZDDt9y)]
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<div align=center>
|
<div align=center>
|
||||||
<a href="https://uvdream.github.io/lancet-docs/"><img src="./logo.png" width="200" height="200"/><a/>
|
<img src="./logo.png" width="200" height="200"/>
|
||||||
|
|
||||||
<br/>
|
<br/>
|
||||||
|
|
||||||
@@ -112,6 +112,7 @@ import "github.com/duke-git/lancet/v2/algorithm"
|
|||||||
[[play](https://go.dev/play/p/Anozfr8ZLH3)]
|
[[play](https://go.dev/play/p/Anozfr8ZLH3)]
|
||||||
- **<big>LinearSearch</big>** : 基于传入的相等函数返回切片中目标值的索引。(线性查找)
|
- **<big>LinearSearch</big>** : 基于传入的相等函数返回切片中目标值的索引。(线性查找)
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LinearSearch)]
|
||||||
|
[[play](https://go.dev/play/p/IsS7rgn5s3x)]
|
||||||
- **<big>LRUCache</big>** : 应用 lru 算法实现内存缓存.
|
- **<big>LRUCache</big>** : 应用 lru 算法实现内存缓存.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/algorithm_zh-CN.md#LRUCache)]
|
||||||
[[play](https://go.dev/play/p/-EZjgOURufP)]
|
[[play](https://go.dev/play/p/-EZjgOURufP)]
|
||||||
@@ -126,24 +127,34 @@ import "github.com/duke-git/lancet/v2/concurrency"
|
|||||||
|
|
||||||
- **<big>NewChannel</big>** : 返回一个 Channel 指针实例。
|
- **<big>NewChannel</big>** : 返回一个 Channel 指针实例。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#NewChannel)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#NewChannel)]
|
||||||
|
[[play](https://go.dev/play/p/7aB4KyMMp9A)]
|
||||||
- **<big>Bridge</big>** : 将多个 channel 链接到一个 channel,直到取消上下文。
|
- **<big>Bridge</big>** : 将多个 channel 链接到一个 channel,直到取消上下文。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/Bridge.md#NewChannel)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/Bridge.md#NewChannel)]
|
||||||
|
[[play](https://go.dev/play/p/qmWSy1NVF-Y)]
|
||||||
- **<big>FanIn</big>** : 将多个 channel 合并为一个 channel,直到取消上下文。
|
- **<big>FanIn</big>** : 将多个 channel 合并为一个 channel,直到取消上下文。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#FanIn)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#FanIn)]
|
||||||
|
[[play](https://go.dev/play/p/2VYFMexEvTm)]
|
||||||
- **<big>Generate</big>** : 根据传入的值,生成 channel。
|
- **<big>Generate</big>** : 根据传入的值,生成 channel。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Generate)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Generate)]
|
||||||
|
[[play](https://go.dev/play/p/7aB4KyMMp9A)]
|
||||||
- **<big>Or</big>** : 将一个或多个 channel 读取到一个 channel 中,当任何读取 channel 关闭时将结束读取。
|
- **<big>Or</big>** : 将一个或多个 channel 读取到一个 channel 中,当任何读取 channel 关闭时将结束读取。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Or)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Or)]
|
||||||
|
[[play](https://go.dev/play/p/Wqz9rwioPww)]
|
||||||
- **<big>OrDone</big>** : 将一个 channel 读入另一个 channel,直到取消上下文。
|
- **<big>OrDone</big>** : 将一个 channel 读入另一个 channel,直到取消上下文。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#OrDone)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#OrDone)]
|
||||||
|
[[play](https://go.dev/play/p/lm_GoS6aDjo)]
|
||||||
- **<big>Repeat</big>** : 返回一个 channel,将参数`values`重复放入 channel,直到取消上下文。
|
- **<big>Repeat</big>** : 返回一个 channel,将参数`values`重复放入 channel,直到取消上下文。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Repeat)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Repeat)]
|
||||||
|
[[play](https://go.dev/play/p/k5N_ALVmYjE)]
|
||||||
- **<big>RepeatFn</big>** : 返回一个 channel,重复执行函数 fn,并将结果放入返回的 channel,直到取消上下文。
|
- **<big>RepeatFn</big>** : 返回一个 channel,重复执行函数 fn,并将结果放入返回的 channel,直到取消上下文。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#RepeatFn)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#RepeatFn)]
|
||||||
|
[[play](https://go.dev/play/p/4J1zAWttP85)]
|
||||||
- **<big>Take</big>** : 返回一个 channel,其值从另一个 channel 获取,直到取消上下文。
|
- **<big>Take</big>** : 返回一个 channel,其值从另一个 channel 获取,直到取消上下文。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Take)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Take)]
|
||||||
|
[[play](https://go.dev/play/p/9Utt-1pDr2J)]
|
||||||
- **<big>Tee</big>** : 将一个 channel 分成两个 channel,直到取消上下文。
|
- **<big>Tee</big>** : 将一个 channel 分成两个 channel,直到取消上下文。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Tee)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/concurrency_zh-CN.md#Tee)]
|
||||||
|
[[play](https://go.dev/play/p/3TQPKnCirrP)]
|
||||||
|
|
||||||
### 3. condition 包含一些用于条件判断的函数。
|
### 3. condition 包含一些用于条件判断的函数。
|
||||||
|
|
||||||
@@ -559,8 +570,10 @@ import "github.com/duke-git/lancet/v2/function"
|
|||||||
[[play](https://go.dev/play/p/0HqUDIFZ3IL)]
|
[[play](https://go.dev/play/p/0HqUDIFZ3IL)]
|
||||||
- **<big>CurryFn</big>** : 创建柯里化函数。
|
- **<big>CurryFn</big>** : 创建柯里化函数。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#CurryFn)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#CurryFn)]
|
||||||
|
[[play](https://go.dev/play/p/5HopfDwANKX)]
|
||||||
- **<big>Compose</big>** : 从右至左组合函数列表fnList,返回组合后的函数。
|
- **<big>Compose</big>** : 从右至左组合函数列表fnList,返回组合后的函数。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Compose)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Compose)]
|
||||||
|
[[play](https://go.dev/play/p/KKfugD4PKYF)]
|
||||||
- **<big>Delay</big>** : 延迟delay时间后调用函数。
|
- **<big>Delay</big>** : 延迟delay时间后调用函数。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Delay)]
|
||||||
[[play](https://go.dev/play/p/Ivtc2ZE-Tye)]
|
[[play](https://go.dev/play/p/Ivtc2ZE-Tye)]
|
||||||
@@ -575,6 +588,7 @@ import "github.com/duke-git/lancet/v2/function"
|
|||||||
[[play](https://go.dev/play/p/mPdUVvj6HD6)]
|
[[play](https://go.dev/play/p/mPdUVvj6HD6)]
|
||||||
- **<big>Watcher</big>** : Watcher用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。
|
- **<big>Watcher</big>** : Watcher用于记录代码执行时间。可以启动/停止/重置手表定时器。获取函数执行的时间。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/function_zh-CN.md#Watcher)]
|
||||||
|
[[play](https://go.dev/play/p/l2yrOpCLd1I)]
|
||||||
|
|
||||||
|
|
||||||
### 11. maputil 包括一些操作 map 的函数.
|
### 11. maputil 包括一些操作 map 的函数.
|
||||||
@@ -644,6 +658,7 @@ import "github.com/duke-git/lancet/v2/mathutil"
|
|||||||
[[play](https://go.dev/play/p/N9qgYg_Ho6f)]
|
[[play](https://go.dev/play/p/N9qgYg_Ho6f)]
|
||||||
- **<big>Percent</big>** : 计算百分比,可以指定保留 n 位小数。
|
- **<big>Percent</big>** : 计算百分比,可以指定保留 n 位小数。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Percent)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#Percent)]
|
||||||
|
[[play](https://go.dev/play/p/QQM9B13coSP)]
|
||||||
- **<big>RoundToFloat</big>** : 四舍五入,保留 n 位小数,返回 float64。
|
- **<big>RoundToFloat</big>** : 四舍五入,保留 n 位小数,返回 float64。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToFloat)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/mathutil_zh-CN.md#RoundToFloat)]
|
||||||
[[play](https://go.dev/play/p/ghyb528JRJL)]
|
[[play](https://go.dev/play/p/ghyb528JRJL)]
|
||||||
@@ -1010,6 +1025,7 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
[[play](https://go.dev/play/p/Us-ySSbWh-3)]
|
[[play](https://go.dev/play/p/Us-ySSbWh-3)]
|
||||||
- **<big>Substring</big>** : 根据指定的位置和长度截取子字符串。
|
- **<big>Substring</big>** : 根据指定的位置和长度截取子字符串。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)]
|
||||||
|
[[play](https://go.dev/play/p/q3sM6ehnPDp)]
|
||||||
- **<big>Wrap</big>** : 用给定字符包裹传入的字符串
|
- **<big>Wrap</big>** : 用给定字符包裹传入的字符串
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Wrap)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil_zh-CN.md#Wrap)]
|
||||||
[[play](https://go.dev/play/p/KoZOlZDDt9y)]
|
[[play](https://go.dev/play/p/KoZOlZDDt9y)]
|
||||||
@@ -1160,7 +1176,6 @@ import "github.com/duke-git/lancet/v2/xerror"
|
|||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#Unwrap)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#Unwrap)]
|
||||||
[[play](https://go.dev/play/p/w84d7Mb3Afk)]
|
[[play](https://go.dev/play/p/w84d7Mb3Afk)]
|
||||||
|
|
||||||
- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/xerror_zh-CN.md#Unwrap)
|
|
||||||
|
|
||||||
## 如何贡献代码
|
## 如何贡献代码
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import "github.com/duke-git/lancet/v2/lancetconstraints"
|
|||||||
|
|
||||||
// LinearSearch return the index of target in slice base on equal function.
|
// LinearSearch return the index of target in slice base on equal function.
|
||||||
// If not found return -1
|
// If not found return -1
|
||||||
// Play: Todo
|
// Play: https://go.dev/play/p/IsS7rgn5s3x
|
||||||
func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int {
|
func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int {
|
||||||
for i, v := range slice {
|
for i, v := range slice {
|
||||||
if equal(v, target) {
|
if equal(v, target) {
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ func NewChannel[T any]() *Channel[T] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Generate creates channel, then put values into the channel.
|
// Generate creates channel, then put values into the channel.
|
||||||
// Play: Todo
|
// Play: https://go.dev/play/p/7aB4KyMMp9A
|
||||||
func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T {
|
func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T {
|
||||||
dataStream := make(chan T)
|
dataStream := make(chan T)
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Repeat create channel, put values into the channel repeatly until cancel the context.
|
// Repeat create channel, put values into the channel repeatly until cancel the context.
|
||||||
// Play: Todo
|
// Play: https://go.dev/play/p/k5N_ALVmYjE
|
||||||
func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T {
|
func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T {
|
||||||
dataStream := make(chan T)
|
dataStream := make(chan T)
|
||||||
|
|
||||||
@@ -61,7 +61,7 @@ func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T {
|
|||||||
|
|
||||||
// RepeatFn create a channel, excutes fn repeatly, and put the result into the channel
|
// RepeatFn create a channel, excutes fn repeatly, and put the result into the channel
|
||||||
// until close context.
|
// until close context.
|
||||||
// Play: Todo
|
// Play: https://go.dev/play/p/4J1zAWttP85
|
||||||
func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T {
|
func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T {
|
||||||
dataStream := make(chan T)
|
dataStream := make(chan T)
|
||||||
|
|
||||||
@@ -79,7 +79,7 @@ func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Take create a channel whose values are taken from another channel with limit number.
|
// Take create a channel whose values are taken from another channel with limit number.
|
||||||
// Play: Todo
|
// Play: https://go.dev/play/p/9Utt-1pDr2J
|
||||||
func (c *Channel[T]) Take(ctx context.Context, valueStream <-chan T, number int) <-chan T {
|
func (c *Channel[T]) Take(ctx context.Context, valueStream <-chan T, number int) <-chan T {
|
||||||
takeStream := make(chan T)
|
takeStream := make(chan T)
|
||||||
|
|
||||||
@@ -99,7 +99,7 @@ func (c *Channel[T]) Take(ctx context.Context, valueStream <-chan T, number int)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FanIn merge multiple channels into one channel.
|
// FanIn merge multiple channels into one channel.
|
||||||
// Play: Todo
|
// Play: https://go.dev/play/p/2VYFMexEvTm
|
||||||
func (c *Channel[T]) FanIn(ctx context.Context, channels ...<-chan T) <-chan T {
|
func (c *Channel[T]) FanIn(ctx context.Context, channels ...<-chan T) <-chan T {
|
||||||
out := make(chan T)
|
out := make(chan T)
|
||||||
|
|
||||||
@@ -127,7 +127,7 @@ func (c *Channel[T]) FanIn(ctx context.Context, channels ...<-chan T) <-chan T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Tee split one chanel into two channels, until cancel the context.
|
// Tee split one chanel into two channels, until cancel the context.
|
||||||
// Play: Todo
|
// Play: https://go.dev/play/p/3TQPKnCirrP
|
||||||
func (c *Channel[T]) Tee(ctx context.Context, in <-chan T) (<-chan T, <-chan T) {
|
func (c *Channel[T]) Tee(ctx context.Context, in <-chan T) (<-chan T, <-chan T) {
|
||||||
out1 := make(chan T)
|
out1 := make(chan T)
|
||||||
out2 := make(chan T)
|
out2 := make(chan T)
|
||||||
@@ -154,7 +154,7 @@ func (c *Channel[T]) Tee(ctx context.Context, in <-chan T) (<-chan T, <-chan T)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Bridge link multiply channels into one channel.
|
// Bridge link multiply channels into one channel.
|
||||||
// Play: Todo
|
// Play: https://go.dev/play/p/qmWSy1NVF-Y
|
||||||
func (c *Channel[T]) Bridge(ctx context.Context, chanStream <-chan <-chan T) <-chan T {
|
func (c *Channel[T]) Bridge(ctx context.Context, chanStream <-chan <-chan T) <-chan T {
|
||||||
valStream := make(chan T)
|
valStream := make(chan T)
|
||||||
|
|
||||||
@@ -186,7 +186,7 @@ func (c *Channel[T]) Bridge(ctx context.Context, chanStream <-chan <-chan T) <-c
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Or read one or more channels into one channel, will close when any readin channel is closed.
|
// Or read one or more channels into one channel, will close when any readin channel is closed.
|
||||||
// Play: Todo
|
// Play: https://go.dev/play/p/Wqz9rwioPww
|
||||||
func (c *Channel[T]) Or(channels ...<-chan T) <-chan T {
|
func (c *Channel[T]) Or(channels ...<-chan T) <-chan T {
|
||||||
switch len(channels) {
|
switch len(channels) {
|
||||||
case 0:
|
case 0:
|
||||||
@@ -220,7 +220,7 @@ func (c *Channel[T]) Or(channels ...<-chan T) <-chan T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// OrDone read a channel into another channel, will close until cancel context.
|
// OrDone read a channel into another channel, will close until cancel context.
|
||||||
// Play: Todo
|
// Play: https://go.dev/play/p/lm_GoS6aDjo
|
||||||
func (c *Channel[T]) OrDone(ctx context.Context, channel <-chan T) <-chan T {
|
func (c *Channel[T]) OrDone(ctx context.Context, channel <-chan T) <-chan T {
|
||||||
valStream := make(chan T)
|
valStream := make(chan T)
|
||||||
|
|
||||||
|
|||||||
@@ -40,8 +40,6 @@ import (
|
|||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BubbleSort">BubbleSort</span>
|
### <span id="BubbleSort">BubbleSort</span>
|
||||||
<p>Sort slice with bubble sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
<p>Sort slice with bubble sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
@@ -60,33 +58,34 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type intComparator struct{}
|
||||||
type intComparator struct{}
|
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
val1, _ := v1.(int)
|
val1, _ := v1.(int)
|
||||||
val2, _ := v2.(int)
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
//ascending order
|
//ascending order
|
||||||
if val1 < val2 {
|
if val1 < val2 {
|
||||||
return -1
|
return -1
|
||||||
} else if val1 > val2 {
|
} else if val1 > val2 {
|
||||||
return 1
|
return 1
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
func main() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.BubbleSort(intSlice, comparator)
|
|
||||||
|
|
||||||
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
algorithm.BubbleSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="InsertionSort">InsertionSort</span>
|
### <span id="InsertionSort">InsertionSort</span>
|
||||||
<p>Sort slice with insertion sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
<p>Sort slice with insertion sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
@@ -105,53 +104,50 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type people struct {
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
}
|
||||||
|
|
||||||
|
// PeopleAageComparator sort people slice by age field
|
||||||
|
type peopleAgeComparator struct{}
|
||||||
|
|
||||||
|
// Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator
|
||||||
|
func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int {
|
||||||
|
p1, _ := v1.(people)
|
||||||
|
p2, _ := v2.(people)
|
||||||
|
|
||||||
|
//ascending order
|
||||||
|
if p1.Age < p2.Age {
|
||||||
|
return -1
|
||||||
|
} else if p1.Age > p2.Age {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
type people struct {
|
peoples := []people{
|
||||||
Name string
|
|
||||||
Age int
|
|
||||||
}
|
|
||||||
|
|
||||||
// PeopleAageComparator sort people slice by age field
|
|
||||||
type peopleAgeComparator struct{}
|
|
||||||
|
|
||||||
// Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator
|
|
||||||
func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int {
|
|
||||||
p1, _ := v1.(people)
|
|
||||||
p2, _ := v2.(people)
|
|
||||||
|
|
||||||
//ascending order
|
|
||||||
if p1.Age < p2.Age {
|
|
||||||
return -1
|
|
||||||
} else if p1.Age > p2.Age {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
|
|
||||||
//decending order
|
|
||||||
// if p1.Age > p2.Age {
|
|
||||||
// return -1
|
|
||||||
// } else if p1.Age < p2.Age {
|
|
||||||
// return 1
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
var peoples = []people{
|
|
||||||
{Name: "a", Age: 20},
|
{Name: "a", Age: 20},
|
||||||
{Name: "b", Age: 10},
|
{Name: "b", Age: 10},
|
||||||
{Name: "c", Age: 17},
|
{Name: "c", Age: 17},
|
||||||
{Name: "d", Age: 8},
|
{Name: "d", Age: 8},
|
||||||
{Name: "e", Age: 28},
|
{Name: "e", Age: 28},
|
||||||
}
|
}
|
||||||
|
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
|
|
||||||
algorithm.InsertionSort(peoples, comparator)
|
algorithm.InsertionSort(peoples, comparator)
|
||||||
|
|
||||||
fmt.Println(peoples) //[{d 8} {b 10} {c 17} {a 20} {e 28}]
|
fmt.Println(peoples)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
//[{d 8} {b 10} {c 17} {a 20} {e 28}]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SelectionSort">SelectionSort</span>
|
### <span id="SelectionSort">SelectionSort</span>
|
||||||
<p>Sort slice with selection sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
<p>Sort slice with selection sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
@@ -170,33 +166,34 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type intComparator struct{}
|
||||||
type intComparator struct{}
|
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
val1, _ := v1.(int)
|
val1, _ := v1.(int)
|
||||||
val2, _ := v2.(int)
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
//ascending order
|
//ascending order
|
||||||
if val1 < val2 {
|
if val1 < val2 {
|
||||||
return -1
|
return -1
|
||||||
} else if val1 > val2 {
|
} else if val1 > val2 {
|
||||||
return 1
|
return 1
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
func main() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.SelectionSort(intSlice, comparator)
|
|
||||||
|
|
||||||
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
algorithm.SelectionSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ShellSort">ShellSort</span>
|
### <span id="ShellSort">ShellSort</span>
|
||||||
<p>Sort slice with shell sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
<p>Sort slice with shell sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
@@ -215,33 +212,34 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type intComparator struct{}
|
||||||
type intComparator struct{}
|
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
val1, _ := v1.(int)
|
val1, _ := v1.(int)
|
||||||
val2, _ := v2.(int)
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
//ascending order
|
//ascending order
|
||||||
if val1 < val2 {
|
if val1 < val2 {
|
||||||
return -1
|
return -1
|
||||||
} else if val1 > val2 {
|
} else if val1 > val2 {
|
||||||
return 1
|
return 1
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
func main() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.ShellSort(intSlice, comparator)
|
|
||||||
|
|
||||||
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
algorithm.ShellSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="QuickSort">QuickSort</span>
|
### <span id="QuickSort">QuickSort</span>
|
||||||
<p>Sort slice with quick sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
<p>Sort slice with quick sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
@@ -260,33 +258,34 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type intComparator struct{}
|
||||||
type intComparator struct{}
|
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
val1, _ := v1.(int)
|
val1, _ := v1.(int)
|
||||||
val2, _ := v2.(int)
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
//ascending order
|
//ascending order
|
||||||
if val1 < val2 {
|
if val1 < val2 {
|
||||||
return -1
|
return -1
|
||||||
} else if val1 > val2 {
|
} else if val1 > val2 {
|
||||||
return 1
|
return 1
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
func main() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.QuickSort(intSlice, comparator)
|
|
||||||
|
|
||||||
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
algorithm.QuickSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HeapSort">HeapSort</span>
|
### <span id="HeapSort">HeapSort</span>
|
||||||
<p>Sort slice with heap sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
<p>Sort slice with heap sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
@@ -305,33 +304,34 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type intComparator struct{}
|
||||||
type intComparator struct{}
|
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
val1, _ := v1.(int)
|
val1, _ := v1.(int)
|
||||||
val2, _ := v2.(int)
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
//ascending order
|
//ascending order
|
||||||
if val1 < val2 {
|
if val1 < val2 {
|
||||||
return -1
|
return -1
|
||||||
} else if val1 > val2 {
|
} else if val1 > val2 {
|
||||||
return 1
|
return 1
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
func main() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.HeapSort(intSlice, comparator)
|
|
||||||
|
|
||||||
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
algorithm.HeapSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="MergeSort">MergeSort</span>
|
### <span id="MergeSort">MergeSort</span>
|
||||||
<p>Sort slice with merge sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
<p>Sort slice with merge sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
@@ -350,32 +350,34 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type intComparator struct{}
|
||||||
type intComparator struct{}
|
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
val1, _ := v1.(int)
|
val1, _ := v1.(int)
|
||||||
val2, _ := v2.(int)
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
//ascending order
|
//ascending order
|
||||||
if val1 < val2 {
|
if val1 < val2 {
|
||||||
return -1
|
return -1
|
||||||
} else if val1 > val2 {
|
} else if val1 > val2 {
|
||||||
return 1
|
return 1
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
func main() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.MergeSort(intSlice, comparator)
|
|
||||||
|
|
||||||
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
algorithm.MergeSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CountSort">CountSort</span>
|
### <span id="CountSort">CountSort</span>
|
||||||
<p>Sort slice with count sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
<p>Sort slice with count sort algorithm. Param comparator should implements lancetconstraints.Comparator.</p>
|
||||||
|
|
||||||
@@ -394,33 +396,35 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
type intComparator struct{}
|
||||||
val1, _ := v1.(int)
|
|
||||||
val2, _ := v2.(int)
|
|
||||||
|
|
||||||
//ascending order
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
if val1 < val2 {
|
val1, _ := v1.(int)
|
||||||
return -1
|
val2, _ := v2.(int)
|
||||||
} else if val1 > val2 {
|
|
||||||
return 1
|
//ascending order
|
||||||
}
|
if val1 < val2 {
|
||||||
return 0
|
return -1
|
||||||
|
} else if val1 > val2 {
|
||||||
|
return 1
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
func main() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.CountSort(intSlice, comparator)
|
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
sortedNums := algorithm.CountSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(sortedNums)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BinarySearch">BinarySearch</span>
|
### <span id="BinarySearch">BinarySearch</span>
|
||||||
<p>BinarySearch search for target within a sorted slice, recursive call itself. If a target is found, the index of the target is returned. Else the function return -1.</p>
|
<p>BinarySearch search for target within a sorted slice, recursive call itself. If a target is found, the index of the target is returned. Else the function return -1.</p>
|
||||||
|
|
||||||
@@ -439,34 +443,37 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type intComparator struct{}
|
||||||
type intComparator struct{}
|
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
val1, _ := v1.(int)
|
val1, _ := v1.(int)
|
||||||
val2, _ := v2.(int)
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
//ascending order
|
//ascending order
|
||||||
if val1 < val2 {
|
if val1 < val2 {
|
||||||
return -1
|
return -1
|
||||||
} else if val1 > val2 {
|
} else if val1 > val2 {
|
||||||
return 1
|
return 1
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8}
|
func main() {
|
||||||
comparator := &intComparator{}
|
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8}
|
||||||
foundIndex := algorithm.BinarySearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)
|
comparator := &intComparator{}
|
||||||
fmt.Println(foundIndex) //4
|
|
||||||
|
|
||||||
notFoundIndex := algorithm.BinarySearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator)
|
result1 := algorithm.BinarySearch(numbers, 5, 0, len(numbers)-1, comparator)
|
||||||
fmt.Println(notFoundIndex) //-1
|
result2 := algorithm.BinarySearch(numbers, 9, 0, len(numbers)-1, comparator)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 4
|
||||||
|
// -1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BinaryIterativeSearch">BinaryIterativeSearch</span>
|
### <span id="BinaryIterativeSearch">BinaryIterativeSearch</span>
|
||||||
<p>BinaryIterativeSearch search for target within a sorted slice, recursive call itself. If a target is found, the index of the target is returned. Else the function return -1.</p>
|
<p>BinaryIterativeSearch search for target within a sorted slice, recursive call itself. If a target is found, the index of the target is returned. Else the function return -1.</p>
|
||||||
|
|
||||||
@@ -485,42 +492,44 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type intComparator struct{}
|
||||||
type intComparator struct{}
|
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
val1, _ := v1.(int)
|
val1, _ := v1.(int)
|
||||||
val2, _ := v2.(int)
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
//ascending order
|
//ascending order
|
||||||
if val1 < val2 {
|
if val1 < val2 {
|
||||||
return -1
|
return -1
|
||||||
} else if val1 > val2 {
|
} else if val1 > val2 {
|
||||||
return 1
|
return 1
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8}
|
func main() {
|
||||||
comparator := &intComparator{}
|
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8}
|
||||||
foundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)
|
comparator := &intComparator{}
|
||||||
fmt.Println(foundIndex) //4
|
|
||||||
|
|
||||||
notFoundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator)
|
result1 := algorithm.BinaryIterativeSearch(numbers, 5, 0, len(numbers)-1, comparator)
|
||||||
fmt.Println(notFoundIndex) //-1
|
result2 := algorithm.BinaryIterativeSearch(numbers, 9, 0, len(numbers)-1, comparator)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 4
|
||||||
|
// -1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="LinearSearch">LinearSearch</span>
|
### <span id="LinearSearch">LinearSearch</span>
|
||||||
<p>LinearSearch Simple linear search algorithm that iterates over all elements of an slice. If a target is found, the index of the target is returned. Else the function return -1.</p>
|
<p>return the index of target in slice base on equal function.If a target is found, the index of the target is returned. Else the function return -1.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func LinearSearch[T any](slice []T, target T, comparator lancetconstraints.Comparator) int
|
func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -533,34 +542,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
type intComparator struct{}
|
numbers := []int{3, 4, 5, 3, 2, 1}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
equalFunc := func(a, b int) bool {
|
||||||
val1, _ := v1.(int)
|
return a == b
|
||||||
val2, _ := v2.(int)
|
}
|
||||||
|
|
||||||
//ascending order
|
result1 := algorithm.LinearSearch(numbers, 3, equalFunc)
|
||||||
if val1 < val2 {
|
result2 := algorithm.LinearSearch(numbers, 6, equalFunc)
|
||||||
return -1
|
|
||||||
} else if val1 > val2 {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
fmt.Println(result1)
|
||||||
comparator := &intComparator{}
|
fmt.Println(result2)
|
||||||
foundIndex := algorithm.LinearSearch(intSlice, 5, comparator)
|
|
||||||
fmt.Println(foundIndex) //2
|
|
||||||
|
|
||||||
notFoundIndex := algorithm.LinearSearch(sortedNumbers, 0, comparator)
|
// Output:
|
||||||
fmt.Println(notFoundIndex) //-1
|
// 0
|
||||||
|
// -1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="LRUCache">LRUCache</span>
|
### <span id="LRUCache">LRUCache</span>
|
||||||
<p>LRUCache implements mem cache with lru.</p>
|
<p>LRUCache implements mem cache with lru.</p>
|
||||||
|
|
||||||
@@ -586,16 +585,28 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
cache := algorithm.NewLRUCache[int, int](2)
|
cache := algorithm.NewLRUCache[int, int](2)
|
||||||
|
|
||||||
cache.Put(1, 1)
|
cache.Put(1, 1)
|
||||||
cache.Put(2, 2)
|
cache.Put(2, 2)
|
||||||
cache.Put(3, 3)
|
|
||||||
|
|
||||||
fmt.Println(cache.Len()) // 3
|
result1, ok1 := cache.Get(1)
|
||||||
|
result2, ok2 := cache.Get(2)
|
||||||
|
result3, ok3 := cache.Get(3)
|
||||||
|
|
||||||
v, ok := cache.Get(1)
|
fmt.Println(result1, ok1)
|
||||||
fmt.Println(v, ok) // 1 true
|
fmt.Println(result2, ok2)
|
||||||
|
fmt.Println(result3, ok3)
|
||||||
|
|
||||||
ok = cache.Delete(1)
|
fmt.Println(cache.Len())
|
||||||
fmt.Println(ok) // true
|
|
||||||
|
ok := cache.Delete(2)
|
||||||
|
fmt.Println(ok)
|
||||||
|
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1 true
|
||||||
|
// 2 true
|
||||||
|
// 0 false
|
||||||
|
// 2
|
||||||
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -40,17 +40,15 @@ import (
|
|||||||
|
|
||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BubbleSort">BubbleSort</span>
|
### <span id="BubbleSort">BubbleSort</span>
|
||||||
<p>冒泡排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>冒泡排序,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
func BubbleSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -60,42 +58,43 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type intComparator struct{}
|
||||||
type intComparator struct{}
|
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
val1, _ := v1.(int)
|
val1, _ := v1.(int)
|
||||||
val2, _ := v2.(int)
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
//ascending order
|
//ascending order
|
||||||
if val1 < val2 {
|
if val1 < val2 {
|
||||||
return -1
|
return -1
|
||||||
} else if val1 > val2 {
|
} else if val1 > val2 {
|
||||||
return 1
|
return 1
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
func main() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.BubbleSort(intSlice, comparator)
|
|
||||||
|
|
||||||
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
algorithm.BubbleSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="InsertionSort">InsertionSort</span>
|
### <span id="InsertionSort">InsertionSort</span>
|
||||||
<p>插入排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>插入排序,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
func InsertionSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -105,62 +104,59 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type people struct {
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
}
|
||||||
|
|
||||||
|
// PeopleAageComparator sort people slice by age field
|
||||||
|
type peopleAgeComparator struct{}
|
||||||
|
|
||||||
|
// Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator
|
||||||
|
func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int {
|
||||||
|
p1, _ := v1.(people)
|
||||||
|
p2, _ := v2.(people)
|
||||||
|
|
||||||
|
//ascending order
|
||||||
|
if p1.Age < p2.Age {
|
||||||
|
return -1
|
||||||
|
} else if p1.Age > p2.Age {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
type people struct {
|
peoples := []people{
|
||||||
Name string
|
|
||||||
Age int
|
|
||||||
}
|
|
||||||
|
|
||||||
// PeopleAageComparator sort people slice by age field
|
|
||||||
type peopleAgeComparator struct{}
|
|
||||||
|
|
||||||
// Compare implements github.com/duke-git/lancet/lancetconstraints/constraints.go/Comparator
|
|
||||||
func (pc *peopleAgeComparator) Compare(v1 any, v2 any) int {
|
|
||||||
p1, _ := v1.(people)
|
|
||||||
p2, _ := v2.(people)
|
|
||||||
|
|
||||||
//ascending order
|
|
||||||
if p1.Age < p2.Age {
|
|
||||||
return -1
|
|
||||||
} else if p1.Age > p2.Age {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
|
|
||||||
//decending order
|
|
||||||
// if p1.Age > p2.Age {
|
|
||||||
// return -1
|
|
||||||
// } else if p1.Age < p2.Age {
|
|
||||||
// return 1
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
|
|
||||||
var peoples = []people{
|
|
||||||
{Name: "a", Age: 20},
|
{Name: "a", Age: 20},
|
||||||
{Name: "b", Age: 10},
|
{Name: "b", Age: 10},
|
||||||
{Name: "c", Age: 17},
|
{Name: "c", Age: 17},
|
||||||
{Name: "d", Age: 8},
|
{Name: "d", Age: 8},
|
||||||
{Name: "e", Age: 28},
|
{Name: "e", Age: 28},
|
||||||
}
|
}
|
||||||
|
|
||||||
comparator := &peopleAgeComparator{}
|
comparator := &peopleAgeComparator{}
|
||||||
|
|
||||||
algorithm.InsertionSort(peoples, comparator)
|
algorithm.InsertionSort(peoples, comparator)
|
||||||
|
|
||||||
fmt.Println(intSlice) //[{d 8} {b 10} {c 17} {a 20} {e 28}]
|
fmt.Println(peoples)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
//[{d 8} {b 10} {c 17} {a 20} {e 28}]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SelectionSort">SelectionSort</span>
|
### <span id="SelectionSort">SelectionSort</span>
|
||||||
<p>选择排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>选择排序,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
func SelectionSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -170,42 +166,43 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type intComparator struct{}
|
||||||
type intComparator struct{}
|
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
val1, _ := v1.(int)
|
val1, _ := v1.(int)
|
||||||
val2, _ := v2.(int)
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
//ascending order
|
//ascending order
|
||||||
if val1 < val2 {
|
if val1 < val2 {
|
||||||
return -1
|
return -1
|
||||||
} else if val1 > val2 {
|
} else if val1 > val2 {
|
||||||
return 1
|
return 1
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
func main() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.SelectionSort(intSlice, comparator)
|
|
||||||
|
|
||||||
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
algorithm.SelectionSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ShellSort">ShellSort</span>
|
### <span id="ShellSort">ShellSort</span>
|
||||||
<p>希尔排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>希尔排序,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
func ShellSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -215,42 +212,43 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type intComparator struct{}
|
||||||
type intComparator struct{}
|
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
val1, _ := v1.(int)
|
val1, _ := v1.(int)
|
||||||
val2, _ := v2.(int)
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
//ascending order
|
//ascending order
|
||||||
if val1 < val2 {
|
if val1 < val2 {
|
||||||
return -1
|
return -1
|
||||||
} else if val1 > val2 {
|
} else if val1 > val2 {
|
||||||
return 1
|
return 1
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
func main() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.ShellSort(intSlice, comparator)
|
|
||||||
|
|
||||||
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
algorithm.ShellSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="QuickSort">QuickSort</span>
|
### <span id="QuickSort">QuickSort</span>
|
||||||
<p>快速排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>快速排序,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func QuickSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func QuickSort[T any](slice []T comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -260,42 +258,43 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type intComparator struct{}
|
||||||
type intComparator struct{}
|
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
val1, _ := v1.(int)
|
val1, _ := v1.(int)
|
||||||
val2, _ := v2.(int)
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
//ascending order
|
//ascending order
|
||||||
if val1 < val2 {
|
if val1 < val2 {
|
||||||
return -1
|
return -1
|
||||||
} else if val1 > val2 {
|
} else if val1 > val2 {
|
||||||
return 1
|
return 1
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
func main() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.QuickSort(intSlice, comparator)
|
|
||||||
|
|
||||||
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
algorithm.QuickSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HeapSort">HeapSort</span>
|
### <span id="HeapSort">HeapSort</span>
|
||||||
<p>堆排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>堆排序,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func HeapSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -305,42 +304,43 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type intComparator struct{}
|
||||||
type intComparator struct{}
|
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
val1, _ := v1.(int)
|
val1, _ := v1.(int)
|
||||||
val2, _ := v2.(int)
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
//ascending order
|
//ascending order
|
||||||
if val1 < val2 {
|
if val1 < val2 {
|
||||||
return -1
|
return -1
|
||||||
} else if val1 > val2 {
|
} else if val1 > val2 {
|
||||||
return 1
|
return 1
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
func main() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.HeapSort(intSlice, comparator)
|
|
||||||
|
|
||||||
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
algorithm.HeapSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="MergeSort">MergeSort</span>
|
### <span id="MergeSort">MergeSort</span>
|
||||||
<p>归并排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>归并排序,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
func MergeSort[T any](slice []T, comparator lancetconstraints.Comparator)
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -350,41 +350,43 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type intComparator struct{}
|
||||||
type intComparator struct{}
|
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
val1, _ := v1.(int)
|
val1, _ := v1.(int)
|
||||||
val2, _ := v2.(int)
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
//ascending order
|
//ascending order
|
||||||
if val1 < val2 {
|
if val1 < val2 {
|
||||||
return -1
|
return -1
|
||||||
} else if val1 > val2 {
|
} else if val1 > val2 {
|
||||||
return 1
|
return 1
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
func main() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
algorithm.MergeSort(intSlice, comparator)
|
|
||||||
|
|
||||||
fmt.Println(intSlice) //[]int{1, 2, 3, 4, 5, 6}
|
algorithm.MergeSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(numbers)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CountSort">CountSort</span>
|
### <span id="CountSort">CountSort</span>
|
||||||
<p>计数排序,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>计数排序,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
func CountSort[T any](slice []T, comparator lancetconstraints.Comparator) []T
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -394,42 +396,44 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
|
||||||
type intComparator struct{}
|
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
type intComparator struct{}
|
||||||
val1, _ := v1.(int)
|
|
||||||
val2, _ := v2.(int)
|
|
||||||
|
|
||||||
//ascending order
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
if val1 < val2 {
|
val1, _ := v1.(int)
|
||||||
return -1
|
val2, _ := v2.(int)
|
||||||
} else if val1 > val2 {
|
|
||||||
return 1
|
//ascending order
|
||||||
}
|
if val1 < val2 {
|
||||||
return 0
|
return -1
|
||||||
|
} else if val1 > val2 {
|
||||||
|
return 1
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
func main() {
|
||||||
|
numbers := []int{2, 1, 5, 3, 6, 4}
|
||||||
comparator := &intComparator{}
|
comparator := &intComparator{}
|
||||||
sortedSlice := algorithm.CountSort(intSlice, comparator)
|
|
||||||
|
|
||||||
fmt.Println(sortedSlice) //[]int{1, 2, 3, 4, 5, 6}
|
sortedNums := algorithm.CountSort(numbers, comparator)
|
||||||
|
|
||||||
|
fmt.Println(sortedNums)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [1 2 3 4 5 6]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BinarySearch">BinarySearch</span>
|
### <span id="BinarySearch">BinarySearch</span>
|
||||||
<p>二分递归查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>二分递归查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int
|
func BinarySearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -439,43 +443,46 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type intComparator struct{}
|
||||||
type intComparator struct{}
|
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
val1, _ := v1.(int)
|
val1, _ := v1.(int)
|
||||||
val2, _ := v2.(int)
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
//ascending order
|
//ascending order
|
||||||
if val1 < val2 {
|
if val1 < val2 {
|
||||||
return -1
|
return -1
|
||||||
} else if val1 > val2 {
|
} else if val1 > val2 {
|
||||||
return 1
|
return 1
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8}
|
func main() {
|
||||||
comparator := &intComparator{}
|
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8}
|
||||||
foundIndex := algorithm.BinarySearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)
|
comparator := &intComparator{}
|
||||||
fmt.Println(foundIndex) //4
|
|
||||||
|
|
||||||
notFoundIndex := algorithm.BinarySearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator)
|
result1 := algorithm.BinarySearch(numbers, 5, 0, len(numbers)-1, comparator)
|
||||||
fmt.Println(notFoundIndex) //-1
|
result2 := algorithm.BinarySearch(numbers, 9, 0, len(numbers)-1, comparator)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 4
|
||||||
|
// -1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BinaryIterativeSearch">BinaryIterativeSearch</span>
|
### <span id="BinaryIterativeSearch">BinaryIterativeSearch</span>
|
||||||
<p>二分迭代查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>二分迭代查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int
|
func BinaryIterativeSearch[T any](sortedSlice []T, target T, lowIndex, highIndex int, comparator lancetconstraints.Comparator) int
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -485,44 +492,46 @@ import (
|
|||||||
"github.com/duke-git/lancet/v2/algorithm"
|
"github.com/duke-git/lancet/v2/algorithm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type intComparator struct{}
|
||||||
type intComparator struct{}
|
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
func (c *intComparator) Compare(v1 any, v2 any) int {
|
||||||
val1, _ := v1.(int)
|
val1, _ := v1.(int)
|
||||||
val2, _ := v2.(int)
|
val2, _ := v2.(int)
|
||||||
|
|
||||||
//ascending order
|
//ascending order
|
||||||
if val1 < val2 {
|
if val1 < val2 {
|
||||||
return -1
|
return -1
|
||||||
} else if val1 > val2 {
|
} else if val1 > val2 {
|
||||||
return 1
|
return 1
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
var sortedNumbers = []int{1, 2, 3, 4, 5, 6, 7, 8}
|
func main() {
|
||||||
comparator := &intComparator{}
|
numbers := []int{1, 2, 3, 4, 5, 6, 7, 8}
|
||||||
foundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 5, 0, len(sortedNumbers)-1, comparator)
|
comparator := &intComparator{}
|
||||||
fmt.Println(foundIndex) //4
|
|
||||||
|
|
||||||
notFoundIndex := algorithm.BinaryIterativeSearch(sortedNumbers, 9, 0, len(sortedNumbers)-1, comparator)
|
result1 := algorithm.BinaryIterativeSearch(numbers, 5, 0, len(numbers)-1, comparator)
|
||||||
fmt.Println(notFoundIndex) //-1
|
result2 := algorithm.BinaryIterativeSearch(numbers, 9, 0, len(numbers)-1, comparator)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 4
|
||||||
|
// -1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="LinearSearch">LinearSearch</span>
|
### <span id="LinearSearch">LinearSearch</span>
|
||||||
<p>线性查找,返回元素索引,未找到元素返回-1,参数comparator需要实现包lancetconstraints.Comparator</p>
|
<p>基于传入的相等函数线性查找元素,返回元素索引,未找到元素返回-1。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func LinearSearch[T any](slice []T, target T, comparator lancetconstraints.Comparator) int
|
func LinearSearch[T any](slice []T, target T, equal func(a, b T) bool) int
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -533,36 +542,26 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
type intComparator struct{}
|
numbers := []int{3, 4, 5, 3, 2, 1}
|
||||||
|
|
||||||
func (c *intComparator) Compare(v1 any, v2 any) int {
|
equalFunc := func(a, b int) bool {
|
||||||
val1, _ := v1.(int)
|
return a == b
|
||||||
val2, _ := v2.(int)
|
}
|
||||||
|
|
||||||
//ascending order
|
result1 := algorithm.LinearSearch(numbers, 3, equalFunc)
|
||||||
if val1 < val2 {
|
result2 := algorithm.LinearSearch(numbers, 6, equalFunc)
|
||||||
return -1
|
|
||||||
} else if val1 > val2 {
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
intSlice := []int{2, 1, 5, 3, 6, 4}
|
fmt.Println(result1)
|
||||||
comparator := &intComparator{}
|
fmt.Println(result2)
|
||||||
foundIndex := algorithm.LinearSearch(intSlice, 5, comparator)
|
|
||||||
fmt.Println(foundIndex) //2
|
|
||||||
|
|
||||||
notFoundIndex := algorithm.LinearSearch(sortedNumbers, 0, comparator)
|
// Output:
|
||||||
fmt.Println(notFoundIndex) //-1
|
// 0
|
||||||
|
// -1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="LRUCache">LRUCache</span>
|
### <span id="LRUCache">LRUCache</span>
|
||||||
<p>lru实现缓存</p>
|
<p>lru算法实现缓存。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -573,7 +572,7 @@ func (l *LRUCache[K, V]) Put(key K, value V)
|
|||||||
func (l *LRUCache[K, V]) Delete(key K) bool
|
func (l *LRUCache[K, V]) Delete(key K) bool
|
||||||
func (l *LRUCache[K, V]) Len() int
|
func (l *LRUCache[K, V]) Len() int
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -586,16 +585,28 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
cache := algorithm.NewLRUCache[int, int](2)
|
cache := algorithm.NewLRUCache[int, int](2)
|
||||||
|
|
||||||
cache.Put(1, 1)
|
cache.Put(1, 1)
|
||||||
cache.Put(2, 2)
|
cache.Put(2, 2)
|
||||||
cache.Put(3, 3)
|
|
||||||
|
|
||||||
fmt.Println(cache.Len()) // 3
|
result1, ok1 := cache.Get(1)
|
||||||
|
result2, ok2 := cache.Get(2)
|
||||||
|
result3, ok3 := cache.Get(3)
|
||||||
|
|
||||||
v, ok := cache.Get(1)
|
fmt.Println(result1, ok1)
|
||||||
fmt.Println(v, ok) // 1 true
|
fmt.Println(result2, ok2)
|
||||||
|
fmt.Println(result3, ok3)
|
||||||
|
|
||||||
ok = cache.Delete(1)
|
fmt.Println(cache.Len())
|
||||||
fmt.Println(ok) // true
|
|
||||||
|
ok := cache.Delete(2)
|
||||||
|
fmt.Println(ok)
|
||||||
|
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 1 true
|
||||||
|
// 2 true
|
||||||
|
// 0 false
|
||||||
|
// 2
|
||||||
|
// true
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -61,8 +61,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Bridge">Bridge</span>
|
### <span id="Bridge">Bridge</span>
|
||||||
|
|
||||||
<p>Link multiple channels into one channel until cancel the context.</p>
|
<p>Link multiple channels into one channel until cancel the context.</p>
|
||||||
@@ -105,6 +103,7 @@ func main() {
|
|||||||
for v := range c.Bridge(ctx, genVals()) {
|
for v := range c.Bridge(ctx, genVals()) {
|
||||||
fmt.Println(v)
|
fmt.Println(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1
|
// 1
|
||||||
// 2
|
// 2
|
||||||
@@ -114,9 +113,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="FanIn">FanIn</span>
|
### <span id="FanIn">FanIn</span>
|
||||||
|
|
||||||
<p>Merge multiple channels into one channel until cancel the context.</p>
|
<p>Merge multiple channels into one channel until cancel the context.</p>
|
||||||
@@ -156,7 +152,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Repeat">Repeat</span>
|
### <span id="Repeat">Repeat</span>
|
||||||
|
|
||||||
<p>Create channel, put values into the channel repeatly until cancel the context.</p>
|
<p>Create channel, put values into the channel repeatly until cancel the context.</p>
|
||||||
@@ -187,6 +182,7 @@ func main() {
|
|||||||
for v := range intStream {
|
for v := range intStream {
|
||||||
fmt.Println(v)
|
fmt.Println(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1
|
// 1
|
||||||
// 2
|
// 2
|
||||||
@@ -195,8 +191,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Generate">Generate</span>
|
### <span id="Generate">Generate</span>
|
||||||
|
|
||||||
<p>Creates a channel, then put values into the channel.</p>
|
<p>Creates a channel, then put values into the channel.</p>
|
||||||
@@ -269,6 +263,7 @@ func main() {
|
|||||||
for v := range intStream {
|
for v := range intStream {
|
||||||
fmt.Println(v)
|
fmt.Println(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// hello
|
// hello
|
||||||
// hello
|
// hello
|
||||||
@@ -276,8 +271,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Or">Or</span>
|
### <span id="Or">Or</span>
|
||||||
|
|
||||||
<p>Read one or more channels into one channel, will close when any readin channel is closed.</p>
|
<p>Read one or more channels into one channel, will close when any readin channel is closed.</p>
|
||||||
@@ -321,9 +314,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="OrDone">OrDone</span>
|
### <span id="OrDone">OrDone</span>
|
||||||
|
|
||||||
<p>Read a channel into another channel, will close until cancel context.</p>
|
<p>Read a channel into another channel, will close until cancel context.</p>
|
||||||
@@ -354,6 +344,7 @@ func main() {
|
|||||||
for v := range c.OrDone(ctx, intStream) {
|
for v := range c.OrDone(ctx, intStream) {
|
||||||
fmt.Println(v)
|
fmt.Println(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1
|
// 1
|
||||||
// 1
|
// 1
|
||||||
@@ -361,9 +352,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Take">Take</span>
|
### <span id="Take">Take</span>
|
||||||
|
|
||||||
<p>Create a channel whose values are taken from another channel with limit number.</p>
|
<p>Create a channel whose values are taken from another channel with limit number.</p>
|
||||||
@@ -402,6 +390,7 @@ func main() {
|
|||||||
for v := range intStream {
|
for v := range intStream {
|
||||||
fmt.Println(v)
|
fmt.Println(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1
|
// 1
|
||||||
// 2
|
// 2
|
||||||
@@ -409,8 +398,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Tee">Tee</span>
|
### <span id="Tee">Tee</span>
|
||||||
|
|
||||||
<p>Split one chanel into two channels, until cancel the context.</p>
|
<p>Split one chanel into two channels, until cancel the context.</p>
|
||||||
@@ -444,6 +431,7 @@ func main() {
|
|||||||
fmt.Println(v)
|
fmt.Println(v)
|
||||||
fmt.Println(<-ch2)
|
fmt.Println(<-ch2)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1
|
// 1
|
||||||
// 1
|
// 1
|
||||||
|
|||||||
@@ -61,8 +61,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Bridge">Bridge</span>
|
### <span id="Bridge">Bridge</span>
|
||||||
|
|
||||||
<p>将多个channel链接到一个channel,直到取消上下文。</p>
|
<p>将多个channel链接到一个channel,直到取消上下文。</p>
|
||||||
@@ -105,6 +103,7 @@ func main() {
|
|||||||
for v := range c.Bridge(ctx, genVals()) {
|
for v := range c.Bridge(ctx, genVals()) {
|
||||||
fmt.Println(v)
|
fmt.Println(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1
|
// 1
|
||||||
// 2
|
// 2
|
||||||
@@ -114,9 +113,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="FanIn">FanIn</span>
|
### <span id="FanIn">FanIn</span>
|
||||||
|
|
||||||
<p>将多个channel合并为一个channel,直到取消上下文。</p>
|
<p>将多个channel合并为一个channel,直到取消上下文。</p>
|
||||||
@@ -156,7 +152,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Generate">Generate</span>
|
### <span id="Generate">Generate</span>
|
||||||
|
|
||||||
<p>根据传入的值,生成channel.</p>
|
<p>根据传入的值,生成channel.</p>
|
||||||
@@ -225,6 +220,7 @@ func main() {
|
|||||||
for v := range intStream {
|
for v := range intStream {
|
||||||
fmt.Println(v)
|
fmt.Println(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1
|
// 1
|
||||||
// 2
|
// 2
|
||||||
@@ -233,9 +229,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RepeatFn">RepeatFn</span>
|
### <span id="RepeatFn">RepeatFn</span>
|
||||||
|
|
||||||
<p>返回一个channel,重复执行函数fn,并将结果放入返回的channel,直到取消上下文。</p>
|
<p>返回一个channel,重复执行函数fn,并将结果放入返回的channel,直到取消上下文。</p>
|
||||||
@@ -277,8 +270,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Or">Or</span>
|
### <span id="Or">Or</span>
|
||||||
|
|
||||||
<p>将一个或多个channel读取到一个channel中,当任何读取channel关闭时将结束读取。</p>
|
<p>将一个或多个channel读取到一个channel中,当任何读取channel关闭时将结束读取。</p>
|
||||||
@@ -322,9 +313,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="OrDone">OrDone</span>
|
### <span id="OrDone">OrDone</span>
|
||||||
|
|
||||||
<p>将一个channel读入另一个channel,直到取消上下文。</p>
|
<p>将一个channel读入另一个channel,直到取消上下文。</p>
|
||||||
@@ -355,6 +343,7 @@ func main() {
|
|||||||
for v := range c.OrDone(ctx, intStream) {
|
for v := range c.OrDone(ctx, intStream) {
|
||||||
fmt.Println(v)
|
fmt.Println(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1
|
// 1
|
||||||
// 1
|
// 1
|
||||||
@@ -362,9 +351,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Take">Take</span>
|
### <span id="Take">Take</span>
|
||||||
|
|
||||||
<p>返回一个channel,其值从另一个channel获取,直到取消上下文。</p>
|
<p>返回一个channel,其值从另一个channel获取,直到取消上下文。</p>
|
||||||
@@ -403,6 +389,7 @@ func main() {
|
|||||||
for v := range intStream {
|
for v := range intStream {
|
||||||
fmt.Println(v)
|
fmt.Println(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 1
|
// 1
|
||||||
// 2
|
// 2
|
||||||
@@ -410,8 +397,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Tee">Tee</span>
|
### <span id="Tee">Tee</span>
|
||||||
|
|
||||||
<p>将一个channel分成两个channel,直到取消上下文。</p>
|
<p>将一个channel分成两个channel,直到取消上下文。</p>
|
||||||
|
|||||||
@@ -58,40 +58,51 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// bool
|
// bool
|
||||||
fmt.Println(condition.Bool(false)) // false
|
result1 := condition.Bool(false)
|
||||||
fmt.Println(condition.Bool(true)) // true
|
result2 := condition.Bool(true)
|
||||||
|
fmt.Println(result1) // false
|
||||||
|
fmt.Println(result2) // true
|
||||||
|
|
||||||
// integer
|
// integer
|
||||||
fmt.Println(condition.Bool(0)) // false
|
result3 := condition.Bool(0)
|
||||||
fmt.Println(condition.Bool(1)) // true
|
result4 := condition.Bool(1)
|
||||||
|
fmt.Println(result3) // false
|
||||||
// float
|
fmt.Println(result4) // true
|
||||||
fmt.Println(condition.Bool(0.0)) // false
|
|
||||||
fmt.Println(condition.Bool(0.1)) // true
|
|
||||||
|
|
||||||
// string
|
// string
|
||||||
fmt.Println(condition.Bool("")) // false
|
result5 := condition.Bool("")
|
||||||
fmt.Println(condition.Bool(" ")) // true
|
result6 := condition.Bool(" ")
|
||||||
fmt.Println(condition.Bool("0")) // true
|
fmt.Println(result5) // false
|
||||||
|
fmt.Println(result6) // true
|
||||||
|
|
||||||
// slice
|
// slice
|
||||||
var nums [2]int
|
nums := []int{}
|
||||||
fmt.Println(condition.Bool(nums)) // false
|
result7 := condition.Bool(nums)
|
||||||
nums = [2]int{0, 1}
|
|
||||||
fmt.Println(condition.Bool(nums)) // true
|
|
||||||
|
|
||||||
// map
|
nums = append(nums, 1, 2)
|
||||||
fmt.Println(condition.Bool(map[string]string{})) // false
|
result8 := condition.Bool(nums)
|
||||||
fmt.Println(condition.Bool(map[string]string{"a": "a"})) // true
|
fmt.Println(result7) // false
|
||||||
|
fmt.Println(result8) // true
|
||||||
|
|
||||||
// struct
|
// struct
|
||||||
fmt.Println(condition.Bool(struct{}{})) // false
|
result9 = condition.Bool(struct{}{})
|
||||||
fmt.Println(condition.Bool(time.Now())) // true
|
fmt.Println(result8) // false
|
||||||
|
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="And">And</span>
|
### <span id="And">And</span>
|
||||||
<p>Returns true if both a and b are truthy.</p>
|
<p>Returns true if both a and b are truthy.</p>
|
||||||
|
|
||||||
@@ -277,10 +288,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
trueValue := "1"
|
conditionTrue := 2 > 1
|
||||||
falseValue := "0"
|
result1 := condition.TernaryOperator(conditionTrue, 0, 1)
|
||||||
|
|
||||||
fmt.Println(condition.TernaryOperator(true, trueValue, falseValue)) // "1"
|
conditionFalse := 2 > 3
|
||||||
|
result2 := condition.TernaryOperator(conditionFalse, 0, 1)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0
|
||||||
|
// 1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -57,40 +57,50 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// bool
|
// bool
|
||||||
fmt.Println(condition.Bool(false)) // false
|
result1 := condition.Bool(false)
|
||||||
fmt.Println(condition.Bool(true)) // true
|
result2 := condition.Bool(true)
|
||||||
|
fmt.Println(result1) // false
|
||||||
|
fmt.Println(result2) // true
|
||||||
|
|
||||||
// integer
|
// integer
|
||||||
fmt.Println(condition.Bool(0)) // false
|
result3 := condition.Bool(0)
|
||||||
fmt.Println(condition.Bool(1)) // true
|
result4 := condition.Bool(1)
|
||||||
|
fmt.Println(result3) // false
|
||||||
// float
|
fmt.Println(result4) // true
|
||||||
fmt.Println(condition.Bool(0.0)) // false
|
|
||||||
fmt.Println(condition.Bool(0.1)) // true
|
|
||||||
|
|
||||||
// string
|
// string
|
||||||
fmt.Println(condition.Bool("")) // false
|
result5 := condition.Bool("")
|
||||||
fmt.Println(condition.Bool(" ")) // true
|
result6 := condition.Bool(" ")
|
||||||
fmt.Println(condition.Bool("0")) // true
|
fmt.Println(result5) // false
|
||||||
|
fmt.Println(result6) // true
|
||||||
|
|
||||||
// slice
|
// slice
|
||||||
var nums [2]int
|
nums := []int{}
|
||||||
fmt.Println(condition.Bool(nums)) // false
|
result7 := condition.Bool(nums)
|
||||||
nums = [2]int{0, 1}
|
|
||||||
fmt.Println(condition.Bool(nums)) // true
|
|
||||||
|
|
||||||
// map
|
nums = append(nums, 1, 2)
|
||||||
fmt.Println(condition.Bool(map[string]string{})) // false
|
result8 := condition.Bool(nums)
|
||||||
fmt.Println(condition.Bool(map[string]string{"a": "a"})) // true
|
fmt.Println(result7) // false
|
||||||
|
fmt.Println(result8) // true
|
||||||
|
|
||||||
// struct
|
// struct
|
||||||
fmt.Println(condition.Bool(struct{}{})) // false
|
result9 = condition.Bool(struct{}{})
|
||||||
fmt.Println(condition.Bool(time.Now())) // true
|
fmt.Println(result8) // false
|
||||||
|
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// true
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="And">And</span>
|
### <span id="And">And</span>
|
||||||
<p>逻辑且操作,当切仅当a和b都为true时返回true</p>
|
<p>逻辑且操作,当切仅当a和b都为true时返回true</p>
|
||||||
|
|
||||||
@@ -117,8 +127,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Or">Or</span>
|
### <span id="Or">Or</span>
|
||||||
<p>逻辑或操作,当切仅当a和b都为false时返回false</p>
|
<p>逻辑或操作,当切仅当a和b都为false时返回false</p>
|
||||||
|
|
||||||
@@ -145,8 +153,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Xor">Xor</span>
|
### <span id="Xor">Xor</span>
|
||||||
<p>逻辑异或操作,a和b相同返回false,a和b不相同返回true</p>
|
<p>逻辑异或操作,a和b相同返回false,a和b不相同返回true</p>
|
||||||
|
|
||||||
@@ -173,8 +179,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Nor">Nor</span>
|
### <span id="Nor">Nor</span>
|
||||||
<p>异或的取反操作</p>
|
<p>异或的取反操作</p>
|
||||||
|
|
||||||
@@ -201,8 +205,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Xnor">Xnor</span>
|
### <span id="Xnor">Xnor</span>
|
||||||
<p>如果a和b都是真的或a和b均是假的,则返回true。</p>
|
<p>如果a和b都是真的或a和b均是假的,则返回true。</p>
|
||||||
|
|
||||||
@@ -255,8 +257,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="TernaryOperator">TernaryOperator</span>
|
### <span id="TernaryOperator">TernaryOperator</span>
|
||||||
<p>三元运算符</p>
|
<p>三元运算符</p>
|
||||||
|
|
||||||
@@ -276,10 +276,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
trueValue := "1"
|
conditionTrue := 2 > 1
|
||||||
falseValue := "0"
|
result1 := condition.TernaryOperator(conditionTrue, 0, 1)
|
||||||
|
|
||||||
fmt.Println(condition.TernaryOperator(true, trueValue, falseValue)) // "1"
|
conditionFalse := 2 > 3
|
||||||
|
result2 := condition.TernaryOperator(conditionFalse, 0, 1)
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0
|
||||||
|
// 1
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -40,8 +40,6 @@ import (
|
|||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ColorHexToRGB">ColorHexToRGB</span>
|
### <span id="ColorHexToRGB">ColorHexToRGB</span>
|
||||||
<p>Convert color hex to color rgb.</p>
|
<p>Convert color hex to color rgb.</p>
|
||||||
|
|
||||||
@@ -62,13 +60,15 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
colorHex := "#003366"
|
colorHex := "#003366"
|
||||||
r, g, b := convertor.ColorHexToRGB(colorHex)
|
r, g, b := convertor.ColorHexToRGB(colorHex)
|
||||||
fmt.Println(r, g, b) //0,51,102
|
|
||||||
|
fmt.Println(r, g, b)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0 51 102
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ColorRGBToHex">ColorRGBToHex</span>
|
### <span id="ColorRGBToHex">ColorRGBToHex</span>
|
||||||
|
|
||||||
<p>Convert color rgb to color hex.</p>
|
<p>Convert color rgb to color hex.</p>
|
||||||
@@ -90,16 +90,17 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
r := 0
|
r := 0
|
||||||
g := 51
|
g := 51
|
||||||
b := 102
|
b := 102
|
||||||
colorHex := convertor.ColorRGBToHex(r, g, b)
|
colorHex := ColorRGBToHex(r, g, b)
|
||||||
|
|
||||||
fmt.Println(colorHex) //#003366
|
fmt.Println(colorHex)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// #003366
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToBool">ToBool</span>
|
### <span id="ToBool">ToBool</span>
|
||||||
|
|
||||||
<p>Convert string to bool. Use strconv.ParseBool.</p>
|
<p>Convert string to bool. Use strconv.ParseBool.</p>
|
||||||
@@ -120,22 +121,26 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
v1, _ := convertor.ToBool("1")
|
cases := []string{"1", "true", "True", "false", "False", "0", "123", "0.0", "abc"}
|
||||||
fmt.Println(v1) //true
|
|
||||||
|
|
||||||
v2, _ := convertor.ToBool("true")
|
for i := 0; i < len(cases); i++ {
|
||||||
fmt.Println(v2) //true
|
result, _ := convertor.ToBool(cases[i])
|
||||||
|
fmt.Println(result)
|
||||||
|
}
|
||||||
|
|
||||||
v3, _ := convertor.ToBool("True")
|
// Output:
|
||||||
fmt.Println(v3) //true
|
// true
|
||||||
|
// true
|
||||||
v4, _ := convertor.ToBool("123")
|
// true
|
||||||
fmt.Println(v4) //false
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToBytes">ToBytes</span>
|
### <span id="ToBytes">ToBytes</span>
|
||||||
|
|
||||||
<p>Convert value to byte slice.</p>
|
<p>Convert value to byte slice.</p>
|
||||||
@@ -156,16 +161,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
bytesData, err := convertor.ToBytes("0")
|
bytesData, err := convertor.ToBytes("abc")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
fmt.Println(bytesData) //[]bytes{3, 4, 0, 0}
|
|
||||||
|
fmt.Println(bytesData)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [97 98 99]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToChar">ToChar</span>
|
### <span id="ToChar">ToChar</span>
|
||||||
|
|
||||||
<p>Convert string to char slice.</p>
|
<p>Convert string to char slice.</p>
|
||||||
@@ -186,18 +193,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
chars := convertor.ToChar("")
|
result1 := convertor.ToChar("")
|
||||||
fmt.Println(chars) //[]string{""}
|
result2 := convertor.ToChar("abc")
|
||||||
|
result3 := convertor.ToChar("1 2#3")
|
||||||
|
|
||||||
chars = convertor.ToChar("abc")
|
fmt.Println(result1)
|
||||||
fmt.Println(chars) //[]string{"a", "b", "c"}
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
chars = convertor.ToChar("1 2#3")
|
// Output:
|
||||||
fmt.Println(chars) //[]string{"1", " ", "2", "#", "3"}
|
// []
|
||||||
|
// [a b c]
|
||||||
|
// [1 2 # 3]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToChannel">ToChannel</span>
|
### <span id="ToChannel">ToChannel</span>
|
||||||
|
|
||||||
<p>Convert a collection of elements to a read-only channel.</p>
|
<p>Convert a collection of elements to a read-only channel.</p>
|
||||||
@@ -219,23 +229,21 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ch := convertor.ToChannel([]int{1, 2, 3})
|
ch := convertor.ToChannel([]int{1, 2, 3})
|
||||||
|
result1 := <-ch
|
||||||
|
result2 := <-ch
|
||||||
|
result3 := <-ch
|
||||||
|
|
||||||
val1, _ := <-ch
|
fmt.Println(result1)
|
||||||
fmt.Println(val1) //1
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
val2, _ := <-ch
|
// Output:
|
||||||
fmt.Println(val2) //2
|
// 1
|
||||||
|
// 2
|
||||||
val3, _ := <-ch
|
// 3
|
||||||
fmt.Println(val3) //3
|
|
||||||
|
|
||||||
_, ok := <-ch
|
|
||||||
fmt.Println(ok) //false
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToFloat">ToFloat</span>
|
### <span id="ToFloat">ToFloat</span>
|
||||||
|
|
||||||
<p>Convert value to a float64 value. If param is a invalid floatable, will return 0.0 and error. </p>
|
<p>Convert value to a float64 value. If param is a invalid floatable, will return 0.0 and error. </p>
|
||||||
@@ -256,19 +264,30 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
v, err := convertor.ToFloat("")
|
result1, _ := convertor.ToFloat("")
|
||||||
if err != nil {
|
result2, err := convertor.ToFloat("abc")
|
||||||
fmt.Println(err) //strconv.ParseFloat: parsing "": invalid syntax
|
result3, _ := convertor.ToFloat("-1")
|
||||||
}
|
result4, _ := convertor.ToFloat("-.11")
|
||||||
fmt.Println(v) //0
|
result5, _ := convertor.ToFloat("1.23e3")
|
||||||
|
result6, _ := convertor.ToFloat(true)
|
||||||
|
|
||||||
v, _ = convertor.ToFloat("-.11")
|
fmt.Println(result1)
|
||||||
fmt.Println(v) //-0.11
|
fmt.Println(result2, err)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0
|
||||||
|
// 0 strconv.ParseFloat: parsing "": invalid syntax
|
||||||
|
// -1
|
||||||
|
// -0.11
|
||||||
|
// 1230
|
||||||
|
// 0
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToInt">ToInt</span>
|
### <span id="ToInt">ToInt</span>
|
||||||
|
|
||||||
<p>Convert value to a int64 value. If param is a invalid intable, will return 0 and error. </p>
|
<p>Convert value to a int64 value. If param is a invalid intable, will return 0 and error. </p>
|
||||||
@@ -289,19 +308,27 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
v, err := convertor.ToInt("")
|
result1, _ := convertor.ToInt("123")
|
||||||
if err != nil {
|
result2, _ := convertor.ToInt("-123")
|
||||||
fmt.Println(err) //strconv.ParseInt: parsing "": invalid syntax
|
result3, _ := convertor.ToInt(float64(12.3))
|
||||||
}
|
result4, err := convertor.ToInt("abc")
|
||||||
fmt.Println(v) //0
|
result5, _ := convertor.ToInt(true)
|
||||||
|
|
||||||
v, _ = convertor.ToFloat(1.12)
|
fmt.Println(result1)
|
||||||
fmt.Println(v) //1
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4, err)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 123
|
||||||
|
// -123
|
||||||
|
// 12
|
||||||
|
// 0 strconv.ParseInt: parsing "": invalid syntax
|
||||||
|
// 0
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToJson">ToJson</span>
|
### <span id="ToJson">ToJson</span>
|
||||||
|
|
||||||
<p>Convert interface to json string. If param can't be converted, will return "" and error. </p>
|
<p>Convert interface to json string. If param can't be converted, will return "" and error. </p>
|
||||||
@@ -322,14 +349,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var aMap = map[string]int{"a": 1, "b": 2, "c": 3}
|
aMap := map[string]int{"a": 1, "b": 2, "c": 3}
|
||||||
jsonStr, _ := convertor.ToJson(aMap)
|
result, err := ToJson(aMap)
|
||||||
fmt.Printf("%q", jsonStr) //"{\"a\":1,\"b\":2,\"c\":3}"
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// {"a":1,"b":2,"c":3}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToMap">ToMap</span>
|
### <span id="ToMap">ToMap</span>
|
||||||
|
|
||||||
<p>Convert a slice of structs to a map based on iteratee function. </p>
|
<p>Convert a slice of structs to a map based on iteratee function. </p>
|
||||||
@@ -358,16 +391,18 @@ func main() {
|
|||||||
{name: "Hello", code: 100},
|
{name: "Hello", code: 100},
|
||||||
{name: "Hi", code: 101},
|
{name: "Hi", code: 101},
|
||||||
}
|
}
|
||||||
|
|
||||||
result := convertor.ToMap(messages, func(msg Message) (int, string) {
|
result := convertor.ToMap(messages, func(msg Message) (int, string) {
|
||||||
return msg.code, msg.name
|
return msg.code, msg.name
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Println(result) //{100: "Hello", 101: "Hi"}
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// map[100:Hello 101:Hi]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToPointer">ToPointer</span>
|
### <span id="ToPointer">ToPointer</span>
|
||||||
|
|
||||||
<p>Returns a pointer to passed value. </p>
|
<p>Returns a pointer to passed value. </p>
|
||||||
@@ -389,11 +424,13 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result := convertor.ToPointer(123)
|
result := convertor.ToPointer(123)
|
||||||
fmt.Println(*result) //123
|
fmt.Println(*result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 123
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToString">ToString</span>
|
### <span id="ToString">ToString</span>
|
||||||
|
|
||||||
<p>ToString convert value to string, for number, string, []byte, will convert to string. For other type (slice, map, array, struct) will call json.Marshal</p>
|
<p>ToString convert value to string, for number, string, []byte, will convert to string. For other type (slice, map, array, struct) will call json.Marshal</p>
|
||||||
@@ -414,13 +451,33 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Printf("%q", convertor.ToString(1)) //"1"
|
result1 := convertor.ToString("")
|
||||||
fmt.Printf("%q", convertor.ToString(1.1)) //"1.1"
|
result2 := convertor.ToString(nil)
|
||||||
fmt.Printf("%q", convertor.ToString([]int{1, 2, 3})) //"[1,2,3]"
|
result3 := convertor.ToString(0)
|
||||||
|
result4 := convertor.ToString(1.23)
|
||||||
|
result5 := convertor.ToString(true)
|
||||||
|
result6 := convertor.ToString(false)
|
||||||
|
result7 := convertor.ToString([]int{1, 2, 3})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 0
|
||||||
|
// 1.23
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// [1,2,3]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="StructToMap">StructToMap</span>
|
### <span id="StructToMap">StructToMap</span>
|
||||||
|
|
||||||
<p>Convert struct to map, only convert exported field, struct field tag `json` should be set.</p>
|
<p>Convert struct to map, only convert exported field, struct field tag `json` should be set.</p>
|
||||||
@@ -442,16 +499,19 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
type People struct {
|
type People struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
age int
|
age int
|
||||||
}
|
}
|
||||||
p := People{
|
p := People{
|
||||||
"test",
|
"test",
|
||||||
100,
|
100,
|
||||||
}
|
}
|
||||||
pm, _ := convertor.StructToMap(p)
|
pm, _ := convertor.StructToMap(p)
|
||||||
|
|
||||||
fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test]
|
fmt.Println(pm)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// map[name:test]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -508,12 +568,13 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
byteData, _ := convertor.EncodeByte("abc")
|
byteData, _ := convertor.EncodeByte("abc")
|
||||||
fmt.Println(byteData) //[]byte{6, 12, 0, 3, 97, 98, 99}
|
fmt.Println(byteData)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [6 12 0 3 97 98 99]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DecodeByte">DecodeByte</span>
|
### <span id="DecodeByte">DecodeByte</span>
|
||||||
|
|
||||||
<p>Decode byte data to target object. target should be a pointer instance.</p>
|
<p>Decode byte data to target object. target should be a pointer instance.</p>
|
||||||
@@ -536,7 +597,15 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
var result string
|
var result string
|
||||||
byteData := []byte{6, 12, 0, 3, 97, 98, 99}
|
byteData := []byte{6, 12, 0, 3, 97, 98, 99}
|
||||||
convertor.DecodeByte(byteData, &result)
|
|
||||||
fmt.Println(result) //"abc"
|
err := convertor.DecodeByte(byteData, &result)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// abc
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -43,16 +43,15 @@ import (
|
|||||||
## 文档
|
## 文档
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ColorHexToRGB">ColorHexToRGB</span>
|
### <span id="ColorHexToRGB">ColorHexToRGB</span>
|
||||||
<p>颜色值十六进制转rgb</p>
|
<p>颜色值十六进制转rgb。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ColorHexToRGB(colorHex string) (red, green, blue int)
|
func ColorHexToRGB(colorHex string) (red, green, blue int)
|
||||||
```
|
```
|
||||||
<b>列子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -64,23 +63,25 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
colorHex := "#003366"
|
colorHex := "#003366"
|
||||||
r, g, b := convertor.ColorHexToRGB(colorHex)
|
r, g, b := convertor.ColorHexToRGB(colorHex)
|
||||||
fmt.Println(r, g, b) //0,51,102
|
|
||||||
|
fmt.Println(r, g, b)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0 51 102
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ColorRGBToHex">ColorRGBToHex</span>
|
### <span id="ColorRGBToHex">ColorRGBToHex</span>
|
||||||
|
|
||||||
<p>颜色值rgb转十六进制</p>
|
<p>颜色值rgb转十六进制。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ColorRGBToHex(red, green, blue int) string
|
func ColorRGBToHex(red, green, blue int) string
|
||||||
```
|
```
|
||||||
<b>列子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -92,26 +93,27 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
r := 0
|
r := 0
|
||||||
g := 51
|
g := 51
|
||||||
b := 102
|
b := 102
|
||||||
colorHex := convertor.ColorRGBToHex(r, g, b)
|
colorHex := ColorRGBToHex(r, g, b)
|
||||||
|
|
||||||
fmt.Println(colorHex) //#003366
|
fmt.Println(colorHex)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// #003366
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToBool">ToBool</span>
|
### <span id="ToBool">ToBool</span>
|
||||||
|
|
||||||
<p>字符串转布尔类型,使用strconv.ParseBool</p>
|
<p>字符串转布尔类型,使用strconv.ParseBool。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ToBool(s string) (bool, error)
|
func ToBool(s string) (bool, error)
|
||||||
```
|
```
|
||||||
<b>列子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -122,32 +124,36 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
v1, _ := convertor.ToBool("1")
|
cases := []string{"1", "true", "True", "false", "False", "0", "123", "0.0", "abc"}
|
||||||
fmt.Println(v1) //true
|
|
||||||
|
|
||||||
v2, _ := convertor.ToBool("true")
|
for i := 0; i < len(cases); i++ {
|
||||||
fmt.Println(v2) //true
|
result, _ := convertor.ToBool(cases[i])
|
||||||
|
fmt.Println(result)
|
||||||
|
}
|
||||||
|
|
||||||
v3, _ := convertor.ToBool("True")
|
// Output:
|
||||||
fmt.Println(v3) //true
|
// true
|
||||||
|
// true
|
||||||
v4, _ := convertor.ToBool("123")
|
// true
|
||||||
fmt.Println(v4) //false
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToBytes">ToBytes</span>
|
### <span id="ToBytes">ToBytes</span>
|
||||||
|
|
||||||
<p>interface转字节切片.</p>
|
<p>Interface转字节切片。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ToBytes(data any) ([]byte, error)
|
func ToBytes(data any) ([]byte, error)
|
||||||
```
|
```
|
||||||
<b>列子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -158,26 +164,28 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
bytesData, err := convertor.ToBytes("0")
|
bytesData, err := convertor.ToBytes("abc")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
}
|
}
|
||||||
fmt.Println(bytesData) //[]bytes{3, 4, 0, 0}
|
|
||||||
|
fmt.Println(bytesData)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [97 98 99]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToChar">ToChar</span>
|
### <span id="ToChar">ToChar</span>
|
||||||
|
|
||||||
<p>字符串转字符切片</p>
|
<p>字符串转字符切片。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ToChar(s string) []string
|
func ToChar(s string) []string
|
||||||
```
|
```
|
||||||
<b>列子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -188,29 +196,31 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
chars := convertor.ToChar("")
|
result1 := convertor.ToChar("")
|
||||||
fmt.Println(chars) //[]string{""}
|
result2 := convertor.ToChar("abc")
|
||||||
|
result3 := convertor.ToChar("1 2#3")
|
||||||
|
|
||||||
chars = convertor.ToChar("abc")
|
fmt.Println(result1)
|
||||||
fmt.Println(chars) //[]string{"a", "b", "c"}
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
chars = convertor.ToChar("1 2#3")
|
// Output:
|
||||||
fmt.Println(chars) //[]string{"1", " ", "2", "#", "3"}
|
// []
|
||||||
|
// [a b c]
|
||||||
|
// [1 2 # 3]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToChannel">ToChannel</span>
|
### <span id="ToChannel">ToChannel</span>
|
||||||
|
|
||||||
<p>将切片转为只读channel</p>
|
<p>将切片转为只读channel。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ToChannel[T any](array []T) <-chan T
|
func ToChannel[T any](array []T) <-chan T
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -222,33 +232,31 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
ch := convertor.ToChannel([]int{1, 2, 3})
|
ch := convertor.ToChannel([]int{1, 2, 3})
|
||||||
|
result1 := <-ch
|
||||||
|
result2 := <-ch
|
||||||
|
result3 := <-ch
|
||||||
|
|
||||||
val1, _ := <-ch
|
fmt.Println(result1)
|
||||||
fmt.Println(val1) //1
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
val2, _ := <-ch
|
// Output:
|
||||||
fmt.Println(val2) //2
|
// 1
|
||||||
|
// 2
|
||||||
val3, _ := <-ch
|
// 3
|
||||||
fmt.Println(val3) //3
|
|
||||||
|
|
||||||
_, ok := <-ch
|
|
||||||
fmt.Println(ok) //false
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToFloat">ToFloat</span>
|
### <span id="ToFloat">ToFloat</span>
|
||||||
|
|
||||||
<p>将interface转成float64类型,如果参数无法转换,会返回0和error</p>
|
<p>将interface转成float64类型,如果参数无法转换,会返回0和error。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ToFloat(value any) (float64, error)
|
func ToFloat(value any) (float64, error)
|
||||||
```
|
```
|
||||||
<b>列子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -259,29 +267,40 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
v, err := convertor.ToFloat("")
|
result1, _ := convertor.ToFloat("")
|
||||||
if err != nil {
|
result2, err := convertor.ToFloat("abc")
|
||||||
fmt.Println(err) //strconv.ParseFloat: parsing "": invalid syntax
|
result3, _ := convertor.ToFloat("-1")
|
||||||
}
|
result4, _ := convertor.ToFloat("-.11")
|
||||||
fmt.Println(v) //0
|
result5, _ := convertor.ToFloat("1.23e3")
|
||||||
|
result6, _ := convertor.ToFloat(true)
|
||||||
|
|
||||||
v, _ = convertor.ToFloat("-.11")
|
fmt.Println(result1)
|
||||||
fmt.Println(v) //-0.11
|
fmt.Println(result2, err)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 0
|
||||||
|
// 0 strconv.ParseFloat: parsing "": invalid syntax
|
||||||
|
// -1
|
||||||
|
// -0.11
|
||||||
|
// 1230
|
||||||
|
// 0
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToInt">ToInt</span>
|
### <span id="ToInt">ToInt</span>
|
||||||
|
|
||||||
<p>将interface转成int64类型,如果参数无法转换,会返回0和error</p>
|
<p>将interface转成int64类型,如果参数无法转换,会返回0和error。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ToInt(value any) (int64, error)
|
func ToInt(value any) (int64, error)
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -292,29 +311,37 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
v, err := convertor.ToInt("")
|
result1, _ := convertor.ToInt("123")
|
||||||
if err != nil {
|
result2, _ := convertor.ToInt("-123")
|
||||||
fmt.Println(err) //strconv.ParseInt: parsing "": invalid syntax
|
result3, _ := convertor.ToInt(float64(12.3))
|
||||||
}
|
result4, err := convertor.ToInt("abc")
|
||||||
fmt.Println(v) //0
|
result5, _ := convertor.ToInt(true)
|
||||||
|
|
||||||
v, _ = convertor.ToFloat(1.12)
|
fmt.Println(result1)
|
||||||
fmt.Println(v) //1
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4, err)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 123
|
||||||
|
// -123
|
||||||
|
// 12
|
||||||
|
// 0 strconv.ParseInt: parsing "": invalid syntax
|
||||||
|
// 0
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToJson">ToJson</span>
|
### <span id="ToJson">ToJson</span>
|
||||||
|
|
||||||
<p>将interface转成json字符串,如果参数无法转换,会返回""和error</p>
|
<p>将interface转成json字符串,如果参数无法转换,会返回""和error。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ToJson(value any) (string, error)
|
func ToJson(value any) (string, error)
|
||||||
```
|
```
|
||||||
<b>列子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -325,24 +352,30 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var aMap = map[string]int{"a": 1, "b": 2, "c": 3}
|
aMap := map[string]int{"a": 1, "b": 2, "c": 3}
|
||||||
jsonStr, _ := convertor.ToJson(aMap)
|
result, err := ToJson(aMap)
|
||||||
fmt.Printf("%q", jsonStr) //"{\"a\":1,\"b\":2,\"c\":3}"
|
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("%v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// {"a":1,"b":2,"c":3}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToMap">ToMap</span>
|
### <span id="ToMap">ToMap</span>
|
||||||
|
|
||||||
<p>将切片转为map</p>
|
<p>将切片转为map。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V
|
func ToMap[T any, K comparable, V any](array []T, iteratee func(T) (K, V)) map[K]V
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -361,26 +394,28 @@ func main() {
|
|||||||
{name: "Hello", code: 100},
|
{name: "Hello", code: 100},
|
||||||
{name: "Hi", code: 101},
|
{name: "Hi", code: 101},
|
||||||
}
|
}
|
||||||
|
|
||||||
result := convertor.ToMap(messages, func(msg Message) (int, string) {
|
result := convertor.ToMap(messages, func(msg Message) (int, string) {
|
||||||
return msg.code, msg.name
|
return msg.code, msg.name
|
||||||
})
|
})
|
||||||
|
|
||||||
fmt.Println(result) //{100: "Hello", 101: "Hi"}
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// map[100:Hello 101:Hi]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToPointer">ToPointer</span>
|
### <span id="ToPointer">ToPointer</span>
|
||||||
|
|
||||||
<p>返回传入值的指针</p>
|
<p>返回传入值的指针。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ToPointer[T any](value T) *T
|
func ToPointer[T any](value T) *T
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -392,22 +427,23 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result := convertor.ToPointer(123)
|
result := convertor.ToPointer(123)
|
||||||
fmt.Println(*result) //123
|
fmt.Println(*result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 123
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToString">ToString</span>
|
### <span id="ToString">ToString</span>
|
||||||
|
|
||||||
<p>将值转换为字符串,对于数字、字符串、[]byte,将转换为字符串。 对于其他类型(切片、映射、数组、结构)将调用 json.Marshal</p>
|
<p>将值转换为字符串,对于数字、字符串、[]byte,将转换为字符串。 对于其他类型(切片、映射、数组、结构体)将调用 json.Marshal</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ToString(value any) string
|
func ToString(value any) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -418,24 +454,43 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Printf("%q", convertor.ToString(1)) //"1"
|
result1 := convertor.ToString("")
|
||||||
fmt.Printf("%q", convertor.ToString(1.1)) //"1.1"
|
result2 := convertor.ToString(nil)
|
||||||
fmt.Printf("%q", convertor.ToString([]int{1, 2, 3})) //"[1,2,3]"
|
result3 := convertor.ToString(0)
|
||||||
|
result4 := convertor.ToString(1.23)
|
||||||
|
result5 := convertor.ToString(true)
|
||||||
|
result6 := convertor.ToString(false)
|
||||||
|
result7 := convertor.ToString([]int{1, 2, 3})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 0
|
||||||
|
// 1.23
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// [1,2,3]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="StructToMap">StructToMap</span>
|
### <span id="StructToMap">StructToMap</span>
|
||||||
|
|
||||||
<p>将struct转成map,只会转换struct中可导出的字段。struct中导出字段需要设置json tag标记</p>
|
<p>将struct转成map,只会转换struct中可导出的字段。struct中导出字段需要设置json tag标记。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func StructToMap(value any) (map[string]any, error)
|
func StructToMap(value any) (map[string]any, error)
|
||||||
```
|
```
|
||||||
<b>列子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -447,31 +502,32 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
type People struct {
|
type People struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
age int
|
age int
|
||||||
}
|
}
|
||||||
p := People{
|
p := People{
|
||||||
"test",
|
"test",
|
||||||
100,
|
100,
|
||||||
}
|
}
|
||||||
pm, _ := convertor.StructToMap(p)
|
pm, _ := convertor.StructToMap(p)
|
||||||
|
|
||||||
fmt.Printf("type: %T, value: %s", pm, pm) //type: map[string]interface {}, value: map[name:test]
|
fmt.Println(pm)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// map[name:test]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="MapToSlice">MapToSlice</span>
|
### <span id="MapToSlice">MapToSlice</span>
|
||||||
|
|
||||||
<p>map中key和value执行函数iteratee后,转为切片</p>
|
<p>map中key和value执行函数iteratee后,转为切片。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func MapToSlice[T any, K comparable, V any](aMap map[K]V, iteratee func(K, V) T) []T
|
func MapToSlice[T any, K comparable, V any](aMap map[K]V, iteratee func(K, V) T) []T
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -491,18 +547,16 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="EncodeByte">EncodeByte</span>
|
### <span id="EncodeByte">EncodeByte</span>
|
||||||
|
|
||||||
<p>将data编码成字节切片</p>
|
<p>将data编码成字节切片。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func EncodeByte(data any) ([]byte, error)
|
func EncodeByte(data any) ([]byte, error)
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -514,22 +568,23 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
byteData, _ := convertor.EncodeByte("abc")
|
byteData, _ := convertor.EncodeByte("abc")
|
||||||
fmt.Println(byteData) //[]byte{6, 12, 0, 3, 97, 98, 99}
|
fmt.Println(byteData)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// [6 12 0 3 97 98 99]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DecodeByte">DecodeByte</span>
|
### <span id="DecodeByte">DecodeByte</span>
|
||||||
|
|
||||||
<p>解码字节切片到目标对象,目标对象需要传入一个指针实例</p>
|
<p>解码字节切片到目标对象,目标对象需要传入一个指针实例。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func DecodeByte(data []byte, target any) error
|
func DecodeByte(data []byte, target any) error
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -542,7 +597,15 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
var result string
|
var result string
|
||||||
byteData := []byte{6, 12, 0, 3, 97, 98, 99}
|
byteData := []byte{6, 12, 0, 3, 97, 98, 99}
|
||||||
convertor.DecodeByte(byteData, &result)
|
|
||||||
fmt.Println(result) //"abc"
|
err := convertor.DecodeByte(byteData, &result)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// abc
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
440
docs/cryptor.md
440
docs/cryptor.md
@@ -61,8 +61,6 @@ import (
|
|||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesEcbEncrypt">AesEcbEncrypt</span>
|
### <span id="AesEcbEncrypt">AesEcbEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Encrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -83,16 +81,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
|
|
||||||
|
|
||||||
fmt.Println(string(encrypted))
|
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
|
||||||
|
decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesEcbDecrypt">AesEcbDecrypt</span>
|
### <span id="AesEcbDecrypt">AesEcbDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Decrypt data with key use AES ECB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -113,16 +114,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
|
|
||||||
decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key))
|
encrypted := cryptor.AesEcbEncrypt([]byte(data), []byte(key))
|
||||||
fmt.Println(string(decrypted)) //hello world
|
decrypted := cryptor.AesEcbDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCbcEncrypt">AesCbcEncrypt</span>
|
### <span id="AesCbcEncrypt">AesCbcEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Encrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -143,16 +147,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
|
|
||||||
|
|
||||||
fmt.Println(string(encrypted))
|
encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
|
||||||
|
decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCbcDecrypt">AesCbcDecrypt</span>
|
### <span id="AesCbcDecrypt">AesCbcDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -174,16 +181,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
|
|
||||||
decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key))
|
encrypted := cryptor.AesCbcEncrypt([]byte(data), []byte(key))
|
||||||
fmt.Println(string(decrypted)) //hello world
|
decrypted := cryptor.AesCbcDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCtrCrypt">AesCtrCrypt</span>
|
### <span id="AesCtrCrypt">AesCtrCrypt</span>
|
||||||
|
|
||||||
<p>Encrypt or decrypt data with key use AES CTR algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Encrypt or decrypt data with key use AES CTR algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -205,17 +215,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key))
|
|
||||||
decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key))
|
|
||||||
|
|
||||||
fmt.Println(string(decrypted)) //hello world
|
encrypted := cryptor.AesCtrCrypt([]byte(data), []byte(key))
|
||||||
|
decrypted := cryptor.AesCtrCrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCfbEncrypt">AesCfbEncrypt</span>
|
### <span id="AesCfbEncrypt">AesCfbEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with key use AES CFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Encrypt data with key use AES CFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -237,15 +249,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
|
|
||||||
fmt.Println(string(encrypted))
|
encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
|
||||||
|
decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCfbDecrypt">AesCfbDecrypt</span>
|
### <span id="AesCfbDecrypt">AesCfbDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Decrypt data with key use AES CBC algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -267,16 +283,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
|
|
||||||
decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))
|
encrypted := cryptor.AesCfbEncrypt([]byte(data), []byte(key))
|
||||||
fmt.Println(string(decrypted)) //hello world
|
decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesOfbEncrypt">AesOfbEncrypt</span>
|
### <span id="AesOfbEncrypt">AesOfbEncrypt</span>
|
||||||
|
|
||||||
<p>Enecrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Enecrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -298,15 +317,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
|
|
||||||
fmt.Println(string(encrypted))
|
encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
|
||||||
|
decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AesCfbDecrypt">AesOfbDecrypt</span>
|
### <span id="AesCfbDecrypt">AesOfbDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
<p>Decrypt data with key use AES OFB algorithm. Length of `key` param should be 16, 24 or 32.</p>
|
||||||
@@ -328,17 +350,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello"
|
||||||
key := "abcdefghijklmnop"
|
key := "abcdefghijklmnop"
|
||||||
encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
|
|
||||||
decrypted := cryptor.AesOfbDecrypt(encrypted, []byte(key))
|
encrypted := cryptor.AesOfbEncrypt([]byte(data), []byte(key))
|
||||||
|
decrypted := cryptor.AesCfbDecrypt(encrypted, []byte(key))
|
||||||
fmt.Println(string(decrypted)) //hello world
|
|
||||||
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Base64StdEncode">Base64StdEncode</span>
|
### <span id="Base64StdEncode">Base64StdEncode</span>
|
||||||
|
|
||||||
<p>Encode string with base64 encoding.</p>
|
<p>Encode string with base64 encoding.</p>
|
||||||
@@ -359,13 +383,13 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
base64Str := cryptor.Base64StdEncode("hello world")
|
base64Str := cryptor.Base64StdEncode("hello")
|
||||||
fmt.Println(base64Str) //aGVsbG8gd29ybGQ=
|
fmt.Println(base64Str)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// aGVsbG8=
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Base64StdDecode">Base64StdDecode</span>
|
### <span id="Base64StdDecode">Base64StdDecode</span>
|
||||||
|
|
||||||
<p>Decode a base64 encoded string.</p>
|
<p>Decode a base64 encoded string.</p>
|
||||||
@@ -387,13 +411,14 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
str := cryptor.Base64StdDecode("aGVsbG8gd29ybGQ=")
|
str := cryptor.Base64StdDecode("aGVsbG8=")
|
||||||
fmt.Println(str) //hello world
|
fmt.Println(str)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesEcbEncrypt">DesEcbEncrypt</span>
|
### <span id="DesEcbEncrypt">DesEcbEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p>
|
<p>Encrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -415,16 +440,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key))
|
|
||||||
|
|
||||||
fmt.Println(string(encrypted))
|
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key))
|
||||||
|
|
||||||
|
decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesEcbDecrypt">DesEcbDecrypt</span>
|
### <span id="DesEcbDecrypt">DesEcbDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p>
|
<p>Decrypt data with key use DES ECB algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -446,17 +474,20 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byt(key)
|
|
||||||
decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key))
|
encrypted := cryptor.DesEcbEncrypt([]byte(data), []byte(key))
|
||||||
|
|
||||||
fmt.Println(string(decrypted)) //hello world
|
decrypted := cryptor.DesEcbDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCbcEncrypt">DesCbcEncrypt</span>
|
### <span id="DesCbcEncrypt">DesCbcEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
|
<p>Encrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -478,16 +509,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesCbcEncrypt([]byte(data), []byt(key)
|
|
||||||
|
|
||||||
fmt.Println(string(encrypted))
|
encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key))
|
||||||
|
decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCbcDecrypt">DesCbcDecrypt</span>
|
### <span id="DesCbcDecrypt">DesCbcDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
|
<p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -509,17 +543,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesCbcEncrypt([]byte(data), []byt(key)
|
|
||||||
|
encrypted := cryptor.DesCbcEncrypt([]byte(data), []byte(key))
|
||||||
decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key))
|
decrypted := cryptor.DesCbcDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCtrCrypt">DesCtrCrypt</span>
|
### <span id="DesCtrCrypt">DesCtrCrypt</span>
|
||||||
|
|
||||||
<p>Encrypt or decrypt data with key use DES CTR algorithm. Length of `key` param should be 8.</p>
|
<p>Encrypt or decrypt data with key use DES CTR algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -541,17 +576,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key))
|
|
||||||
decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key))
|
|
||||||
|
|
||||||
fmt.Println(string(decrypted)) //hello world
|
encrypted := cryptor.DesCtrCrypt([]byte(data), []byte(key))
|
||||||
|
decrypted := cryptor.DesCtrCrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCfbEncrypt">DesCfbEncrypt</span>
|
### <span id="DesCfbEncrypt">DesCfbEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with key use DES CFB algorithm. Length of `key` param should be 8.</p>
|
<p>Encrypt data with key use DES CFB algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -573,15 +610,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byt(key)
|
|
||||||
fmt.Println(string(encrypted))
|
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key))
|
||||||
|
decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesCfbDecrypt">DesCfbDecrypt</span>
|
### <span id="DesCfbDecrypt">DesCfbDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
|
<p>Decrypt data with key use DES CBC algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -603,16 +643,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byt(key)
|
|
||||||
decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key))
|
encrypted := cryptor.DesCfbEncrypt([]byte(data), []byte(key))
|
||||||
fmt.Println(string(decrypted)) //hello world
|
decrypted := cryptor.DesCfbDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesOfbEncrypt">DesOfbEncrypt</span>
|
### <span id="DesOfbEncrypt">DesOfbEncrypt</span>
|
||||||
|
|
||||||
<p>Enecrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p>
|
<p>Enecrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -634,15 +676,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
|
|
||||||
fmt.Println(string(encrypted))
|
encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
|
||||||
|
decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key))
|
||||||
|
|
||||||
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="DesOfbDecrypt">DesOfbDecrypt</span>
|
### <span id="DesOfbDecrypt">DesOfbDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p>
|
<p>Decrypt data with key use DES OFB algorithm. Length of `key` param should be 8.</p>
|
||||||
@@ -664,17 +709,19 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
data := "hello world"
|
data := "hello"
|
||||||
key := "abcdefgh"
|
key := "abcdefgh"
|
||||||
encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
|
|
||||||
decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key))
|
encrypted := cryptor.DesOfbEncrypt([]byte(data), []byte(key))
|
||||||
|
decrypted := cryptor.DesOfbDecrypt(encrypted, []byte(key))
|
||||||
fmt.Println(string(decrypted)) //hello world
|
|
||||||
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacMd5">HmacMd5</span>
|
### <span id="HmacMd5">HmacMd5</span>
|
||||||
|
|
||||||
<p>Get the md5 hmac hash of string.</p>
|
<p>Get the md5 hmac hash of string.</p>
|
||||||
@@ -696,13 +743,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.HmacMd5("hello world", "12345"))
|
str := "hello"
|
||||||
fmt.Println(s) //5f4c9faaff0a1ad3007d9ddc06abe36d
|
key := "12345"
|
||||||
|
|
||||||
|
hms := cryptor.HmacMd5(str, key)
|
||||||
|
fmt.Println(hms)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// e834306eab892d872525d4918a7a639a
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacSha1">HmacSha1</span>
|
### <span id="HmacSha1">HmacSha1</span>
|
||||||
|
|
||||||
<p>Get the sha1 hmac hash of string.</p>
|
<p>Get the sha1 hmac hash of string.</p>
|
||||||
@@ -724,13 +774,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.HmacSha1("hello world", "12345"))
|
str := "hello"
|
||||||
fmt.Println(s) //3826f812255d8683f051ee97346d1359234d5dbd
|
key := "12345"
|
||||||
|
|
||||||
|
hms := cryptor.HmacSha1(str, key)
|
||||||
|
fmt.Println(hms)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 5c6a9db0cccb92e36ed0323fd09b7f936de9ace0
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacSha256">HmacSha256</span>
|
### <span id="HmacSha256">HmacSha256</span>
|
||||||
|
|
||||||
<p>Get the sha256 hmac hash of string</p>
|
<p>Get the sha256 hmac hash of string</p>
|
||||||
@@ -752,13 +805,17 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.HmacSha256("hello world", "12345"))
|
str := "hello"
|
||||||
fmt.Println(s) //9dce2609f2d67d41f74c7f9efc8ccd44370d41ad2de52982627588dfe7289ab8
|
key := "12345"
|
||||||
|
|
||||||
|
hms := cryptor.HmacSha256(str, key)
|
||||||
|
fmt.Println(hms)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 315bb93c4e989862ba09cb62e05d73a5f376cb36f0d786edab0c320d059fde75
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="HmacSha512">HmacSha512</span>
|
### <span id="HmacSha512">HmacSha512</span>
|
||||||
|
|
||||||
<p>Get the sha512 hmac hash of string.</p>
|
<p>Get the sha512 hmac hash of string.</p>
|
||||||
@@ -780,14 +837,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.HmacSha512("hello world", "12345"))
|
str := "hello"
|
||||||
fmt.Println(s)
|
key := "12345"
|
||||||
//5b1563ac4e9b49c9ada8ccb232588fc4f0c30fd12f756b3a0b95af4985c236ca60925253bae10ce2c6bf9af1c1679b51e5395ff3d2826c0a2c7c0d72225d4175
|
|
||||||
|
hms := cryptor.HmacSha512(str, key)
|
||||||
|
fmt.Println(hms)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// dd8f1290a9dd23d354e2526d9a2e9ce8cffffdd37cb320800d1c6c13d2efc363288376a196c5458daf53f8e1aa6b45a6d856303d5c0a2064bff9785861d48cfc
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Md5String">Md5String</span>
|
### <span id="Md5String">Md5String</span>
|
||||||
|
|
||||||
<p>Get the md5 value of string.</p>
|
<p>Get the md5 value of string.</p>
|
||||||
@@ -809,13 +870,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.Md5String("hello"))
|
str := "hello"
|
||||||
fmt.Println(s) //5d41402abc4b2a76b9719d911017c592
|
|
||||||
|
md5Str := cryptor.Md5String(str)
|
||||||
|
fmt.Println(md5Str)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 5d41402abc4b2a76b9719d911017c592
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Md5File">Md5File</span>
|
### <span id="Md5File">Md5File</span>
|
||||||
|
|
||||||
<p>Get the md5 value of file.</p>
|
<p>Get the md5 value of file.</p>
|
||||||
@@ -842,8 +906,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Sha1">Sha1</span>
|
### <span id="Sha1">Sha1</span>
|
||||||
|
|
||||||
<p>Get the sha1 value of string.</p>
|
<p>Get the sha1 value of string.</p>
|
||||||
@@ -865,13 +927,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.Sha1("hello world"))
|
str := "hello"
|
||||||
fmt.Println(s) //2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
|
|
||||||
|
result := cryptor.Sha1(str)
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Sha256">Sha256</span>
|
### <span id="Sha256">Sha256</span>
|
||||||
|
|
||||||
<p>Get the sha256 value of string.</p>
|
<p>Get the sha256 value of string.</p>
|
||||||
@@ -893,13 +958,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.Sha256("hello world"))
|
str := "hello"
|
||||||
fmt.Println(s) //b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9
|
|
||||||
|
result := cryptor.Sha256(str)
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Sha512">Sha512</span>
|
### <span id="Sha512">Sha512</span>
|
||||||
|
|
||||||
<p>Get the sha512 value of string.</p>
|
<p>Get the sha512 value of string.</p>
|
||||||
@@ -921,13 +989,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s := cryptor.Sha512("hello world"))
|
str := "hello"
|
||||||
fmt.Println(s) //309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f
|
|
||||||
|
result := cryptor.Sha512(str)
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="GenerateRsaKey">GenerateRsaKey</span>
|
### <span id="GenerateRsaKey">GenerateRsaKey</span>
|
||||||
|
|
||||||
<p>Create the rsa public and private key file in current directory.</p>
|
<p>Create the rsa public and private key file in current directory.</p>
|
||||||
@@ -956,8 +1027,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RsaEncrypt">RsaEncrypt</span>
|
### <span id="RsaEncrypt">RsaEncrypt</span>
|
||||||
|
|
||||||
<p>Encrypt data with public key file useing ras algorithm.</p>
|
<p>Encrypt data with public key file useing ras algorithm.</p>
|
||||||
@@ -981,19 +1050,21 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
|
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data := []byte("hello world")
|
data := []byte("hello")
|
||||||
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
||||||
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
||||||
|
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="RsaDecrypt">RsaDecrypt</span>
|
### <span id="RsaDecrypt">RsaDecrypt</span>
|
||||||
|
|
||||||
<p>Decrypt data with private key file useing ras algorithm.</p>
|
<p>Decrypt data with private key file useing ras algorithm.</p>
|
||||||
@@ -1017,14 +1088,17 @@ import (
|
|||||||
func main() {
|
func main() {
|
||||||
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
|
err := cryptor.GenerateRsaKey(4096, "rsa_private.pem", "rsa_public.pem")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data := []byte("hello world")
|
data := []byte("hello")
|
||||||
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
encrypted := cryptor.RsaEncrypt(data, "rsa_public.pem")
|
||||||
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
decrypted := cryptor.RsaDecrypt(encrypted, "rsa_private.pem")
|
||||||
|
|
||||||
fmt.Println(string(decrypted)) //hello world
|
fmt.Println(string(decrypted))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// hello
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
522
docs/strutil.md
522
docs/strutil.md
@@ -64,19 +64,27 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.After("lancet", "")
|
result1 := strutil.After("foo", "")
|
||||||
fmt.Println(s1) //lancet
|
result2 := strutil.After("foo", "foo")
|
||||||
|
result3 := strutil.After("foo/bar", "foo")
|
||||||
|
result4 := strutil.After("foo/bar", "/")
|
||||||
|
result5 := strutil.After("foo/bar/baz", "/")
|
||||||
|
|
||||||
s2 := strutil.After("github.com/test/lancet", "/")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //test/lancet
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
s3 := strutil.After("github.com/test/lancet", "test")
|
// Output:
|
||||||
fmt.Println(s3) // /lancet
|
// foo
|
||||||
|
//
|
||||||
|
// /bar
|
||||||
|
// bar
|
||||||
|
// bar/baz
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AfterLast">AfterLast</span>
|
### <span id="AfterLast">AfterLast</span>
|
||||||
<p>Returns the substring after the last occurrence of a specified string in the source string.</p>
|
<p>Returns the substring after the last occurrence of a specified string in the source string.</p>
|
||||||
|
|
||||||
@@ -94,20 +102,27 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.AfterLast("lancet", "")
|
result1 := strutil.AfterLast("foo", "")
|
||||||
fmt.Println(s1) //lancet
|
result2 := strutil.AfterLast("foo", "foo")
|
||||||
|
result3 := strutil.AfterLast("foo/bar", "/")
|
||||||
|
result4 := strutil.AfterLast("foo/bar/baz", "/")
|
||||||
|
result5 := strutil.AfterLast("foo/bar/foo/baz", "foo")
|
||||||
|
|
||||||
s2 := strutil.AfterLast("github.com/test/lancet", "/")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //lancet
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
s3 := strutil.AfterLast("github.com/test/test/lancet", "test")
|
// Output:
|
||||||
fmt.Println(s3) // /test/lancet
|
// foo
|
||||||
|
//
|
||||||
|
// bar
|
||||||
|
// baz
|
||||||
|
// /baz
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Before">Before</span>
|
### <span id="Before">Before</span>
|
||||||
<p>Returns the substring of the source string up to the first occurrence of the specified string.</p>
|
<p>Returns the substring of the source string up to the first occurrence of the specified string.</p>
|
||||||
|
|
||||||
@@ -125,20 +140,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.Before("lancet", "")
|
result1 := strutil.Before("foo", "")
|
||||||
fmt.Println(s1) //lancet
|
result2 := strutil.Before("foo", "foo")
|
||||||
|
result3 := strutil.Before("foo/bar", "/")
|
||||||
|
result4 := strutil.Before("foo/bar/baz", "/")
|
||||||
|
|
||||||
s2 := strutil.Before("github.com/test/lancet", "/")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //github.com
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
s3 := strutil.Before("github.com/test/lancet", "test")
|
// Output:
|
||||||
fmt.Println(s3) // github.com/
|
// foo
|
||||||
|
//
|
||||||
|
// foo
|
||||||
|
// foo
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeforeLast">BeforeLast</span>
|
### <span id="BeforeLast">BeforeLast</span>
|
||||||
<p>Returns the substring of the source string up to the last occurrence of the specified string.</p>
|
<p>Returns the substring of the source string up to the last occurrence of the specified string.</p>
|
||||||
|
|
||||||
@@ -156,18 +175,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.BeforeLast("lancet", "")
|
result1 := strutil.BeforeLast("foo", "")
|
||||||
fmt.Println(s1) //lancet
|
result2 := strutil.BeforeLast("foo", "foo")
|
||||||
|
result3 := strutil.BeforeLast("foo/bar", "/")
|
||||||
|
result4 := strutil.BeforeLast("foo/bar/baz", "/")
|
||||||
|
|
||||||
s2 := strutil.BeforeLast("github.com/test/lancet", "/")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //github.com/test
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
s3 := strutil.BeforeLast("github.com/test/test/lancet", "test")
|
// Output:
|
||||||
fmt.Println(s3) //github.com/test/
|
// foo
|
||||||
|
//
|
||||||
|
// foo
|
||||||
|
// foo/bar
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="CamelCase">CamelCase</span>
|
### <span id="CamelCase">CamelCase</span>
|
||||||
<p>Coverts string to camelCase string, non letters and numbers will be ignored.</p>
|
<p>Coverts string to camelCase string, non letters and numbers will be ignored.</p>
|
||||||
|
|
||||||
@@ -185,25 +210,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.CamelCase("foo_bar")
|
strings := []string{"", "foobar", "&FOO:BAR$BAZ", "$foo%", "Foo-#1😄$_%^&*(1bar"}
|
||||||
fmt.Println(s1) //fooBar
|
|
||||||
|
|
||||||
s2 := strutil.CamelCase("Foo-Bar")
|
for _, v := range strings {
|
||||||
fmt.Println(s2) //fooBar
|
s := strutil.CamelCase(v)
|
||||||
|
fmt.Println(s)
|
||||||
s3 := strutil.CamelCase("Foo&bar")
|
}
|
||||||
fmt.Println(s3) //fooBar
|
|
||||||
|
|
||||||
s4 := strutil.CamelCase("foo bar")
|
|
||||||
fmt.Println(s4) //fooBar
|
|
||||||
|
|
||||||
s4 := strutil.CamelCase("Foo-#1😄$_%^&*(1bar")
|
|
||||||
fmt.Println(s4) //foo11Bar
|
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
//
|
||||||
|
// foobar
|
||||||
|
// fooBarBaz
|
||||||
|
// foo
|
||||||
|
// foo11Bar
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="KebabCase">KebabCase</span>
|
### <span id="KebabCase">KebabCase</span>
|
||||||
<p>KebabCase covert string to kebab-case, non letters and numbers will be ignored.</p>
|
<p>KebabCase covert string to kebab-case, non letters and numbers will be ignored.</p>
|
||||||
|
|
||||||
@@ -221,29 +243,29 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.KebabCase("Foo Bar-")
|
strings := []string{"", "foo-bar", "Foo Bar-", "FOOBAR", "Foo-#1😄$_%^&*(1bar"}
|
||||||
fmt.Println(s1) //foo-bar
|
|
||||||
|
|
||||||
s2 := strutil.KebabCase("foo_Bar")
|
for _, v := range strings {
|
||||||
fmt.Println(s2) //foo-bar
|
s := strutil.KebabCase(v)
|
||||||
|
fmt.Println(s)
|
||||||
|
}
|
||||||
|
|
||||||
s3 := strutil.KebabCase("fooBar")
|
// Output:
|
||||||
fmt.Println(s3) //foo-bar
|
//
|
||||||
|
// foo-bar
|
||||||
s4 := strutil.KebabCase("__FOO_BAR__")
|
// foo-bar
|
||||||
fmt.Println(s4) //foo-bar
|
// foobar
|
||||||
|
// foo-1-1-bar
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UpperKebabCase">UpperKebabCase</span>
|
### <span id="UpperKebabCase">UpperKebabCase</span>
|
||||||
<p>UpperKebabCase covert string to upper KEBAB-CASE, non letters and numbers will be ignored.</p>
|
<p>UpperKebabCase covert string to upper KEBAB-CASE, non letters and numbers will be ignored.</p>
|
||||||
|
|
||||||
<b>Signature:</b>
|
<b>Signature:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func KebabCase(s string) string
|
func UpperKebabCase(s string) string
|
||||||
```
|
```
|
||||||
<b>Example:</b>
|
<b>Example:</b>
|
||||||
|
|
||||||
@@ -254,23 +276,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.UpperKebabCase("Foo Bar-")
|
strings := []string{"", "foo-bar", "Foo Bar-", "FooBAR", "Foo-#1😄$_%^&*(1bar"}
|
||||||
fmt.Println(s1) //FOO-BAR
|
|
||||||
|
|
||||||
s2 := strutil.UpperKebabCase("foo_Bar")
|
for _, v := range strings {
|
||||||
fmt.Println(s2) //FOO-BAR
|
s := strutil.UpperKebabCase(v)
|
||||||
|
fmt.Println(s)
|
||||||
|
}
|
||||||
|
|
||||||
s3 := strutil.UpperKebabCase("fooBar")
|
// Output:
|
||||||
fmt.Println(s3) //FOO-BAR
|
//
|
||||||
|
// FOO-BAR
|
||||||
s4 := strutil.UpperKebabCase("__FOO_BAR__")
|
// FOO-BAR
|
||||||
fmt.Println(s4) //FOO-BAR
|
// FOO-BAR
|
||||||
|
// FOO-1-1-BAR
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Capitalize">Capitalize</span>
|
### <span id="Capitalize">Capitalize</span>
|
||||||
<p>Convert the first character of a string to upper case.</p>
|
<p>Convert the first character of a string to upper case.</p>
|
||||||
|
|
||||||
@@ -288,19 +309,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.Capitalize("foo")
|
strings := []string{"", "Foo", "_foo", "fooBar", "foo-bar"}
|
||||||
fmt.Println(s1) //foo
|
|
||||||
|
|
||||||
s2 := strutil.Capitalize("Foo")
|
for _, v := range strings {
|
||||||
fmt.Println(s2) //foo
|
s := strutil.Capitalize(v)
|
||||||
|
fmt.Println(s)
|
||||||
|
}
|
||||||
|
|
||||||
s3 := strutil.Capitalize("FOo"
|
// Output:
|
||||||
fmt.Println(s3) //fOo
|
//
|
||||||
|
// Foo
|
||||||
|
// _foo
|
||||||
|
// Foobar
|
||||||
|
// Foo-bar
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsString">IsString</span>
|
### <span id="IsString">IsString</span>
|
||||||
<p>Check if the value's data type is string.</p>
|
<p>Check if the value's data type is string.</p>
|
||||||
|
|
||||||
@@ -318,51 +342,27 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println(strutil.IsString("lancet")) //true
|
result1 := strutil.IsString("")
|
||||||
fmt.Println(strutil.IsString("")) //true
|
result2 := strutil.IsString("a")
|
||||||
|
result3 := strutil.IsString(1)
|
||||||
|
result4 := strutil.IsString(true)
|
||||||
|
result5 := strutil.IsString([]string{"a"})
|
||||||
|
|
||||||
fmt.Println(strutil.IsString(1)) //false
|
fmt.Println(result1)
|
||||||
fmt.Println(strutil.IsString("")) //false
|
fmt.Println(result2)
|
||||||
fmt.Println(strutil.IsString([]string{})) //false
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="KebabCase">KebabCase</span>
|
|
||||||
<p>Covert string to kebab-case.</p>
|
|
||||||
|
|
||||||
<b>Signature:</b>
|
|
||||||
|
|
||||||
```go
|
|
||||||
func KebabCase(s string) string
|
|
||||||
```
|
|
||||||
<b>Example:</b>
|
|
||||||
|
|
||||||
```go
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/duke-git/lancet/v2/strutil"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
s1 := strutil.KebabCase("Foo Bar-")
|
|
||||||
fmt.Println(s1) //foo-bar
|
|
||||||
|
|
||||||
s2 := strutil.KebabCase("foo_Bar")
|
|
||||||
fmt.Println(s2) //foo-bar
|
|
||||||
|
|
||||||
s3 := strutil.KebabCase("fooBar")
|
|
||||||
fmt.Println(s3) //foo-bar
|
|
||||||
|
|
||||||
s4 := strutil.KebabCase("__FOO_BAR__")
|
|
||||||
fmt.Println(s4) //f-o-o-b-a-r
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="LowerFirst">LowerFirst</span>
|
### <span id="LowerFirst">LowerFirst</span>
|
||||||
<p>Convert the first character of string to lower case.</p>
|
<p>Convert the first character of string to lower case.</p>
|
||||||
|
|
||||||
@@ -380,23 +380,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.LowerFirst("foo")
|
strings := []string{"", "bar", "BAr", "Bar大"}
|
||||||
fmt.Println(s1) //foo
|
|
||||||
|
|
||||||
s2 := strutil.LowerFirst("BAR")
|
for _, v := range strings {
|
||||||
fmt.Println(s2) //bAR
|
s := strutil.LowerFirst(v)
|
||||||
|
fmt.Println(s)
|
||||||
|
}
|
||||||
|
|
||||||
s3 := strutil.LowerFirst("FOo")
|
// Output:
|
||||||
fmt.Println(s3) //fOo
|
//
|
||||||
|
// bar
|
||||||
s4 := strutil.LowerFirst("fOo大")
|
// bAr
|
||||||
fmt.Println(s4) //fOo大
|
// bar大
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UpperFirst">UpperFirst</span>
|
### <span id="UpperFirst">UpperFirst</span>
|
||||||
<p>Convert the first character of string to upper case.</p>
|
<p>Convert the first character of string to upper case.</p>
|
||||||
|
|
||||||
@@ -414,23 +412,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.UpperFirst("foo")
|
strings := []string{"", "bar", "BAr", "bar大"}
|
||||||
fmt.Println(s1) //Foo
|
|
||||||
|
|
||||||
s2 := strutil.UpperFirst("bAR")
|
for _, v := range strings {
|
||||||
fmt.Println(s2) //BAR
|
s := strutil.UpperFirst(v)
|
||||||
|
fmt.Println(s)
|
||||||
|
}
|
||||||
|
|
||||||
s3 := strutil.UpperFirst("FOo")
|
// Output:
|
||||||
fmt.Println(s3) //FOo
|
//
|
||||||
|
// Bar
|
||||||
s4 := strutil.UpperFirst("fOo大")
|
// BAr
|
||||||
fmt.Println(s4) //FOo大
|
// Bar大
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="PadEnd">PadEnd</span>
|
### <span id="PadEnd">PadEnd</span>
|
||||||
<p>Pads string on the right side if it's shorter than size.</p>
|
<p>Pads string on the right side if it's shorter than size.</p>
|
||||||
|
|
||||||
@@ -448,23 +444,33 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.PadEnd("a", 1, "b")
|
result1 := strutil.PadEnd("foo", 1, "bar")
|
||||||
fmt.Println(s1) //a
|
result2 := strutil.PadEnd("foo", 2, "bar")
|
||||||
|
result3 := strutil.PadEnd("foo", 3, "bar")
|
||||||
|
result4 := strutil.PadEnd("foo", 4, "bar")
|
||||||
|
result5 := strutil.PadEnd("foo", 5, "bar")
|
||||||
|
result6 := strutil.PadEnd("foo", 6, "bar")
|
||||||
|
result7 := strutil.PadEnd("foo", 7, "bar")
|
||||||
|
|
||||||
s2 := strutil.PadEnd("a", 2, "b")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //ab
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
s3 := strutil.PadEnd("abcd", 6, "mno")
|
// Output:
|
||||||
fmt.Println(s3) //abcdmn
|
// foo
|
||||||
|
// foo
|
||||||
s4 := strutil.PadEnd("abc", 6, "ab")
|
// foo
|
||||||
fmt.Println(s4) //abcaba
|
// foob
|
||||||
|
// fooba
|
||||||
|
// foobar
|
||||||
|
// foobarb
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="PadStart">PadStart</span>
|
### <span id="PadStart">PadStart</span>
|
||||||
<p>Pads string on the left side if it's shorter than size.</p>
|
<p>Pads string on the left side if it's shorter than size.</p>
|
||||||
|
|
||||||
@@ -482,23 +488,33 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.PadStart("a", 1, "b")
|
result1 := strutil.PadStart("foo", 1, "bar")
|
||||||
fmt.Println(s1) //a
|
result2 := strutil.PadStart("foo", 2, "bar")
|
||||||
|
result3 := strutil.PadStart("foo", 3, "bar")
|
||||||
|
result4 := strutil.PadStart("foo", 4, "bar")
|
||||||
|
result5 := strutil.PadStart("foo", 5, "bar")
|
||||||
|
result6 := strutil.PadStart("foo", 6, "bar")
|
||||||
|
result7 := strutil.PadStart("foo", 7, "bar")
|
||||||
|
|
||||||
s2 := strutil.PadStart("a", 2, "b")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //ba
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
s3 := strutil.PadStart("abcd", 6, "mno")
|
// Output:
|
||||||
fmt.Println(s3) //mnabcd
|
// foo
|
||||||
|
// foo
|
||||||
s4 := strutil.PadStart("abc", 6, "ab")
|
// foo
|
||||||
fmt.Println(s4) //abaabc
|
// bfoo
|
||||||
|
// bafoo
|
||||||
|
// barfoo
|
||||||
|
// barbfoo
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Reverse">Reverse</span>
|
### <span id="Reverse">Reverse</span>
|
||||||
<p>Return string whose char order is reversed to the given string.</p>
|
<p>Return string whose char order is reversed to the given string.</p>
|
||||||
|
|
||||||
@@ -516,16 +532,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.ReverseStr("abc")
|
s := "foo"
|
||||||
fmt.Println(s1) //cba
|
rs := strutil.Reverse(s)
|
||||||
|
|
||||||
s2 := strutil.ReverseStr("12345")
|
fmt.Println(s)
|
||||||
fmt.Println(s2) //54321
|
fmt.Println(rs)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// foo
|
||||||
|
// oof
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SnakeCase">SnakeCase</span>
|
### <span id="SnakeCase">SnakeCase</span>
|
||||||
<p>Coverts string to snake_case, non letters and numbers will be ignored.</p>
|
<p>Coverts string to snake_case, non letters and numbers will be ignored.</p>
|
||||||
|
|
||||||
@@ -543,24 +561,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.SnakeCase("Foo Bar-")
|
strings := []string{"", "foo-bar", "Foo Bar-", "FOOBAR", "Foo-#1😄$_%^&*(1bar"}
|
||||||
fmt.Println(s1) //foo_bar
|
|
||||||
|
|
||||||
s2 := strutil.SnakeCase("foo_Bar")
|
for _, v := range strings {
|
||||||
fmt.Println(s2) //foo_bar
|
s := strutil.SnakeCase(v)
|
||||||
|
fmt.Println(s)
|
||||||
|
}
|
||||||
|
|
||||||
s3 := strutil.SnakeCase("fooBar")
|
// Output:
|
||||||
fmt.Println(s3) //foo_bar
|
//
|
||||||
|
// foo_bar
|
||||||
s4 := strutil.SnakeCase("__FOO_BAR__")
|
// foo_bar
|
||||||
fmt.Println(s4) //foo_bar
|
// foobar
|
||||||
|
// foo_1_1_bar
|
||||||
s5 := strutil.SnakeCase("Foo-#1😄$_%^&*(1bar")
|
|
||||||
fmt.Println(s5) //foo_1_1_bar
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="UpperSnakeCase">UpperSnakeCase</span>
|
### <span id="UpperSnakeCase">UpperSnakeCase</span>
|
||||||
<p>Coverts string to upper KEBAB-CASE, non letters and numbers will be ignored.</p>
|
<p>Coverts string to upper KEBAB-CASE, non letters and numbers will be ignored.</p>
|
||||||
|
|
||||||
@@ -578,24 +594,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.UpperSnakeCase("Foo Bar-")
|
strings := []string{"", "foo-bar", "Foo Bar-", "FooBAR", "Foo-#1😄$_%^&*(1bar"}
|
||||||
fmt.Println(s1) //FOO_BAR
|
|
||||||
|
|
||||||
s2 := strutil.UpperSnakeCase("foo_Bar")
|
for _, v := range strings {
|
||||||
fmt.Println(s2) //FOO_BAR
|
s := strutil.UpperSnakeCase(v)
|
||||||
|
fmt.Println(s)
|
||||||
|
}
|
||||||
|
|
||||||
s3 := strutil.UpperSnakeCase("fooBar")
|
// Output:
|
||||||
fmt.Println(s3) //FOO_BAR
|
//
|
||||||
|
// FOO_BAR
|
||||||
s4 := strutil.UpperSnakeCase("__FOO_BAR__")
|
// FOO_BAR
|
||||||
fmt.Println(s4) //FOO_BAR
|
// FOO_BAR
|
||||||
|
// FOO_1_1_BAR
|
||||||
s5 := strutil.UpperSnakeCase("Foo-#1😄$_%^&*(1bar")
|
|
||||||
fmt.Println(s5) //FOO_1_1_BAR
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="SplitEx">SplitEx</span>
|
### <span id="SplitEx">SplitEx</span>
|
||||||
<p>Split a given string whether the result contains empty string.</p>
|
<p>Split a given string whether the result contains empty string.</p>
|
||||||
|
|
||||||
@@ -613,25 +627,28 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
arr1 := strutil.SplitEx(" a b c ", "", true)
|
result1 := strutil.SplitEx(" a b c ", "", true)
|
||||||
fmt.Println(arr1) //[]string{}
|
|
||||||
|
|
||||||
arr2 := strutil.SplitEx(" a b c ", " ", false)
|
result2 := strutil.SplitEx(" a b c ", " ", false)
|
||||||
fmt.Println(arr2) //[]string{"", "a", "b", "c", ""}
|
result3 := strutil.SplitEx(" a b c ", " ", true)
|
||||||
|
|
||||||
arr3 := strutil.SplitEx(" a b c ", " ", true)
|
result4 := strutil.SplitEx("a = b = c = ", " = ", false)
|
||||||
fmt.Println(arr3) //[]string{"a", "b", "c"}
|
result5 := strutil.SplitEx("a = b = c = ", " = ", true)
|
||||||
|
|
||||||
arr4 := strutil.SplitEx(" a = b = c = ", " = ", false)
|
fmt.Println(result1)
|
||||||
fmt.Println(arr4) //[]string{" a", "b", "c", ""}
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
arr5 := strutil.SplitEx(" a = b = c = ", " = ", true)
|
// Output:
|
||||||
fmt.Println(arr5) //[]string{" a", "b", "c"}
|
// []
|
||||||
|
// [ a b c ]
|
||||||
|
// [a b c]
|
||||||
|
// [a b c ]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Substring">Substring</span>
|
### <span id="Substring">Substring</span>
|
||||||
<p>Returns a substring of the specified length starting at the specified offset position.</p>
|
<p>Returns a substring of the specified length starting at the specified offset position.</p>
|
||||||
|
|
||||||
@@ -650,22 +667,26 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.Substring("abcde", 1, 3)
|
result1 := strutil.Substring("abcde", 1, 3)
|
||||||
fmt.Println(result1) //bcd
|
|
||||||
|
|
||||||
result2 := strutil.Substring("abcde", 1, 5)
|
result2 := strutil.Substring("abcde", 1, 5)
|
||||||
fmt.Println(result2) //bcde
|
|
||||||
|
|
||||||
result3 := strutil.Substring("abcde", -1, 3)
|
result3 := strutil.Substring("abcde", -1, 3)
|
||||||
fmt.Println(result3) //e
|
|
||||||
|
|
||||||
result4 := strutil.Substring("abcde", -2, 2)
|
result4 := strutil.Substring("abcde", -2, 2)
|
||||||
fmt.Println(result4) //de
|
|
||||||
|
|
||||||
result5 := strutil.Substring("abcde", -2, 3)
|
result5 := strutil.Substring("abcde", -2, 3)
|
||||||
fmt.Println(result5) //de
|
|
||||||
|
|
||||||
result6 := strutil.Substring("你好,欢迎你", 0, 2)
|
result6 := strutil.Substring("你好,欢迎你", 0, 2)
|
||||||
fmt.Println(result6) //你好
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// bcd
|
||||||
|
// bcde
|
||||||
|
// e
|
||||||
|
// de
|
||||||
|
// de
|
||||||
|
// 你好
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -686,26 +707,25 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.Wrap("ab", "")
|
result1 := strutil.Wrap("foo", "")
|
||||||
fmt.Println(s1) //ab
|
result2 := strutil.Wrap("foo", "*")
|
||||||
|
result3 := strutil.Wrap("'foo'", "'")
|
||||||
|
result4 := strutil.Wrap("", "*")
|
||||||
|
|
||||||
s2 := strutil.Wrap("", "*")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //""
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
s3 := strutil.Wrap("ab", "*")
|
// Output:
|
||||||
fmt.Println(s3) //*ab*
|
// foo
|
||||||
|
// *foo*
|
||||||
s4 := strutil.Wrap("ab", "\"")
|
// ''foo''
|
||||||
fmt.Println(s4) //\"ab\"
|
//
|
||||||
|
|
||||||
s5 := strutil.Wrap("ab", "'")
|
|
||||||
fmt.Println(s5) //'ab'
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Wrap">Wrap</span>
|
### <span id="Wrap">Wrap</span>
|
||||||
<p>Unwrap a given string from anther string. will change source string.</p>
|
<p>Unwrap a given string from anther string. will change source string.</p>
|
||||||
|
|
||||||
@@ -723,20 +743,24 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.Unwrap("ab", "")
|
result1 := strutil.Unwrap("foo", "")
|
||||||
fmt.Println(s1) //ab
|
result2 := strutil.Unwrap("*foo*", "*")
|
||||||
|
result3 := strutil.Unwrap("*foo", "*")
|
||||||
|
result4 := strutil.Unwrap("foo*", "*")
|
||||||
|
result5 := strutil.Unwrap("**foo**", "*")
|
||||||
|
|
||||||
s2 := strutil.Unwrap("ab", "*")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //ab
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
s3 := strutil.Unwrap("**ab**", "*")
|
// Output:
|
||||||
fmt.Println(s3) //*ab*
|
// foo
|
||||||
|
// foo
|
||||||
s4 := strutil.Unwrap("*ab", "*")
|
// *foo
|
||||||
fmt.Println(s4) //*ab
|
// foo*
|
||||||
|
// *foo*
|
||||||
s5 := strutil.Unwrap("***", "**")
|
|
||||||
fmt.Println(s5) //***
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@@ -49,14 +49,14 @@ import (
|
|||||||
|
|
||||||
|
|
||||||
### <span id="After">After</span>
|
### <span id="After">After</span>
|
||||||
<p>返回源字符串中特定字符串首次出现时的位置之后的子字符串</p>
|
<p>返回源字符串中特定字符串首次出现时的位置之后的子字符串。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func After(s, char string) string
|
func After(s, char string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -65,28 +65,36 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.After("lancet", "")
|
result1 := strutil.After("foo", "")
|
||||||
fmt.Println(s1) //lancet
|
result2 := strutil.After("foo", "foo")
|
||||||
|
result3 := strutil.After("foo/bar", "foo")
|
||||||
|
result4 := strutil.After("foo/bar", "/")
|
||||||
|
result5 := strutil.After("foo/bar/baz", "/")
|
||||||
|
|
||||||
s2 := strutil.After("github.com/test/lancet", "/")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //test/lancet
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
s3 := strutil.After("github.com/test/lancet", "test")
|
// Output:
|
||||||
fmt.Println(s3) // /lancet
|
// foo
|
||||||
|
//
|
||||||
|
// /bar
|
||||||
|
// bar
|
||||||
|
// bar/baz
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="AfterLast">AfterLast</span>
|
### <span id="AfterLast">AfterLast</span>
|
||||||
<p>返回源字符串中指定字符串最后一次出现时的位置之后的子字符串</p>
|
<p>返回源字符串中指定字符串最后一次出现时的位置之后的子字符串。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func AfterLast(s, char string) string
|
func AfterLast(s, char string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -95,29 +103,36 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.AfterLast("lancet", "")
|
result1 := strutil.AfterLast("foo", "")
|
||||||
fmt.Println(s1) //lancet
|
result2 := strutil.AfterLast("foo", "foo")
|
||||||
|
result3 := strutil.AfterLast("foo/bar", "/")
|
||||||
|
result4 := strutil.AfterLast("foo/bar/baz", "/")
|
||||||
|
result5 := strutil.AfterLast("foo/bar/foo/baz", "foo")
|
||||||
|
|
||||||
s2 := strutil.AfterLast("github.com/test/lancet", "/")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //lancet
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
s3 := strutil.AfterLast("github.com/test/test/lancet", "test")
|
// Output:
|
||||||
fmt.Println(s3) // /lancet
|
// foo
|
||||||
|
//
|
||||||
|
// bar
|
||||||
|
// baz
|
||||||
|
// /baz
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Before">Before</span>
|
### <span id="Before">Before</span>
|
||||||
<p>返回源字符串中指定字符串第一次出现时的位置之前的子字符串</p>
|
<p>返回源字符串中指定字符串第一次出现时的位置之前的子字符串。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func Before(s, char string) string
|
func Before(s, char string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -126,29 +141,33 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.Before("lancet", "")
|
result1 := strutil.Before("foo", "")
|
||||||
fmt.Println(s1) //lancet
|
result2 := strutil.Before("foo", "foo")
|
||||||
|
result3 := strutil.Before("foo/bar", "/")
|
||||||
|
result4 := strutil.Before("foo/bar/baz", "/")
|
||||||
|
|
||||||
s2 := strutil.Before("github.com/test/lancet", "/")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //github.com
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
s3 := strutil.Before("github.com/test/lancet", "test")
|
// Output:
|
||||||
fmt.Println(s3) // github.com/
|
// foo
|
||||||
|
//
|
||||||
|
// foo
|
||||||
|
// foo
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="BeforeLast">BeforeLast</span>
|
### <span id="BeforeLast">BeforeLast</span>
|
||||||
<p>返回源字符串中指定字符串最后一次出现时的位置之前的子字符串</p>
|
<p>返回源字符串中指定字符串最后一次出现时的位置之前的子字符串。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func BeforeLast(s, char string) string
|
func BeforeLast(s, char string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -157,29 +176,33 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.BeforeLast("lancet", "")
|
result1 := strutil.BeforeLast("foo", "")
|
||||||
fmt.Println(s1) //lancet
|
result2 := strutil.BeforeLast("foo", "foo")
|
||||||
|
result3 := strutil.BeforeLast("foo/bar", "/")
|
||||||
|
result4 := strutil.BeforeLast("foo/bar/baz", "/")
|
||||||
|
|
||||||
s2 := strutil.BeforeLast("github.com/test/lancet", "/")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //github.com/test
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
|
||||||
s3 := strutil.BeforeLast("github.com/test/test/lancet", "test")
|
// Output:
|
||||||
fmt.Println(s3) //github.com/test/
|
// foo
|
||||||
|
//
|
||||||
|
// foo
|
||||||
|
// foo/bar
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="CamelCase">CamelCase</span>
|
### <span id="CamelCase">CamelCase</span>
|
||||||
<p>将字符串转换为驼峰式字符串, 非字母和数字会被忽略</p>
|
<p>将字符串转换为驼峰式字符串, 非字母和数字会被忽略。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func CamelCase(s string) string
|
func CamelCase(s string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -188,35 +211,97 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.CamelCase("foo_bar")
|
strings := []string{"", "foobar", "&FOO:BAR$BAZ", "$foo%", "Foo-#1😄$_%^&*(1bar"}
|
||||||
fmt.Println(s1) //fooBar
|
|
||||||
|
|
||||||
s2 := strutil.CamelCase("Foo-Bar")
|
for _, v := range strings {
|
||||||
fmt.Println(s2) //fooBar
|
s := strutil.CamelCase(v)
|
||||||
|
fmt.Println(s)
|
||||||
|
}
|
||||||
|
|
||||||
s3 := strutil.CamelCase("Foo&bar")
|
// Output:
|
||||||
fmt.Println(s3) //fooBar
|
//
|
||||||
|
// foobar
|
||||||
s4 := strutil.CamelCase("foo bar")
|
// fooBarBaz
|
||||||
fmt.Println(s4) //fooBar
|
// foo
|
||||||
|
// foo11Bar
|
||||||
s4 := strutil.CamelCase("Foo-#1😄$_%^&*(1bar")
|
|
||||||
fmt.Println(s4) //foo11Bar
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="KebabCase">KebabCase</span>
|
||||||
|
<p>将字符串转换为kebab-case, 非字母和数字会被忽略。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func KebabCase(s string) string
|
||||||
|
```
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
strings := []string{"", "foo-bar", "Foo Bar-", "FOOBAR", "Foo-#1😄$_%^&*(1bar"}
|
||||||
|
|
||||||
|
for _, v := range strings {
|
||||||
|
s := strutil.KebabCase(v)
|
||||||
|
fmt.Println(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
//
|
||||||
|
// foo-bar
|
||||||
|
// foo-bar
|
||||||
|
// foobar
|
||||||
|
// foo-1-1-bar
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="UpperKebabCase">UpperKebabCase</span>
|
||||||
|
<p>将字符串转换为大写KEBAB-CASE, 非字母和数字会被忽略。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func UpperKebabCase(s string) string
|
||||||
|
```
|
||||||
|
<b>示例:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/strutil"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
strings := []string{"", "foo-bar", "Foo Bar-", "FooBAR", "Foo-#1😄$_%^&*(1bar"}
|
||||||
|
|
||||||
|
for _, v := range strings {
|
||||||
|
s := strutil.UpperKebabCase(v)
|
||||||
|
fmt.Println(s)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
//
|
||||||
|
// FOO-BAR
|
||||||
|
// FOO-BAR
|
||||||
|
// FOO-BAR
|
||||||
|
// FOO-1-1-BAR
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="Capitalize">Capitalize</span>
|
### <span id="Capitalize">Capitalize</span>
|
||||||
<p>将字符串的第一个字符转换为大写</p>
|
<p>将字符串的第一个字符转换为大写。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func Capitalize(s string) string
|
func Capitalize(s string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -225,28 +310,31 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.Capitalize("foo")
|
strings := []string{"", "Foo", "_foo", "fooBar", "foo-bar"}
|
||||||
fmt.Println(s1) //foo
|
|
||||||
|
|
||||||
s2 := strutil.Capitalize("Foo")
|
for _, v := range strings {
|
||||||
fmt.Println(s2) //foo
|
s := strutil.Capitalize(v)
|
||||||
|
fmt.Println(s)
|
||||||
|
}
|
||||||
|
|
||||||
s3 := strutil.Capitalize("FOo"
|
// Output:
|
||||||
fmt.Println(s3) //fOo
|
//
|
||||||
|
// Foo
|
||||||
|
// _foo
|
||||||
|
// Foobar
|
||||||
|
// Foo-bar
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="IsString">IsString</span>
|
### <span id="IsString">IsString</span>
|
||||||
<p>判断传入参数的数据类型是否为字符串</p>
|
<p>判断传入参数的数据类型是否为字符串。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func IsString(v any) bool
|
func IsString(v any) bool
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -255,93 +343,36 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println(strutil.IsString("lancet")) //true
|
result1 := strutil.IsString("")
|
||||||
fmt.Println(strutil.IsString("")) //true
|
result2 := strutil.IsString("a")
|
||||||
|
result3 := strutil.IsString(1)
|
||||||
|
result4 := strutil.IsString(true)
|
||||||
|
result5 := strutil.IsString([]string{"a"})
|
||||||
|
|
||||||
fmt.Println(strutil.IsString(1)) //false
|
fmt.Println(result1)
|
||||||
fmt.Println(strutil.IsString("")) //false
|
fmt.Println(result2)
|
||||||
fmt.Println(strutil.IsString([]string{})) //false
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
// false
|
||||||
|
// false
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="KebabCase">KebabCase</span>
|
|
||||||
<p>将字符串转换为kebab-case, 非字母和数字会被忽略</p>
|
|
||||||
|
|
||||||
<b>函数签名:</b>
|
|
||||||
|
|
||||||
```go
|
|
||||||
func KebabCase(s string) string
|
|
||||||
```
|
|
||||||
<b>例子:</b>
|
|
||||||
|
|
||||||
```go
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/duke-git/lancet/v2/strutil"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
s1 := strutil.KebabCase("Foo Bar-")
|
|
||||||
fmt.Println(s1) //foo-bar
|
|
||||||
|
|
||||||
s2 := strutil.KebabCase("foo_Bar")
|
|
||||||
fmt.Println(s2) //foo-bar
|
|
||||||
|
|
||||||
s3 := strutil.KebabCase("fooBar")
|
|
||||||
fmt.Println(s3) //foo-bar
|
|
||||||
|
|
||||||
s4 := strutil.KebabCase("__FOO_BAR__")
|
|
||||||
fmt.Println(s4) //foo-bar
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UpperKebabCase">UpperKebabCase</span>
|
|
||||||
<p>将字符串转换为大写KEBAB-CASE, 非字母和数字会被忽略</p>
|
|
||||||
|
|
||||||
<b>函数签名:</b>
|
|
||||||
|
|
||||||
```go
|
|
||||||
func KebabCase(s string) string
|
|
||||||
```
|
|
||||||
<b>例子:</b>
|
|
||||||
|
|
||||||
```go
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/duke-git/lancet/v2/strutil"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
s1 := strutil.UpperKebabCase("Foo Bar-")
|
|
||||||
fmt.Println(s1) //FOO-BAR
|
|
||||||
|
|
||||||
s2 := strutil.UpperKebabCase("foo_Bar")
|
|
||||||
fmt.Println(s2) //FOO-BAR
|
|
||||||
|
|
||||||
s3 := strutil.UpperKebabCase("fooBar")
|
|
||||||
fmt.Println(s3) //FOO-BAR
|
|
||||||
|
|
||||||
s4 := strutil.UpperKebabCase("__FOO_BAR__")
|
|
||||||
fmt.Println(s4) //FOO-BAR
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="LowerFirst">LowerFirst</span>
|
### <span id="LowerFirst">LowerFirst</span>
|
||||||
<p>将字符串的第一个字符转换为小写</p>
|
<p>将字符串的第一个字符转换为小写。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func LowerFirst(s string) string
|
func LowerFirst(s string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -350,32 +381,30 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.LowerFirst("foo")
|
strings := []string{"", "bar", "BAr", "Bar大"}
|
||||||
fmt.Println(s1) //foo
|
|
||||||
|
|
||||||
s2 := strutil.LowerFirst("BAR")
|
for _, v := range strings {
|
||||||
fmt.Println(s2) //bAR
|
s := strutil.LowerFirst(v)
|
||||||
|
fmt.Println(s)
|
||||||
|
}
|
||||||
|
|
||||||
s3 := strutil.LowerFirst("FOo")
|
// Output:
|
||||||
fmt.Println(s3) //fOo
|
//
|
||||||
|
// bar
|
||||||
s4 := strutil.LowerFirst("fOo大")
|
// bAr
|
||||||
fmt.Println(s4) //fOo大
|
// bar大
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="UpperFirst">UpperFirst</span>
|
### <span id="UpperFirst">UpperFirst</span>
|
||||||
<p>将字符串的第一个字符转换为大写形式</p>
|
<p>将字符串的第一个字符转换为大写形式。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func UpperFirst(s string) string
|
func UpperFirst(s string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -384,32 +413,30 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.UpperFirst("foo")
|
strings := []string{"", "bar", "BAr", "bar大"}
|
||||||
fmt.Println(s1) //Foo
|
|
||||||
|
|
||||||
s2 := strutil.UpperFirst("bAR")
|
for _, v := range strings {
|
||||||
fmt.Println(s2) //BAR
|
s := strutil.UpperFirst(v)
|
||||||
|
fmt.Println(s)
|
||||||
|
}
|
||||||
|
|
||||||
s3 := strutil.UpperFirst("FOo")
|
// Output:
|
||||||
fmt.Println(s3) //FOo
|
//
|
||||||
|
// Bar
|
||||||
s4 := strutil.UpperFirst("fOo大")
|
// BAr
|
||||||
fmt.Println(s4) //FOo大
|
// Bar大
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="PadEnd">PadEnd</span>
|
### <span id="PadEnd">PadEnd</span>
|
||||||
<p>如果字符串长度短于size,则在右侧填充字符串</p>
|
<p>如果字符串长度短于size,则在右侧填充字符串。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func PadEnd(source string, size int, padStr string) string
|
func PadEnd(source string, size int, padStr string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -418,32 +445,42 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.PadEnd("a", 1, "b")
|
result1 := strutil.PadEnd("foo", 1, "bar")
|
||||||
fmt.Println(s1) //a
|
result2 := strutil.PadEnd("foo", 2, "bar")
|
||||||
|
result3 := strutil.PadEnd("foo", 3, "bar")
|
||||||
|
result4 := strutil.PadEnd("foo", 4, "bar")
|
||||||
|
result5 := strutil.PadEnd("foo", 5, "bar")
|
||||||
|
result6 := strutil.PadEnd("foo", 6, "bar")
|
||||||
|
result7 := strutil.PadEnd("foo", 7, "bar")
|
||||||
|
|
||||||
s2 := strutil.PadEnd("a", 2, "b")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //ab
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
s3 := strutil.PadEnd("abcd", 6, "mno")
|
// Output:
|
||||||
fmt.Println(s3) //abcdmn
|
// foo
|
||||||
|
// foo
|
||||||
s4 := strutil.PadEnd("abc", 6, "ab")
|
// foo
|
||||||
fmt.Println(s4) //abcaba
|
// foob
|
||||||
|
// fooba
|
||||||
|
// foobar
|
||||||
|
// foobarb
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="PadStart">PadStart</span>
|
### <span id="PadStart">PadStart</span>
|
||||||
<p>如果字符串长度短于size,则在左侧填充字符串</p>
|
<p>如果字符串长度短于size,则在左侧填充字符串。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func PadStart(source string, size int, padStr string) string
|
func PadStart(source string, size int, padStr string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -452,32 +489,42 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.PadStart("a", 1, "b")
|
result1 := strutil.PadStart("foo", 1, "bar")
|
||||||
fmt.Println(s1) //a
|
result2 := strutil.PadStart("foo", 2, "bar")
|
||||||
|
result3 := strutil.PadStart("foo", 3, "bar")
|
||||||
|
result4 := strutil.PadStart("foo", 4, "bar")
|
||||||
|
result5 := strutil.PadStart("foo", 5, "bar")
|
||||||
|
result6 := strutil.PadStart("foo", 6, "bar")
|
||||||
|
result7 := strutil.PadStart("foo", 7, "bar")
|
||||||
|
|
||||||
s2 := strutil.PadStart("a", 2, "b")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //ba
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
fmt.Println(result7)
|
||||||
|
|
||||||
s3 := strutil.PadStart("abcd", 6, "mno")
|
// Output:
|
||||||
fmt.Println(s3) //mnabcd
|
// foo
|
||||||
|
// foo
|
||||||
s4 := strutil.PadStart("abc", 6, "ab")
|
// foo
|
||||||
fmt.Println(s4) //abaabc
|
// bfoo
|
||||||
|
// bafoo
|
||||||
|
// barfoo
|
||||||
|
// barbfoo
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="Reverse">Reverse</span>
|
### <span id="Reverse">Reverse</span>
|
||||||
<p>返回字符顺序与给定字符串相反的字符串</p>
|
<p>返回字符顺序与给定字符串相反的字符串。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func Reverse(s string) string
|
func Reverse(s string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -486,25 +533,27 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.ReverseStr("abc")
|
s := "foo"
|
||||||
fmt.Println(s1) //cba
|
rs := strutil.Reverse(s)
|
||||||
|
|
||||||
s2 := strutil.ReverseStr("12345")
|
fmt.Println(s)
|
||||||
fmt.Println(s2) //54321
|
fmt.Println(rs)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// foo
|
||||||
|
// oof
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SnakeCase">SnakeCase</span>
|
### <span id="SnakeCase">SnakeCase</span>
|
||||||
<p>将字符串转换为snake_case形式, 非字母和数字会被忽略</p>
|
<p>将字符串转换为snake_case形式, 非字母和数字会被忽略。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func SnakeCase(s string) string
|
func SnakeCase(s string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -513,33 +562,31 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.SnakeCase("Foo Bar-")
|
strings := []string{"", "foo-bar", "Foo Bar-", "FOOBAR", "Foo-#1😄$_%^&*(1bar"}
|
||||||
fmt.Println(s1) //foo_bar
|
|
||||||
|
|
||||||
s2 := strutil.SnakeCase("foo_Bar")
|
for _, v := range strings {
|
||||||
fmt.Println(s2) //foo_bar
|
s := strutil.SnakeCase(v)
|
||||||
|
fmt.Println(s)
|
||||||
|
}
|
||||||
|
|
||||||
s3 := strutil.SnakeCase("fooBar")
|
// Output:
|
||||||
fmt.Println(s3) //foo_bar
|
//
|
||||||
|
// foo_bar
|
||||||
s4 := strutil.SnakeCase("__FOO_BAR__")
|
// foo_bar
|
||||||
fmt.Println(s4) //foo_bar
|
// foobar
|
||||||
|
// foo_1_1_bar
|
||||||
s5 := strutil.SnakeCase("Foo-#1😄$_%^&*(1bar")
|
|
||||||
fmt.Println(s5) //foo_1_1_bar
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="UpperSnakeCase">UpperSnakeCase</span>
|
### <span id="UpperSnakeCase">UpperSnakeCase</span>
|
||||||
<p>将字符串转换为大写SNAKE_CASE形式, 非字母和数字会被忽略</p>
|
<p>将字符串转换为大写SNAKE_CASE形式, 非字母和数字会被忽略。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func SnakeCase(s string) string
|
func SnakeCase(s string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -548,34 +595,31 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.UpperSnakeCase("Foo Bar-")
|
strings := []string{"", "foo-bar", "Foo Bar-", "FooBAR", "Foo-#1😄$_%^&*(1bar"}
|
||||||
fmt.Println(s1) //FOO_BAR
|
|
||||||
|
|
||||||
s2 := strutil.UpperSnakeCase("foo_Bar")
|
for _, v := range strings {
|
||||||
fmt.Println(s2) //FOO_BAR
|
s := strutil.UpperSnakeCase(v)
|
||||||
|
fmt.Println(s)
|
||||||
|
}
|
||||||
|
|
||||||
s3 := strutil.UpperSnakeCase("fooBar")
|
// Output:
|
||||||
fmt.Println(s3) //FOO_BAR
|
//
|
||||||
|
// FOO_BAR
|
||||||
s4 := strutil.UpperSnakeCase("__FOO_BAR__")
|
// FOO_BAR
|
||||||
fmt.Println(s4) //FOO_BAR
|
// FOO_BAR
|
||||||
|
// FOO_1_1_BAR
|
||||||
s5 := strutil.UpperSnakeCase("Foo-#1😄$_%^&*(1bar")
|
|
||||||
fmt.Println(s5) //FOO_1_1_BAR
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="SplitEx">SplitEx</span>
|
### <span id="SplitEx">SplitEx</span>
|
||||||
<p>分割字符串为切片,removeEmptyString参数指定是否去除空字符串</p>
|
<p>分割字符串为切片,removeEmptyString参数指定是否去除空字符串。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func SplitEx(s, sep string, removeEmptyString bool) []string
|
func SplitEx(s, sep string, removeEmptyString bool) []string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -584,32 +628,37 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
arr1 := strutil.SplitEx(" a b c ", "", true)
|
result1 := strutil.SplitEx(" a b c ", "", true)
|
||||||
fmt.Println(arr1) //[]string{}
|
|
||||||
|
|
||||||
arr2 := strutil.SplitEx(" a b c ", " ", false)
|
result2 := strutil.SplitEx(" a b c ", " ", false)
|
||||||
fmt.Println(arr2) //[]string{"", "a", "b", "c", ""}
|
result3 := strutil.SplitEx(" a b c ", " ", true)
|
||||||
|
|
||||||
arr3 := strutil.SplitEx(" a b c ", " ", true)
|
result4 := strutil.SplitEx("a = b = c = ", " = ", false)
|
||||||
fmt.Println(arr3) //[]string{"a", "b", "c"}
|
result5 := strutil.SplitEx("a = b = c = ", " = ", true)
|
||||||
|
|
||||||
arr4 := strutil.SplitEx(" a = b = c = ", " = ", false)
|
fmt.Println(result1)
|
||||||
fmt.Println(arr4) //[]string{" a", "b", "c", ""}
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
arr5 := strutil.SplitEx(" a = b = c = ", " = ", true)
|
// Output:
|
||||||
fmt.Println(arr5) //[]string{" a", "b", "c"}
|
// []
|
||||||
|
// [ a b c ]
|
||||||
|
// [a b c]
|
||||||
|
// [a b c ]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### <span id="Substring">Substring</span>
|
### <span id="Substring">Substring</span>
|
||||||
<p>根据指定的位置和长度截取子字符串</p>
|
<p>根据指定的位置和长度截取字符串。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func Substring(s string, offset int, length uint) string
|
func Substring(s string, offset int, length uint) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -619,35 +668,38 @@ import (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
result1 := strutil.Substring("abcde", 1, 3)
|
result1 := strutil.Substring("abcde", 1, 3)
|
||||||
fmt.Println(result1) //bcd
|
|
||||||
|
|
||||||
result2 := strutil.Substring("abcde", 1, 5)
|
result2 := strutil.Substring("abcde", 1, 5)
|
||||||
fmt.Println(result2) //bcde
|
|
||||||
|
|
||||||
result3 := strutil.Substring("abcde", -1, 3)
|
result3 := strutil.Substring("abcde", -1, 3)
|
||||||
fmt.Println(result3) //e
|
|
||||||
|
|
||||||
result4 := strutil.Substring("abcde", -2, 2)
|
result4 := strutil.Substring("abcde", -2, 2)
|
||||||
fmt.Println(result4) //de
|
|
||||||
|
|
||||||
result5 := strutil.Substring("abcde", -2, 3)
|
result5 := strutil.Substring("abcde", -2, 3)
|
||||||
fmt.Println(result5) //de
|
|
||||||
|
|
||||||
result6 := strutil.Substring("你好,欢迎你", 0, 2)
|
result6 := strutil.Substring("你好,欢迎你", 0, 2)
|
||||||
fmt.Println(result6) //你好
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
fmt.Println(result6)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// bcd
|
||||||
|
// bcde
|
||||||
|
// e
|
||||||
|
// de
|
||||||
|
// de
|
||||||
|
// 你好
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="Wrap">Wrap</span>
|
### <span id="Wrap">Wrap</span>
|
||||||
<p>用另一个字符串包裹一个字符串</p>
|
<p>用另一个字符串包裹一个字符串。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func Wrap(str string, wrapWith string) string
|
func Wrap(str string, wrapWith string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -656,35 +708,34 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.Wrap("ab", "")
|
result1 := strutil.Wrap("foo", "")
|
||||||
fmt.Println(s1) //ab
|
result2 := strutil.Wrap("foo", "*")
|
||||||
|
result3 := strutil.Wrap("'foo'", "'")
|
||||||
|
result4 := strutil.Wrap("", "*")
|
||||||
|
|
||||||
s2 := strutil.Wrap("", "*")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //""
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
s3 := strutil.Wrap("ab", "*")
|
fmt.Println(result4)
|
||||||
fmt.Println(s3) //*ab*
|
|
||||||
|
// Output:
|
||||||
s4 := strutil.Wrap("ab", "\"")
|
// foo
|
||||||
fmt.Println(s4) //\"ab\"
|
// *foo*
|
||||||
|
// ''foo''
|
||||||
s5 := strutil.Wrap("ab", "'")
|
//
|
||||||
fmt.Println(s5) //'ab'
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### <span id="Wrap">Wrap</span>
|
||||||
|
<p>用另一个字符串解开包裹一个字符串。</p>
|
||||||
### <span id="Unwrap">Unwrap</span>
|
|
||||||
<p>用另一个字符串解开包裹一个字符串</p>
|
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func Unwrap(str string, wrapToken string) string
|
func Unwrap(str string, wrapToken string) string
|
||||||
```
|
```
|
||||||
<b>例子:</b>
|
<b>示例:</b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -693,28 +744,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
s1 := strutil.Unwrap("ab", "")
|
result1 := strutil.Unwrap("foo", "")
|
||||||
fmt.Println(s1) //ab
|
result2 := strutil.Unwrap("*foo*", "*")
|
||||||
|
result3 := strutil.Unwrap("*foo", "*")
|
||||||
|
result4 := strutil.Unwrap("foo*", "*")
|
||||||
|
result5 := strutil.Unwrap("**foo**", "*")
|
||||||
|
|
||||||
s2 := strutil.Unwrap("ab", "*")
|
fmt.Println(result1)
|
||||||
fmt.Println(s2) //ab
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
fmt.Println(result4)
|
||||||
|
fmt.Println(result5)
|
||||||
|
|
||||||
s3 := strutil.Unwrap("**ab**", "*")
|
// Output:
|
||||||
fmt.Println(s3) //*ab*
|
// foo
|
||||||
|
// foo
|
||||||
s4 := strutil.Unwrap("*ab", "*")
|
// *foo
|
||||||
fmt.Println(s4) //*ab
|
// foo*
|
||||||
|
// *foo*
|
||||||
s5 := strutil.Unwrap("***", "**")
|
|
||||||
fmt.Println(s5) //***
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -47,7 +47,7 @@ func Before(n int, fn any) func(args ...any) []reflect.Value {
|
|||||||
type CurryFn[T any] func(...T) T
|
type CurryFn[T any] func(...T) T
|
||||||
|
|
||||||
// New make a curry function for specific value.
|
// New make a curry function for specific value.
|
||||||
// Play: Todo
|
// Play: https://go.dev/play/p/5HopfDwANKX
|
||||||
func (cf CurryFn[T]) New(val T) func(...T) T {
|
func (cf CurryFn[T]) New(val T) func(...T) T {
|
||||||
return func(vals ...T) T {
|
return func(vals ...T) T {
|
||||||
args := append([]T{val}, vals...)
|
args := append([]T{val}, vals...)
|
||||||
@@ -56,7 +56,7 @@ func (cf CurryFn[T]) New(val T) func(...T) T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Compose compose the functions from right to left.
|
// Compose compose the functions from right to left.
|
||||||
// Play: Todo
|
// Play: https://go.dev/play/p/KKfugD4PKYF
|
||||||
func Compose[T any](fnList ...func(...T) T) func(...T) T {
|
func Compose[T any](fnList ...func(...T) T) func(...T) T {
|
||||||
return func(args ...T) T {
|
return func(args ...T) T {
|
||||||
firstFn := fnList[0]
|
firstFn := fnList[0]
|
||||||
|
|||||||
@@ -114,15 +114,15 @@ func ExampleSchedule() {
|
|||||||
count++
|
count++
|
||||||
}
|
}
|
||||||
|
|
||||||
stop := Schedule(1*time.Second, increase)
|
stop := Schedule(2*time.Second, increase)
|
||||||
|
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(2 * time.Second)
|
||||||
close(stop)
|
close(stop)
|
||||||
|
|
||||||
fmt.Println(count)
|
fmt.Println(count)
|
||||||
|
|
||||||
// Output:
|
// Output:
|
||||||
// 3
|
// 2
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExamplePipeline() {
|
func ExamplePipeline() {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package function
|
|||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
// Watcher is used for record code excution time
|
// Watcher is used for record code excution time
|
||||||
// Play: Todo
|
// Play: https://go.dev/play/p/l2yrOpCLd1I
|
||||||
type Watcher struct {
|
type Watcher struct {
|
||||||
startTime int64
|
startTime int64
|
||||||
stopTime int64
|
stopTime int64
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ func Factorial(x uint) uint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Percent calculate the percentage of value to total.
|
// Percent calculate the percentage of value to total.
|
||||||
// Play: Todo
|
// Play: https://go.dev/play/p/QQM9B13coSP
|
||||||
func Percent(val, total float64, n int) float64 {
|
func Percent(val, total float64, n int) float64 {
|
||||||
if total == 0 {
|
if total == 0 {
|
||||||
return float64(0)
|
return float64(0)
|
||||||
|
|||||||
@@ -283,7 +283,7 @@ func SplitEx(s, sep string, removeEmptyString bool) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Substring returns a substring of the specified length starting at the specified offset position.
|
// Substring returns a substring of the specified length starting at the specified offset position.
|
||||||
// Play: Todo
|
// Play: https://go.dev/play/p/q3sM6ehnPDp
|
||||||
func Substring(s string, offset int, length uint) string {
|
func Substring(s string, offset int, length uint) string {
|
||||||
rs := []rune(s)
|
rs := []rune(s)
|
||||||
size := len(rs)
|
size := len(rs)
|
||||||
|
|||||||
Reference in New Issue
Block a user