mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
doc: update document for version v2.3.8
This commit is contained in:
82
README.md
82
README.md
@@ -313,6 +313,15 @@ import "github.com/duke-git/lancet/v2/convertor"
|
|||||||
- **<big>ToPointer</big>** : return a pointer of passed value.
|
- **<big>ToPointer</big>** : return a pointer of passed value.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/convertor.md#ToPointer)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/convertor.md#ToPointer)]
|
||||||
[[play](https://go.dev/play/p/ASf_etHNlw1)]
|
[[play](https://go.dev/play/p/ASf_etHNlw1)]
|
||||||
|
- **<big>ToPointers</big>** : convert a slice of values to a slice of pointers.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/convertor.md#ToPointers)]
|
||||||
|
[[play](https://go.dev/play/p/ZUoXd2i5ZkV)]
|
||||||
|
- **<big>FromPointer</big>** : returns the value pointed to by the pointer.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/convertor.md#FromPointer)]
|
||||||
|
[[play](https://go.dev/play/p/wAp90V7Zu6g)]
|
||||||
|
- **<big>FromPointers</big>** : convert a slice of pointers to a slice of values.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/convertor.md#FromPointers)]
|
||||||
|
[[play](https://go.dev/play/p/qIPsyYtNy3Q)]
|
||||||
- **<big>ToString</big>** : convert value to string.
|
- **<big>ToString</big>** : convert value to string.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/convertor.md#ToString)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/convertor.md#ToString)]
|
||||||
[[play](https://go.dev/play/p/nF1zOOslpQq)]
|
[[play](https://go.dev/play/p/nF1zOOslpQq)]
|
||||||
@@ -724,7 +733,7 @@ import optional "github.com/duke-git/lancet/v2/datastructure/optional"
|
|||||||
import "github.com/duke-git/lancet/v2/eventbus"
|
import "github.com/duke-git/lancet/v2/eventbus"
|
||||||
```
|
```
|
||||||
|
|
||||||
#### 函数列表:
|
#### Function list:
|
||||||
|
|
||||||
- **<big>NewEventBus</big>** : Create an EventBus instance.
|
- **<big>NewEventBus</big>** : Create an EventBus instance.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/eventbus.md#NewEventBus)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/eventbus.md#NewEventBus)]
|
||||||
@@ -757,6 +766,69 @@ import "github.com/duke-git/lancet/v2/eventbus"
|
|||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/eventbus.md#SetErrorHandler)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/eventbus.md#SetErrorHandler)]
|
||||||
[[play](https://go.dev/play/p/gmB0gnFe5mc)]
|
[[play](https://go.dev/play/p/gmB0gnFe5mc)]
|
||||||
|
|
||||||
|
<h3 id="eventbus"> 9. Package enum provides a simple enum implementation. <a href="#index">Index</a></h3>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "github.com/duke-git/lancet/v2/enum"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Function list:
|
||||||
|
|
||||||
|
- **<big>NewItem</big>** : Creates a new enum item.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/enum.md#NewEventBus)]
|
||||||
|
[[play](https://go.dev/play/p/8qNsLw01HD5)]
|
||||||
|
- **<big>NewItemsFromPairs</big>** : Creates enum items from a slice of Pair structs.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/enum.md#NewItemsFromPairs)]
|
||||||
|
[[play](https://go.dev/play/p/xKnoGa7gnev)]
|
||||||
|
- **<big>Value</big>** : Returns the value of the enum item.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/enum.md#Value)]
|
||||||
|
[[play](https://go.dev/play/p/xKnoGa7gnev)]
|
||||||
|
- **<big>Name</big>** : Returns the name of the enum item.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/enum.md#Name)]
|
||||||
|
[[play](https://go.dev/play/p/xKnoGa7gnev)]
|
||||||
|
- **<big>Valid</big>** : Checks if the enum item is valid. If a custom check function is provided, it will be used to validate the value.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/enum.md#Valid)]
|
||||||
|
[[play](https://go.dev/play/p/pA3lYY2VSm3)]
|
||||||
|
- **<big>MarshalJSON</big>** : Implementation of json.Marshaler interface.</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/enum.md#MarshalJSON)]
|
||||||
|
[[play](https://go.dev/play/p/zIZEdAnneB5)]
|
||||||
|
- **<big>NewRegistry</big>** : Creates a new enum registry..</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/enum.md#NewRegistry)]
|
||||||
|
[[play](https://go.dev/play/p/ABEXsYfJKMo)]
|
||||||
|
- **<big>Add</big>** : Adds enum items to the registry.</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/enum.md#Add)]
|
||||||
|
[[play](https://go.dev/play/p/ABEXsYfJKMo)]
|
||||||
|
- **<big>Remove</big>** : Removes an enum item from the registry by its value.</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/enum.md#Remove)]
|
||||||
|
[[play](https://go.dev/play/p/dSG84wQ3TuC)]
|
||||||
|
- **<big>Update</big>** : Updates the name of an enum item in the registry by its value.</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/enum.md#Update)]
|
||||||
|
[[play](https://go.dev/play/p/Ol0moT1J9Xl)]
|
||||||
|
- **<big>GetByValue</big>** : Retrieves an enum item by its value.</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/enum.md#GetByValue)]
|
||||||
|
[[play](https://go.dev/play/p/niJ1U2KlE_m)]
|
||||||
|
- **<big>GetByName</big>** : Retrieves an enum item by its name.</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/enum.md#GetByName)]
|
||||||
|
[[play](https://go.dev/play/p/49ie_gpqH0m)]
|
||||||
|
- **<big>Items</big>** : Returns a slice of all enum items in the registry.</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/enum.md#Items)]
|
||||||
|
[[play](https://go.dev/play/p/lAJFAradbvQ)]
|
||||||
|
- **<big>Contains</big>** : Checks if an enum item with the given value exists in the registry.</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/enum.md#Contains)]
|
||||||
|
[[play](https://go.dev/play/p/_T-lPYkZn2j)]
|
||||||
|
- **<big>Size</big>** : Returns the number of enum items in the registry.</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/enum.md#Size)]
|
||||||
|
[[play](https://go.dev/play/p/TeDArWhlQe2)]
|
||||||
|
- **<big>Range</big>** : Iterates over all enum items in the registry and applies the given function.</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/enum.md#Range)]
|
||||||
|
[[play](https://go.dev/play/p/GPsZbQbefWN)]
|
||||||
|
- **<big>SortedItems</big>** : Returns a slice of all enum items sorted by the given less function.</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/enum.md#SortedItems)]
|
||||||
|
[[play](https://go.dev/play/p/tN9RE_m_WEI)]
|
||||||
|
- **<big>Filter</big>** : Returns a slice of enum items that satisfy the given predicate function.</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/enum.md#Filter)]
|
||||||
|
[[play](https://go.dev/play/p/uTUpTdcyoCU)]
|
||||||
|
|
||||||
<h3 id="fileutil"> 9. Fileutil package implements some basic functions for file operations. <a href="#index">index</a></h3>
|
<h3 id="fileutil"> 9. Fileutil package implements some basic functions for file operations. <a href="#index">index</a></h3>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1127,7 +1199,7 @@ import "github.com/duke-git/lancet/v2/maputil"
|
|||||||
[[play](https://go.dev/play/p/bvNwNBZDm6v)]
|
[[play](https://go.dev/play/p/bvNwNBZDm6v)]
|
||||||
- **<big>ToMarkdownTable</big>** : Convert a map slice data to a Markdown table string. It supports custom header display names and column display order.
|
- **<big>ToMarkdownTable</big>** : Convert a map slice data to a Markdown table string. It supports custom header display names and column display order.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#ToMarkdownTable)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#ToMarkdownTable)]
|
||||||
[[play](https://go.dev/play/p/todo)]
|
[[play](https://go.dev/play/p/w_pSLfeyEB5)]
|
||||||
|
|
||||||
<h3 id="mathutil"> 13. Mathutil package implements some functions for math calculation. <a href="#index">index</a></h3>
|
<h3 id="mathutil"> 13. Mathutil package implements some functions for math calculation. <a href="#index">index</a></h3>
|
||||||
|
|
||||||
@@ -1867,7 +1939,7 @@ import "github.com/duke-git/lancet/v2/structs"
|
|||||||
[[play](https://go.dev/play/p/Ig75P-agN39)]
|
[[play](https://go.dev/play/p/Ig75P-agN39)]
|
||||||
- **<big>TypeName</big>** : Return struct type name.
|
- **<big>TypeName</big>** : Return struct type name.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#TypeName)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/struct.md#TypeName)]
|
||||||
[[play](https://go.dev/play/p/todo)]
|
[[play](https://go.dev/play/p/SWLWd0XBaBb)]
|
||||||
|
|
||||||
<h3 id="strutil"> 21. Strutil package contains some functions to manipulate string. <a href="#index">index</a></h3>
|
<h3 id="strutil"> 21. Strutil package contains some functions to manipulate string. <a href="#index">index</a></h3>
|
||||||
|
|
||||||
@@ -2328,10 +2400,10 @@ import "github.com/duke-git/lancet/v2/validator"
|
|||||||
[[play](https://go.dev/play/p/yafpdxLiymu)]
|
[[play](https://go.dev/play/p/yafpdxLiymu)]
|
||||||
- **<big>IsPassport</big>** : Passport validation(using regex).
|
- **<big>IsPassport</big>** : Passport validation(using regex).
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsPassport)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsPassport)]
|
||||||
[[play](https://go.dev/play/p/todo)]
|
[[play](https://go.dev/play/p/dvOiV2BW7Aw)]
|
||||||
- **<big>IsChineseHMPassport</big>** : Mainland travel permit for Hong Kong, Macao validation (using regex).
|
- **<big>IsChineseHMPassport</big>** : Mainland travel permit for Hong Kong, Macao validation (using regex).
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsChineseHMPassport)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsChineseHMPassport)]
|
||||||
[[play](https://go.dev/play/p/todo)]
|
[[play](https://go.dev/play/p/xKG6spQTcY0)]
|
||||||
|
|
||||||
<h3 id="xerror"> 25. Xerror package implements helpers for errors. <a href="#index">index</a></h3>
|
<h3 id="xerror"> 25. Xerror package implements helpers for errors. <a href="#index">index</a></h3>
|
||||||
|
|
||||||
|
|||||||
@@ -312,6 +312,15 @@ import "github.com/duke-git/lancet/v2/convertor"
|
|||||||
- **<big>ToPointer</big>** : 返回传入值的指针。
|
- **<big>ToPointer</big>** : 返回传入值的指针。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/convertor.md#ToPointer)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/convertor.md#ToPointer)]
|
||||||
[[play](https://go.dev/play/p/ASf_etHNlw1)]
|
[[play](https://go.dev/play/p/ASf_etHNlw1)]
|
||||||
|
- **<big>ToPointers</big>** : 将值的切片转换为指针的切片。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/convertor.md#ToPointers)]
|
||||||
|
[[play](https://go.dev/play/p/ZUoXd2i5ZkV)]
|
||||||
|
- **<big>FromPointer</big>** : 返回指针所指向的值。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/convertor.md#FromPointer)]
|
||||||
|
[[play](https://go.dev/play/p/wAp90V7Zu6g)]
|
||||||
|
- **<big>FromPointers</big>** : 将指针的切片转换为值的切片。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/convertor.md#FromPointers)]
|
||||||
|
[[play](https://go.dev/play/p/qIPsyYtNy3Q)]
|
||||||
- **<big>ToString</big>** : 将值转换为字符串,对于数字、字符串、[]byte,将转换为字符串。 对于其他类型(切片、映射、数组、结构)将调用 json.Marshal。
|
- **<big>ToString</big>** : 将值转换为字符串,对于数字、字符串、[]byte,将转换为字符串。 对于其他类型(切片、映射、数组、结构)将调用 json.Marshal。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/convertor.md#ToString)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/convertor.md#ToString)]
|
||||||
[[play](https://go.dev/play/p/nF1zOOslpQq)]
|
[[play](https://go.dev/play/p/nF1zOOslpQq)]
|
||||||
@@ -767,6 +776,69 @@ import "github.com/duke-git/lancet/v2/eventbus"
|
|||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/eventbus.md#SetErrorHandler)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/eventbus.md#SetErrorHandler)]
|
||||||
[[play](https://go.dev/play/p/gmB0gnFe5mc)]
|
[[play](https://go.dev/play/p/gmB0gnFe5mc)]
|
||||||
|
|
||||||
|
<h3 id="eventbus"> 9. Package enum provides a simple enum implementation. <a href="#index">Index</a></h3>
|
||||||
|
|
||||||
|
```go
|
||||||
|
import "github.com/duke-git/lancet/v2/enum"
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Function list:
|
||||||
|
|
||||||
|
- **<big>NewItem</big>** : 创建枚举项。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/enum.md#NewEventBus)]
|
||||||
|
[[play](https://go.dev/play/p/8qNsLw01HD5)]
|
||||||
|
- **<big>NewItemsFromPairs</big>** : 从 Pair 结构体的切片创建枚举项。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/enum.md#NewItemsFromPairs)]
|
||||||
|
[[play](https://go.dev/play/p/xKnoGa7gnev)]
|
||||||
|
- **<big>Value</big>** : R 返回枚举项的值。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/enum.md#Value)]
|
||||||
|
[[play](https://go.dev/play/p/xKnoGa7gnev)]
|
||||||
|
- **<big>Name</big>** : 返回枚举项的名称。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/enum.md#Name)]
|
||||||
|
[[play](https://go.dev/play/p/xKnoGa7gnev)]
|
||||||
|
- **<big>Valid</big>** : 检查枚举项是否有效。如果提供了自定义检查函数,将使用该函数验证值。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/enum.md#Valid)]
|
||||||
|
[[play](https://go.dev/play/p/pA3lYY2VSm3)]
|
||||||
|
- **<big>MarshalJSON</big>** : 枚举项实现 json.Marshaler 接口。</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/enum.md#MarshalJSON)]
|
||||||
|
[[play](https://go.dev/play/p/zIZEdAnneB5)]
|
||||||
|
- **<big>NewRegistry</big>** : Registry 定义了一个通用的枚举注册表结构体。</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/enum.md#NewRegistry)]
|
||||||
|
[[play](https://go.dev/play/p/ABEXsYfJKMo)]
|
||||||
|
- **<big>Add</big>** : 向枚举注册表添加枚举项。</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/enum.md#Add)]
|
||||||
|
[[play](https://go.dev/play/p/ABEXsYfJKMo)]
|
||||||
|
- **<big>Remove</big>** : 在枚举注册表中删除枚举项。</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/enum.md#Remove)]
|
||||||
|
[[play](https://go.dev/play/p/dSG84wQ3TuC)]
|
||||||
|
- **<big>Update</big>** : 在枚举注册表中更新枚举项。</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/enum.md#Update)]
|
||||||
|
[[play](https://go.dev/play/p/Ol0moT1J9Xl)]
|
||||||
|
- **<big>GetByValue</big>** : 在枚举注册表中通过值获取枚举项。</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/enum.md#GetByValue)]
|
||||||
|
[[play](https://go.dev/play/p/niJ1U2KlE_m)]
|
||||||
|
- **<big>GetByName</big>** : 在枚举注册表中通过名称获取枚举项。</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/enum.md#GetByName)]
|
||||||
|
[[play](https://go.dev/play/p/49ie_gpqH0m)]
|
||||||
|
- **<big>Items</big>** : 返回枚举注册表中的枚举项。</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/enum.md#Items)]
|
||||||
|
[[play](https://go.dev/play/p/lAJFAradbvQ)]
|
||||||
|
- **<big>Contains</big>** : 检查注册表中是否存在具有给定值的枚举项。</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/enum.md#Contains)]
|
||||||
|
[[play](https://go.dev/play/p/_T-lPYkZn2j)]
|
||||||
|
- **<big>Size</big>** : 返回注册表中枚举项的数目。</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/enum.md#Size)]
|
||||||
|
[[play](https://go.dev/play/p/TeDArWhlQe2)]
|
||||||
|
- **<big>Range</big>** : 遍历注册表中的所有枚举项,并应用给定的函数。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/enum.md#Range)]
|
||||||
|
[[play](https://go.dev/play/p/GPsZbQbefWN)]
|
||||||
|
- **<big>SortedItems</big>** : 返回按给定比较函数排序的所有枚举项的切片。</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/enum.md#SortedItems)]
|
||||||
|
[[play](https://go.dev/play/p/tN9RE_m_WEI)]
|
||||||
|
- **<big>Filter</big>** : 返回满足给定谓词函数的枚举项切片。</p>
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/enum.md#Filter)]
|
||||||
|
[[play](https://go.dev/play/p/uTUpTdcyoCU)]
|
||||||
|
|
||||||
<h3 id="fileutil"> 10. fileutil 包含文件基本操作。 <a href="#index">回到目录</a></h3>
|
<h3 id="fileutil"> 10. fileutil 包含文件基本操作。 <a href="#index">回到目录</a></h3>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@@ -1137,7 +1209,7 @@ import "github.com/duke-git/lancet/v2/maputil"
|
|||||||
[[play](https://go.dev/play/p/bvNwNBZDm6v)]
|
[[play](https://go.dev/play/p/bvNwNBZDm6v)]
|
||||||
- **<big>ToMarkdownTable</big>** : 将一个 map 切片数据转换为 Markdown 表格字符串。支持自定义表头显示名称和列的显示顺序。
|
- **<big>ToMarkdownTable</big>** : 将一个 map 切片数据转换为 Markdown 表格字符串。支持自定义表头显示名称和列的显示顺序。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#ToMarkdownTable)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#ToMarkdownTable)]
|
||||||
[[play](https://go.dev/play/p/todo)]
|
[[play](https://go.dev/play/p/w_pSLfeyEB5)]
|
||||||
|
|
||||||
<h3 id="mathutil"> 14. mathutil 包实现了一些数学计算的函数。 <a href="#index">回到目录</a></h3>
|
<h3 id="mathutil"> 14. mathutil 包实现了一些数学计算的函数。 <a href="#index">回到目录</a></h3>
|
||||||
|
|
||||||
@@ -1873,7 +1945,7 @@ import "github.com/duke-git/lancet/v2/structs"
|
|||||||
[[play](https://go.dev/play/p/Ig75P-agN39)]
|
[[play](https://go.dev/play/p/Ig75P-agN39)]
|
||||||
- **<big>TypeName</big>** : 获取结构体类型名。
|
- **<big>TypeName</big>** : 获取结构体类型名。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#TypeName)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/struct.md#TypeName)]
|
||||||
[[play](https://go.dev/play/p/todo)]
|
[[play](https://go.dev/play/p/SWLWd0XBaBb)]
|
||||||
|
|
||||||
<h3 id="strutil"> 22. strutil 包含字符串处理的相关函数。 <a href="#index">回到目录</a></h3>
|
<h3 id="strutil"> 22. strutil 包含字符串处理的相关函数。 <a href="#index">回到目录</a></h3>
|
||||||
|
|
||||||
@@ -2335,10 +2407,11 @@ import "github.com/duke-git/lancet/v2/validator"
|
|||||||
[[play](https://go.dev/play/p/yafpdxLiymu)]
|
[[play](https://go.dev/play/p/yafpdxLiymu)]
|
||||||
- **<big>IsPassport</big>** : 判断护照(正则判断)。
|
- **<big>IsPassport</big>** : 判断护照(正则判断)。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsPassport)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsPassport)]
|
||||||
[[play](https://go.dev/play/p/todo)]
|
[[play](https://go.dev/play/p/dvOiV2BW7Aw)]
|
||||||
- **<big>IsChineseHMPassport</big>** : 判断港澳台通行证(正则判断)。
|
- **<big>IsChineseHMPassport</big>** : 判断港澳台通行证(正则判断)。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsChineseHMPassport)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsChineseHMPassport)]
|
||||||
[[play](https://go.dev/play/p/todo)]
|
[[play](https://go.dev/play/p/xKG6spQTcY0)]
|
||||||
|
|
||||||
<h3 id="xerror"> 26. xerror 包实现一些错误处理函数。 <a href="#index">回到目录</a></h3>
|
<h3 id="xerror"> 26. xerror 包实现一些错误处理函数。 <a href="#index">回到目录</a></h3>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|||||||
@@ -229,7 +229,7 @@ func ToPointer[T any](value T) *T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ToPointers convert a slice of values to a slice of pointers.
|
// ToPointers convert a slice of values to a slice of pointers.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/ZUoXd2i5ZkV
|
||||||
func ToPointers[T any](values []T) []*T {
|
func ToPointers[T any](values []T) []*T {
|
||||||
result := make([]*T, len(values))
|
result := make([]*T, len(values))
|
||||||
for i := range values {
|
for i := range values {
|
||||||
@@ -239,7 +239,7 @@ func ToPointers[T any](values []T) []*T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FromPointer returns the value pointed to by the pointer.
|
// FromPointer returns the value pointed to by the pointer.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/wAp90V7Zu6g
|
||||||
func FromPointer[T any](ptr *T) T {
|
func FromPointer[T any](ptr *T) T {
|
||||||
if ptr == nil {
|
if ptr == nil {
|
||||||
var zeroValue T
|
var zeroValue T
|
||||||
@@ -250,7 +250,7 @@ func FromPointer[T any](ptr *T) T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FromPointers convert a slice of pointers to a slice of values.
|
// FromPointers convert a slice of pointers to a slice of values.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/qIPsyYtNy3Q
|
||||||
func FromPointers[T any](pointers []*T) []T {
|
func FromPointers[T any](pointers []*T) []T {
|
||||||
result := make([]T, len(pointers))
|
result := make([]T, len(pointers))
|
||||||
for i, ptr := range pointers {
|
for i, ptr := range pointers {
|
||||||
|
|||||||
@@ -169,6 +169,45 @@ func ExampleToPointer() {
|
|||||||
// 123
|
// 123
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleToPointers() {
|
||||||
|
strs := []string{"a", "b", "c"}
|
||||||
|
pointerStrs := ToPointers(strs)
|
||||||
|
fmt.Println(*pointerStrs[0])
|
||||||
|
fmt.Println(*pointerStrs[1])
|
||||||
|
fmt.Println(*pointerStrs[2])
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// a
|
||||||
|
// b
|
||||||
|
// c
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleFromPointer() {
|
||||||
|
str := "abc"
|
||||||
|
strPtr := &str
|
||||||
|
result := FromPointer(strPtr)
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// abc
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleFromPointers() {
|
||||||
|
strs := []string{"a", "b", "c"}
|
||||||
|
strPtr := []*string{&strs[0], &strs[1], &strs[2]}
|
||||||
|
|
||||||
|
result := FromPointers(strPtr)
|
||||||
|
|
||||||
|
fmt.Println(result[0])
|
||||||
|
fmt.Println(result[1])
|
||||||
|
fmt.Println(result[2])
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// a
|
||||||
|
// b
|
||||||
|
// c
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleToMap() {
|
func ExampleToMap() {
|
||||||
type Message struct {
|
type Message struct {
|
||||||
name string
|
name string
|
||||||
|
|||||||
@@ -33,6 +33,9 @@ import (
|
|||||||
- [ToJson](#ToJson)
|
- [ToJson](#ToJson)
|
||||||
- [ToMap](#ToMap)
|
- [ToMap](#ToMap)
|
||||||
- [ToPointer](#ToPointer)
|
- [ToPointer](#ToPointer)
|
||||||
|
- [ToPointers](#ToPointers)
|
||||||
|
- [FromPointer](#FromPointer)
|
||||||
|
- [FromPointers](#FromPointers)
|
||||||
- [ToString](#ToString)
|
- [ToString](#ToString)
|
||||||
- [StructToMap](#StructToMap)
|
- [StructToMap](#StructToMap)
|
||||||
- [MapToSlice](#MapToSlice)
|
- [MapToSlice](#MapToSlice)
|
||||||
@@ -456,6 +459,108 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="ToPointers">ToPointers</span>
|
||||||
|
|
||||||
|
<p>将值的切片转换为指针的切片。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ToPointers[T any](values []T) []*T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/ZUoXd2i5ZkV)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
strs := []string{"a", "b", "c"}
|
||||||
|
pointerStrs := convertor.ToPointers(strs)
|
||||||
|
fmt.Println(*pointerStrs[0])
|
||||||
|
fmt.Println(*pointerStrs[1])
|
||||||
|
fmt.Println(*pointerStrs[2])
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// a
|
||||||
|
// b
|
||||||
|
// c
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="FromPointer">FromPointer</span>
|
||||||
|
|
||||||
|
<p>返回指针所指向的值。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func FromPointer[T any](ptr *T) T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/wAp90V7Zu6g)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "abc"
|
||||||
|
strPtr := &str
|
||||||
|
result := convertor.FromPointer(strPtr)
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// abc
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="FromPointers">FromPointers</span>
|
||||||
|
|
||||||
|
<p>将指针的切片转换为值的切片。</p>
|
||||||
|
|
||||||
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func FromPointers[T any](pointers []*T) []T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/qIPsyYtNy3Q)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
strs := []string{"a", "b", "c"}
|
||||||
|
strPtr := []*string{&strs[0], &strs[1], &strs[2]}
|
||||||
|
|
||||||
|
result := convertor.FromPointers(strPtr)
|
||||||
|
|
||||||
|
fmt.Println(result[0])
|
||||||
|
fmt.Println(result[1])
|
||||||
|
fmt.Println(result[2])
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// a
|
||||||
|
// b
|
||||||
|
// c
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <span id="ToString">ToString</span>
|
### <span id="ToString">ToString</span>
|
||||||
|
|
||||||
<p>将值转换为字符串,对于数字、字符串、[]byte,将转换为字符串。 对于其他类型(切片、映射、数组、结构体)将调用 json.Marshal</p>
|
<p>将值转换为字符串,对于数字、字符串、[]byte,将转换为字符串。 对于其他类型(切片、映射、数组、结构体)将调用 json.Marshal</p>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
# Enum
|
# Enum
|
||||||
|
|
||||||
Enum实现一个简单枚举工具包。
|
Enum 实现一个简单枚举工具包。
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
@@ -55,7 +55,7 @@ import (
|
|||||||
func NewItem[T comparable](value T, name string) *Item[T]
|
func NewItem[T comparable](value T, name string) *Item[T]
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/8qNsLw01HD5)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -96,7 +96,7 @@ func main() {
|
|||||||
func NewItemsFromPairs[T comparable](pairs ...Pair[T]) []*Item[T]
|
func NewItemsFromPairs[T comparable](pairs ...Pair[T]) []*Item[T]
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/xKnoGa7gnev)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -139,7 +139,7 @@ func main() {
|
|||||||
func (it *Item[T]) Value() T
|
func (it *Item[T]) Value() T
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/xKnoGa7gnev)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -182,7 +182,7 @@ func main() {
|
|||||||
func (it *Item[T]) Name() string
|
func (it *Item[T]) Name() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/xKnoGa7gnev)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -225,7 +225,7 @@ func main() {
|
|||||||
func (it *Item[T]) Valid(checker ...func(T) bool) bool
|
func (it *Item[T]) Valid(checker ...func(T) bool) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/pA3lYY2VSm3)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -258,7 +258,7 @@ func main() {
|
|||||||
|
|
||||||
### <span id="MarshalJSON">MarshalJSON</span>
|
### <span id="MarshalJSON">MarshalJSON</span>
|
||||||
|
|
||||||
<p>枚举项实现json.Marshaler 接口。</p>
|
<p>枚举项实现json.Marshaler接口。</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -267,7 +267,7 @@ func (it *Item[T]) MarshalJSON() ([]byte, error)
|
|||||||
func (it *Item[T]) UnmarshalJSON(data []byte) error
|
func (it *Item[T]) UnmarshalJSON(data []byte) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/zIZEdAnneB5)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -290,7 +290,7 @@ func main() {
|
|||||||
data, _ := item.MarshalJSON()
|
data, _ := item.MarshalJSON()
|
||||||
fmt.Println(string(data))
|
fmt.Println(string(data))
|
||||||
|
|
||||||
var unmarshaledItem Item[Status]
|
var unmarshaledItem enum.Item[Status]
|
||||||
_ = unmarshaledItem.UnmarshalJSON(data)
|
_ = unmarshaledItem.UnmarshalJSON(data)
|
||||||
fmt.Println(unmarshaledItem.Name(), unmarshaledItem.Value())
|
fmt.Println(unmarshaledItem.Name(), unmarshaledItem.Value())
|
||||||
|
|
||||||
@@ -310,7 +310,7 @@ func main() {
|
|||||||
func NewRegistry[T comparable](items ...*Item[T]) *Registry[T]
|
func NewRegistry[T comparable](items ...*Item[T]) *Registry[T]
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/ABEXsYfJKMo)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -359,7 +359,7 @@ func main() {
|
|||||||
func (r *Registry[T]) Add(items ...*Item[T])
|
func (r *Registry[T]) Add(items ...*Item[T])
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/ABEXsYfJKMo)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -408,7 +408,7 @@ func main() {
|
|||||||
func (r *Registry[T]) Remove(value T) bool
|
func (r *Registry[T]) Remove(value T) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/dSG84wQ3TuC)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -454,7 +454,7 @@ func main() {
|
|||||||
func (r *Registry[T]) Update(value T, newName string) bool
|
func (r *Registry[T]) Update(value T, newName string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/Ol0moT1J9Xl)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -500,7 +500,7 @@ func main() {
|
|||||||
func (r *Registry[T]) GetByValue(value T) (*Item[T], bool)
|
func (r *Registry[T]) GetByValue(value T) (*Item[T], bool)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/niJ1U2KlE_m)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -520,9 +520,9 @@ const (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
registry := enum.NewRegistry[Status]()
|
registry := enum.NewRegistry[Status]()
|
||||||
item1 := enum.NewItem(Active, "Active")
|
item := enum.NewItem(Active, "Active")
|
||||||
|
|
||||||
registry.Add(item1, item2)
|
registry.Add(item)
|
||||||
|
|
||||||
if item, found := registry.GetByValue(Active); found {
|
if item, found := registry.GetByValue(Active); found {
|
||||||
fmt.Println("Found name by value:", item.Name())
|
fmt.Println("Found name by value:", item.Name())
|
||||||
@@ -543,7 +543,7 @@ func main() {
|
|||||||
func (r *Registry[T]) GetByName(name string) (*Item[T], bool)
|
func (r *Registry[T]) GetByName(name string) (*Item[T], bool)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/49ie_gpqH0m)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -586,7 +586,7 @@ func main() {
|
|||||||
func (r *Registry[T]) Items() []*Item[T]
|
func (r *Registry[T]) Items() []*Item[T]
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/lAJFAradbvQ)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -631,7 +631,7 @@ func main() {
|
|||||||
func (r *Registry[T]) Contains(value T) bool
|
func (r *Registry[T]) Contains(value T) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/_T-lPYkZn2j)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -673,7 +673,7 @@ func main() {
|
|||||||
func (r *Registry[T]) Size() int
|
func (r *Registry[T]) Size() int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/TeDArWhlQe2)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -721,7 +721,7 @@ func main() {
|
|||||||
func (r *Registry[T]) Range(fn func(*Item[T]) bool)
|
func (r *Registry[T]) Range(fn func(*Item[T]) bool)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/GPsZbQbefWN)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -745,7 +745,7 @@ func main() {
|
|||||||
item2 := enum.NewItem(Inactive, "Inactive")
|
item2 := enum.NewItem(Inactive, "Inactive")
|
||||||
registry.Add(item1, item2)
|
registry.Add(item1, item2)
|
||||||
|
|
||||||
registry.Range(func(item *Item[Status]) bool {
|
registry.Range(func(item *enum.Item[Status]) bool {
|
||||||
fmt.Println(item.Name(), item.Value())
|
fmt.Println(item.Name(), item.Value())
|
||||||
return true // continue iteration
|
return true // continue iteration
|
||||||
})
|
})
|
||||||
@@ -766,7 +766,7 @@ func main() {
|
|||||||
func (r *Registry[T]) SortedItems(less func(*Item[T], *Item[T]) bool) []*Item[T]
|
func (r *Registry[T]) SortedItems(less func(*Item[T], *Item[T]) bool) []*Item[T]
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/tN9RE_m_WEI)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -790,8 +790,8 @@ func main() {
|
|||||||
item2 := enum.NewItem(Active, "Active")
|
item2 := enum.NewItem(Active, "Active")
|
||||||
registry.Add(item1, item2)
|
registry.Add(item1, item2)
|
||||||
|
|
||||||
for _, item := range registry.SortedItems(func(i1, i2 *Item[Status]) bool {
|
for _, item := range registry.SortedItems(func(i1, i2 *enum.Item[Status]) bool {
|
||||||
return i1.value < i2.value
|
return i1.Value() < i2.Value()
|
||||||
}) {
|
}) {
|
||||||
fmt.Println(item.Name(), item.Value())
|
fmt.Println(item.Name(), item.Value())
|
||||||
}
|
}
|
||||||
@@ -812,7 +812,7 @@ func main() {
|
|||||||
func (r *Registry[T]) Filter(predicate func(*Item[T]) bool) []*Item[T]
|
func (r *Registry[T]) Filter(predicate func(*Item[T]) bool) []*Item[T]
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/uTUpTdcyoCU)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -836,7 +836,7 @@ func main() {
|
|||||||
item2 := enum.NewItem(Inactive, "Inactive")
|
item2 := enum.NewItem(Inactive, "Inactive")
|
||||||
registry.Add(item1, item2)
|
registry.Add(item1, item2)
|
||||||
|
|
||||||
activeItems := registry.Filter(func(item *Item[Status]) bool {
|
activeItems := registry.Filter(func(item *enum.Item[Status]) bool {
|
||||||
return item.Value() == Active
|
return item.Value() == Active
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -2357,8 +2357,8 @@ func main() {
|
|||||||
编辑
|
编辑
|
||||||
func ToMarkdownTable(data []map[string]interface{}, headerMap map[string]string, columnOrder []string) string
|
func ToMarkdownTable(data []map[string]interface{}, headerMap map[string]string, columnOrder []string) string
|
||||||
```
|
```
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
|
||||||
|
|
||||||
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/w_pSLfeyEB5)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
|||||||
@@ -514,7 +514,7 @@ func main() {
|
|||||||
func (s *Struct) TypeName() string
|
func (s *Struct) TypeName() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/SWLWd0XBaBb)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -531,7 +531,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
p := &Parent{Age: 11}
|
p := &Parent{Age: 11}
|
||||||
s := structs.New(p1)
|
s := structs.New(p)
|
||||||
|
|
||||||
fmt.Println(s.TypeName())
|
fmt.Println(s.TypeName())
|
||||||
|
|
||||||
|
|||||||
@@ -1552,6 +1552,8 @@ func IsChinaUnionPay(v string) bool
|
|||||||
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/yafpdxLiymu)</span></b>
|
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/yafpdxLiymu)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/validator"
|
"github.com/duke-git/lancet/v2/validator"
|
||||||
@@ -1580,9 +1582,11 @@ func main() {
|
|||||||
func IsPassport(passport, country string) bool
|
func IsPassport(passport, country string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/dvOiV2BW7Aw)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/duke-git/lancet/v2/validator"
|
"github.com/duke-git/lancet/v2/validator"
|
||||||
@@ -1617,7 +1621,7 @@ func main() {
|
|||||||
func IsChineseHMPassport(hmPassport string) bool
|
func IsChineseHMPassport(hmPassport string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/todo)</span></b>
|
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/xKG6spQTcY0)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
|
|||||||
@@ -33,6 +33,9 @@ import (
|
|||||||
- [ToJson](#ToJson)
|
- [ToJson](#ToJson)
|
||||||
- [ToMap](#ToMap)
|
- [ToMap](#ToMap)
|
||||||
- [ToPointer](#ToPointer)
|
- [ToPointer](#ToPointer)
|
||||||
|
- [ToPointers](#ToPointers)
|
||||||
|
- [FromPointer](#FromPointer)
|
||||||
|
- [FromPointers](#FromPointers)
|
||||||
- [ToString](#ToString)
|
- [ToString](#ToString)
|
||||||
- [StructToMap](#StructToMap)
|
- [StructToMap](#StructToMap)
|
||||||
- [MapToSlice](#MapToSlice)
|
- [MapToSlice](#MapToSlice)
|
||||||
@@ -456,6 +459,108 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### <span id="ToPointers">ToPointers</span>
|
||||||
|
|
||||||
|
<p>Convert a slice of values to a slice of pointers.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func ToPointers[T any](values []T) []*T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/ZUoXd2i5ZkV)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
strs := []string{"a", "b", "c"}
|
||||||
|
pointerStrs := convertor.ToPointers(strs)
|
||||||
|
fmt.Println(*pointerStrs[0])
|
||||||
|
fmt.Println(*pointerStrs[1])
|
||||||
|
fmt.Println(*pointerStrs[2])
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// a
|
||||||
|
// b
|
||||||
|
// c
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="FromPointer">FromPointer</span>
|
||||||
|
|
||||||
|
<p>Returns the value pointed to by the pointer.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func FromPointer[T any](ptr *T) T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/wAp90V7Zu6g)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
str := "abc"
|
||||||
|
strPtr := &str
|
||||||
|
result := convertor.FromPointer(strPtr)
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// abc
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### <span id="FromPointers">FromPointers</span>
|
||||||
|
|
||||||
|
<p>Convert a slice of pointers to a slice of values.</p>
|
||||||
|
|
||||||
|
<b>Signature:</b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
func FromPointers[T any](pointers []*T) []T
|
||||||
|
```
|
||||||
|
|
||||||
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/qIPsyYtNy3Q)</span></b>
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/duke-git/lancet/v2/convertor"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
strs := []string{"a", "b", "c"}
|
||||||
|
strPtr := []*string{&strs[0], &strs[1], &strs[2]}
|
||||||
|
|
||||||
|
result := convertor.FromPointers(strPtr)
|
||||||
|
|
||||||
|
fmt.Println(result[0])
|
||||||
|
fmt.Println(result[1])
|
||||||
|
fmt.Println(result[2])
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// a
|
||||||
|
// b
|
||||||
|
// c
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### <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>
|
||||||
@@ -572,7 +677,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="EncodeByte">EncodeByte</span>
|
### <span id="EncodeByte">EncodeByte</span>
|
||||||
|
|
||||||
<p>Encode data to byte slice.</p>
|
<p>Encode data to byte slice.</p>
|
||||||
@@ -638,7 +742,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### <span id="CopyProperties">CopyProperties</span>
|
### <span id="CopyProperties">CopyProperties</span>
|
||||||
|
|
||||||
<p>Copies each field from the source struct into the destination struct. Use json.Marshal/Unmarshal, so json tag should be set for fields of dst and src struct.</p>
|
<p>Copies each field from the source struct into the destination struct. Use json.Marshal/Unmarshal, so json tag should be set for fields of dst and src struct.</p>
|
||||||
@@ -857,8 +960,6 @@ func main() {
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### <span id="ToUrlBase64">ToUrlBase64</span>
|
### <span id="ToUrlBase64">ToUrlBase64</span>
|
||||||
|
|
||||||
<p>Convert a value to a string encoded in url Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON formatted string.</p>
|
<p>Convert a value to a string encoded in url Base64. Error data of type "error" will also be encoded, and complex structures will be converted to a JSON formatted string.</p>
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ import (
|
|||||||
func NewItem[T comparable](value T, name string) *Item[T]
|
func NewItem[T comparable](value T, name string) *Item[T]
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/8qNsLw01HD5)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -96,7 +96,7 @@ func main() {
|
|||||||
func NewItemsFromPairs[T comparable](pairs ...Pair[T]) []*Item[T]
|
func NewItemsFromPairs[T comparable](pairs ...Pair[T]) []*Item[T]
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/xKnoGa7gnev)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -139,7 +139,7 @@ func main() {
|
|||||||
func (it *Item[T]) Value() T
|
func (it *Item[T]) Value() T
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/xKnoGa7gnev)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -182,7 +182,7 @@ func main() {
|
|||||||
func (it *Item[T]) Name() string
|
func (it *Item[T]) Name() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/xKnoGa7gnev)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -225,7 +225,7 @@ func main() {
|
|||||||
func (it *Item[T]) Valid(checker ...func(T) bool) bool
|
func (it *Item[T]) Valid(checker ...func(T) bool) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/pA3lYY2VSm3)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -267,7 +267,7 @@ func (it *Item[T]) MarshalJSON() ([]byte, error)
|
|||||||
func (it *Item[T]) UnmarshalJSON(data []byte) error
|
func (it *Item[T]) UnmarshalJSON(data []byte) error
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/zIZEdAnneB5)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -290,7 +290,7 @@ func main() {
|
|||||||
data, _ := item.MarshalJSON()
|
data, _ := item.MarshalJSON()
|
||||||
fmt.Println(string(data))
|
fmt.Println(string(data))
|
||||||
|
|
||||||
var unmarshaledItem Item[Status]
|
var unmarshaledItem enum.Item[Status]
|
||||||
_ = unmarshaledItem.UnmarshalJSON(data)
|
_ = unmarshaledItem.UnmarshalJSON(data)
|
||||||
fmt.Println(unmarshaledItem.Name(), unmarshaledItem.Value())
|
fmt.Println(unmarshaledItem.Name(), unmarshaledItem.Value())
|
||||||
|
|
||||||
@@ -310,7 +310,7 @@ func main() {
|
|||||||
func NewRegistry[T comparable](items ...*Item[T]) *Registry[T]
|
func NewRegistry[T comparable](items ...*Item[T]) *Registry[T]
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/ABEXsYfJKMo)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -359,7 +359,7 @@ func main() {
|
|||||||
func (r *Registry[T]) Add(items ...*Item[T])
|
func (r *Registry[T]) Add(items ...*Item[T])
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/ABEXsYfJKMo)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -408,7 +408,7 @@ func main() {
|
|||||||
func (r *Registry[T]) Remove(value T) bool
|
func (r *Registry[T]) Remove(value T) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/dSG84wQ3TuC)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -454,7 +454,7 @@ func main() {
|
|||||||
func (r *Registry[T]) Update(value T, newName string) bool
|
func (r *Registry[T]) Update(value T, newName string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/Ol0moT1J9Xl)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -500,7 +500,7 @@ func main() {
|
|||||||
func (r *Registry[T]) GetByValue(value T) (*Item[T], bool)
|
func (r *Registry[T]) GetByValue(value T) (*Item[T], bool)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/niJ1U2KlE_m)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -520,9 +520,9 @@ const (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
registry := enum.NewRegistry[Status]()
|
registry := enum.NewRegistry[Status]()
|
||||||
item1 := enum.NewItem(Active, "Active")
|
item := enum.NewItem(Active, "Active")
|
||||||
|
|
||||||
registry.Add(item1, item2)
|
registry.Add(item)
|
||||||
|
|
||||||
if item, found := registry.GetByValue(Active); found {
|
if item, found := registry.GetByValue(Active); found {
|
||||||
fmt.Println("Found name by value:", item.Name())
|
fmt.Println("Found name by value:", item.Name())
|
||||||
@@ -543,7 +543,7 @@ func main() {
|
|||||||
func (r *Registry[T]) GetByName(name string) (*Item[T], bool)
|
func (r *Registry[T]) GetByName(name string) (*Item[T], bool)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/49ie_gpqH0m)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -586,7 +586,7 @@ func main() {
|
|||||||
func (r *Registry[T]) Items() []*Item[T]
|
func (r *Registry[T]) Items() []*Item[T]
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/lAJFAradbvQ)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -631,7 +631,7 @@ func main() {
|
|||||||
func (r *Registry[T]) Contains(value T) bool
|
func (r *Registry[T]) Contains(value T) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/_T-lPYkZn2j)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -673,7 +673,7 @@ func main() {
|
|||||||
func (r *Registry[T]) Size() int
|
func (r *Registry[T]) Size() int
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/TeDArWhlQe2)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -721,7 +721,7 @@ func main() {
|
|||||||
func (r *Registry[T]) Range(fn func(*Item[T]) bool)
|
func (r *Registry[T]) Range(fn func(*Item[T]) bool)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/GPsZbQbefWN)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -745,7 +745,7 @@ func main() {
|
|||||||
item2 := enum.NewItem(Inactive, "Inactive")
|
item2 := enum.NewItem(Inactive, "Inactive")
|
||||||
registry.Add(item1, item2)
|
registry.Add(item1, item2)
|
||||||
|
|
||||||
registry.Range(func(item *Item[Status]) bool {
|
registry.Range(func(item *enum.Item[Status]) bool {
|
||||||
fmt.Println(item.Name(), item.Value())
|
fmt.Println(item.Name(), item.Value())
|
||||||
return true // continue iteration
|
return true // continue iteration
|
||||||
})
|
})
|
||||||
@@ -766,7 +766,7 @@ func main() {
|
|||||||
func (r *Registry[T]) SortedItems(less func(*Item[T], *Item[T]) bool) []*Item[T]
|
func (r *Registry[T]) SortedItems(less func(*Item[T], *Item[T]) bool) []*Item[T]
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/tN9RE_m_WEI)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -790,8 +790,8 @@ func main() {
|
|||||||
item2 := enum.NewItem(Active, "Active")
|
item2 := enum.NewItem(Active, "Active")
|
||||||
registry.Add(item1, item2)
|
registry.Add(item1, item2)
|
||||||
|
|
||||||
for _, item := range registry.SortedItems(func(i1, i2 *Item[Status]) bool {
|
for _, item := range registry.SortedItems(func(i1, i2 *enum.Item[Status]) bool {
|
||||||
return i1.value < i2.value
|
return i1.Value() < i2.Value()
|
||||||
}) {
|
}) {
|
||||||
fmt.Println(item.Name(), item.Value())
|
fmt.Println(item.Name(), item.Value())
|
||||||
}
|
}
|
||||||
@@ -812,7 +812,7 @@ func main() {
|
|||||||
func (r *Registry[T]) Filter(predicate func(*Item[T]) bool) []*Item[T]
|
func (r *Registry[T]) Filter(predicate func(*Item[T]) bool) []*Item[T]
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/uTUpTdcyoCU)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -836,7 +836,7 @@ func main() {
|
|||||||
item2 := enum.NewItem(Inactive, "Inactive")
|
item2 := enum.NewItem(Inactive, "Inactive")
|
||||||
registry.Add(item1, item2)
|
registry.Add(item1, item2)
|
||||||
|
|
||||||
activeItems := registry.Filter(func(item *Item[Status]) bool {
|
activeItems := registry.Filter(func(item *enum.Item[Status]) bool {
|
||||||
return item.Value() == Active
|
return item.Value() == Active
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -81,7 +81,6 @@ import (
|
|||||||
- [FindValuesBy](#FindValuesBy)
|
- [FindValuesBy](#FindValuesBy)
|
||||||
- [ToMarkdownTable](#ToMarkdownTable)
|
- [ToMarkdownTable](#ToMarkdownTable)
|
||||||
|
|
||||||
|
|
||||||
<div STYLE="page-break-after: always;"></div>
|
<div STYLE="page-break-after: always;"></div>
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
@@ -2376,8 +2375,8 @@ func main() {
|
|||||||
编辑
|
编辑
|
||||||
func ToMarkdownTable(data []map[string]interface{}, headerMap map[string]string, columnOrder []string) string
|
func ToMarkdownTable(data []map[string]interface{}, headerMap map[string]string, columnOrder []string) string
|
||||||
```
|
```
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
|
||||||
|
|
||||||
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/w_pSLfeyEB5)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -2424,7 +2423,7 @@ func main() {
|
|||||||
result = maputil.ToMarkdownTable(data, nil, columnOrder)
|
result = maputil.ToMarkdownTable(data, nil, columnOrder)
|
||||||
fmt.Println(result)
|
fmt.Println(result)
|
||||||
|
|
||||||
// 输出:
|
// output:
|
||||||
// |salary|name|
|
// |salary|name|
|
||||||
// |---|---|
|
// |---|---|
|
||||||
// |50000|Alice|
|
// |50000|Alice|
|
||||||
|
|||||||
@@ -516,7 +516,7 @@ func main() {
|
|||||||
func (s *Struct) TypeName() string
|
func (s *Struct) TypeName() string
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/SWLWd0XBaBb)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -533,7 +533,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
p := &Parent{Age: 11}
|
p := &Parent{Age: 11}
|
||||||
s := structs.New(p1)
|
s := structs.New(p)
|
||||||
|
|
||||||
fmt.Println(s.TypeName())
|
fmt.Println(s.TypeName())
|
||||||
|
|
||||||
|
|||||||
@@ -1582,7 +1582,7 @@ func main() {
|
|||||||
func IsPassport(passport, country string) bool
|
func IsPassport(passport, country string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/dvOiV2BW7Aw)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
@@ -1619,7 +1619,7 @@ func main() {
|
|||||||
func IsChineseHMPassport(hmPassport string) bool
|
func IsChineseHMPassport(hmPassport string) bool
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/todo)</span></b>
|
<b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/xKG6spQTcY0)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
|
|||||||
19
enum/enum.go
19
enum/enum.go
@@ -27,6 +27,7 @@ type Item[T comparable] struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewItem creates a new enum item.
|
// NewItem creates a new enum item.
|
||||||
|
// Play: https://go.dev/play/p/8qNsLw01HD5
|
||||||
func NewItem[T comparable](value T, name string) *Item[T] {
|
func NewItem[T comparable](value T, name string) *Item[T] {
|
||||||
return &Item[T]{value: value, name: name}
|
return &Item[T]{value: value, name: name}
|
||||||
}
|
}
|
||||||
@@ -38,6 +39,7 @@ type Pair[T comparable] struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewItemsFromPairs creates enum items from a slice of Pair structs.
|
// NewItemsFromPairs creates enum items from a slice of Pair structs.
|
||||||
|
// Play: https://go.dev/play/p/xKnoGa7gnev
|
||||||
func NewItemsFromPairs[T comparable](pairs ...Pair[T]) []*Item[T] {
|
func NewItemsFromPairs[T comparable](pairs ...Pair[T]) []*Item[T] {
|
||||||
if len(pairs) == 0 {
|
if len(pairs) == 0 {
|
||||||
return []*Item[T]{}
|
return []*Item[T]{}
|
||||||
@@ -52,11 +54,13 @@ func NewItemsFromPairs[T comparable](pairs ...Pair[T]) []*Item[T] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Value returns the value of the enum item.
|
// Value returns the value of the enum item.
|
||||||
|
// Play: https://go.dev/play/p/xKnoGa7gnev
|
||||||
func (it *Item[T]) Value() T {
|
func (it *Item[T]) Value() T {
|
||||||
return it.value
|
return it.value
|
||||||
}
|
}
|
||||||
|
|
||||||
// Name returns the name of the enum item.
|
// Name returns the name of the enum item.
|
||||||
|
// Play: https://go.dev/play/p/xKnoGa7gnev
|
||||||
func (it *Item[T]) Name() string {
|
func (it *Item[T]) Name() string {
|
||||||
return it.name
|
return it.name
|
||||||
}
|
}
|
||||||
@@ -67,6 +71,7 @@ func (it *Item[T]) String() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Valid checks if the enum item is valid. If a custom check function is provided, it will be used to validate the value.
|
// Valid checks if the enum item is valid. If a custom check function is provided, it will be used to validate the value.
|
||||||
|
// Play: https://go.dev/play/p/pA3lYY2VSm3
|
||||||
func (it *Item[T]) Valid(checker ...func(T) bool) bool {
|
func (it *Item[T]) Valid(checker ...func(T) bool) bool {
|
||||||
if len(checker) > 0 {
|
if len(checker) > 0 {
|
||||||
return checker[0](it.value) && it.name != ""
|
return checker[0](it.value) && it.name != ""
|
||||||
@@ -76,6 +81,7 @@ func (it *Item[T]) Valid(checker ...func(T) bool) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MarshalJSON implements the json.Marshaler interface.
|
// MarshalJSON implements the json.Marshaler interface.
|
||||||
|
// Play: https://go.dev/play/p/zIZEdAnneB5
|
||||||
func (it *Item[T]) MarshalJSON() ([]byte, error) {
|
func (it *Item[T]) MarshalJSON() ([]byte, error) {
|
||||||
return json.Marshal(map[string]any{
|
return json.Marshal(map[string]any{
|
||||||
"value": it.value,
|
"value": it.value,
|
||||||
@@ -84,6 +90,7 @@ func (it *Item[T]) MarshalJSON() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UnmarshalJSON implements the json.Unmarshaler interface.
|
// UnmarshalJSON implements the json.Unmarshaler interface.
|
||||||
|
// Play: https://go.dev/play/p/zIZEdAnneB5
|
||||||
func (it *Item[T]) UnmarshalJSON(data []byte) error {
|
func (it *Item[T]) UnmarshalJSON(data []byte) error {
|
||||||
type alias struct {
|
type alias struct {
|
||||||
Value any `json:"value"`
|
Value any `json:"value"`
|
||||||
@@ -156,6 +163,7 @@ type Registry[T comparable] struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewRegistry creates a new enum registry.
|
// NewRegistry creates a new enum registry.
|
||||||
|
// Play: https://go.dev/play/p/ABEXsYfJKMo
|
||||||
func NewRegistry[T comparable](items ...*Item[T]) *Registry[T] {
|
func NewRegistry[T comparable](items ...*Item[T]) *Registry[T] {
|
||||||
r := &Registry[T]{
|
r := &Registry[T]{
|
||||||
values: make(map[T]*Item[T]),
|
values: make(map[T]*Item[T]),
|
||||||
@@ -169,6 +177,7 @@ func NewRegistry[T comparable](items ...*Item[T]) *Registry[T] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add adds enum items to the registry.
|
// Add adds enum items to the registry.
|
||||||
|
// Play: https://go.dev/play/p/ABEXsYfJKMo
|
||||||
func (r *Registry[T]) Add(items ...*Item[T]) {
|
func (r *Registry[T]) Add(items ...*Item[T]) {
|
||||||
r.mu.Lock()
|
r.mu.Lock()
|
||||||
defer r.mu.Unlock()
|
defer r.mu.Unlock()
|
||||||
@@ -188,6 +197,7 @@ func (r *Registry[T]) Add(items ...*Item[T]) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Remove removes an enum item from the registry by its value.
|
// Remove removes an enum item from the registry by its value.
|
||||||
|
// Play: https://go.dev/play/p/dSG84wQ3TuC
|
||||||
func (r *Registry[T]) Remove(value T) bool {
|
func (r *Registry[T]) Remove(value T) bool {
|
||||||
r.mu.Lock()
|
r.mu.Lock()
|
||||||
defer r.mu.Unlock()
|
defer r.mu.Unlock()
|
||||||
@@ -207,6 +217,7 @@ func (r *Registry[T]) Remove(value T) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update updates the name of an enum item in the registry by its value.
|
// Update updates the name of an enum item in the registry by its value.
|
||||||
|
// Play: https://go.dev/play/p/Ol0moT1J9Xl
|
||||||
func (r *Registry[T]) Update(value T, newName string) bool {
|
func (r *Registry[T]) Update(value T, newName string) bool {
|
||||||
r.mu.Lock()
|
r.mu.Lock()
|
||||||
defer r.mu.Unlock()
|
defer r.mu.Unlock()
|
||||||
@@ -221,6 +232,7 @@ func (r *Registry[T]) Update(value T, newName string) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetByValue retrieves an enum item by its value.
|
// GetByValue retrieves an enum item by its value.
|
||||||
|
// Play: https://go.dev/play/p/niJ1U2KlE_m
|
||||||
func (r *Registry[T]) GetByValue(value T) (*Item[T], bool) {
|
func (r *Registry[T]) GetByValue(value T) (*Item[T], bool) {
|
||||||
r.mu.RLock()
|
r.mu.RLock()
|
||||||
defer r.mu.RUnlock()
|
defer r.mu.RUnlock()
|
||||||
@@ -230,6 +242,7 @@ func (r *Registry[T]) GetByValue(value T) (*Item[T], bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetByName retrieves an enum item by its name.
|
// GetByName retrieves an enum item by its name.
|
||||||
|
// Play: https://go.dev/play/p/49ie_gpqH0m
|
||||||
func (r *Registry[T]) GetByName(name string) (*Item[T], bool) {
|
func (r *Registry[T]) GetByName(name string) (*Item[T], bool) {
|
||||||
r.mu.RLock()
|
r.mu.RLock()
|
||||||
defer r.mu.RUnlock()
|
defer r.mu.RUnlock()
|
||||||
@@ -239,6 +252,7 @@ func (r *Registry[T]) GetByName(name string) (*Item[T], bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Items returns a slice of all enum items in the registry.
|
// Items returns a slice of all enum items in the registry.
|
||||||
|
// Play: https://go.dev/play/p/lAJFAradbvQ
|
||||||
func (r *Registry[T]) Items() []*Item[T] {
|
func (r *Registry[T]) Items() []*Item[T] {
|
||||||
r.mu.RLock()
|
r.mu.RLock()
|
||||||
defer r.mu.RUnlock()
|
defer r.mu.RUnlock()
|
||||||
@@ -249,12 +263,14 @@ func (r *Registry[T]) Items() []*Item[T] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Contains checks if an enum item with the given value exists in the registry.
|
// Contains checks if an enum item with the given value exists in the registry.
|
||||||
|
// Play: https://go.dev/play/p/_T-lPYkZn2j
|
||||||
func (r *Registry[T]) Contains(value T) bool {
|
func (r *Registry[T]) Contains(value T) bool {
|
||||||
_, ok := r.GetByValue(value)
|
_, ok := r.GetByValue(value)
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
|
|
||||||
// Size returns the number of enum items in the registry.
|
// Size returns the number of enum items in the registry.
|
||||||
|
// Play: https://go.dev/play/p/TeDArWhlQe2
|
||||||
func (r *Registry[T]) Size() int {
|
func (r *Registry[T]) Size() int {
|
||||||
r.mu.RLock()
|
r.mu.RLock()
|
||||||
defer r.mu.RUnlock()
|
defer r.mu.RUnlock()
|
||||||
@@ -263,6 +279,7 @@ func (r *Registry[T]) Size() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Range iterates over all enum items in the registry and applies the given function.
|
// Range iterates over all enum items in the registry and applies the given function.
|
||||||
|
// Play: https://go.dev/play/p/GPsZbQbefWN
|
||||||
func (r *Registry[T]) Range(fn func(*Item[T]) bool) {
|
func (r *Registry[T]) Range(fn func(*Item[T]) bool) {
|
||||||
r.mu.RLock()
|
r.mu.RLock()
|
||||||
defer r.mu.RUnlock()
|
defer r.mu.RUnlock()
|
||||||
@@ -275,6 +292,7 @@ func (r *Registry[T]) Range(fn func(*Item[T]) bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SortedItems returns a slice of all enum items sorted by the given less function.
|
// SortedItems returns a slice of all enum items sorted by the given less function.
|
||||||
|
// Play: https://go.dev/play/p/tN9RE_m_WEI
|
||||||
func (r *Registry[T]) SortedItems(less func(*Item[T], *Item[T]) bool) []*Item[T] {
|
func (r *Registry[T]) SortedItems(less func(*Item[T], *Item[T]) bool) []*Item[T] {
|
||||||
items := r.Items()
|
items := r.Items()
|
||||||
sort.Slice(items, func(i, j int) bool {
|
sort.Slice(items, func(i, j int) bool {
|
||||||
@@ -284,6 +302,7 @@ func (r *Registry[T]) SortedItems(less func(*Item[T], *Item[T]) bool) []*Item[T]
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Filter returns a slice of enum items that satisfy the given predicate function.
|
// Filter returns a slice of enum items that satisfy the given predicate function.
|
||||||
|
// Play: https://go.dev/play/p/uTUpTdcyoCU
|
||||||
func (r *Registry[T]) Filter(predicate func(*Item[T]) bool) []*Item[T] {
|
func (r *Registry[T]) Filter(predicate func(*Item[T]) bool) []*Item[T] {
|
||||||
r.mu.RLock()
|
r.mu.RLock()
|
||||||
defer r.mu.RUnlock()
|
defer r.mu.RUnlock()
|
||||||
|
|||||||
@@ -683,7 +683,7 @@ func FindValuesBy[K comparable, V any](m map[K]V, predicate func(key K, value V)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ToMarkdownTable converts a slice of maps to a Markdown table.
|
// ToMarkdownTable converts a slice of maps to a Markdown table.
|
||||||
// Play: ttps://go.dev/play/p/todo
|
// Play: ttps://go.dev/play/p/w_pSLfeyEB5
|
||||||
func ToMarkdownTable(data []map[string]interface{}, headerMap map[string]string, columnOrder []string) string {
|
func ToMarkdownTable(data []map[string]interface{}, headerMap map[string]string, columnOrder []string) string {
|
||||||
if len(data) == 0 {
|
if len(data) == 0 {
|
||||||
return "| |\n|---|\n"
|
return "| |\n|---|\n"
|
||||||
|
|||||||
@@ -130,7 +130,7 @@ func ToMap(v any) (map[string]any, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TypeName return struct type name
|
// TypeName return struct type name
|
||||||
// Play: https://go.dev/play/p/todo
|
// Play: https://go.dev/play/p/SWLWd0XBaBb
|
||||||
func (s *Struct) TypeName() string {
|
func (s *Struct) TypeName() string {
|
||||||
return s.rtype.Name()
|
return s.rtype.Name()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -634,7 +634,7 @@ func IsChinaUnionPay(cardNo string) bool {
|
|||||||
|
|
||||||
// IsPassport checks if the passport number is valid for a given country.
|
// IsPassport checks if the passport number is valid for a given country.
|
||||||
// country is a two-letter country code (ISO 3166-1 alpha-2).
|
// country is a two-letter country code (ISO 3166-1 alpha-2).
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/dvOiV2BW7Aw
|
||||||
func IsPassport(passport, country string) bool {
|
func IsPassport(passport, country string) bool {
|
||||||
if matcher, ok := passportMatcher[country]; ok {
|
if matcher, ok := passportMatcher[country]; ok {
|
||||||
return matcher.MatchString(passport)
|
return matcher.MatchString(passport)
|
||||||
@@ -645,7 +645,7 @@ func IsPassport(passport, country string) bool {
|
|||||||
|
|
||||||
// IsChineseHMPassport checks if the string is a valid Chinese Hong Kong and Macau Travel Permit number.
|
// IsChineseHMPassport checks if the string is a valid Chinese Hong Kong and Macau Travel Permit number.
|
||||||
// Chinese Hong Kong and Macau Travel Permit format: C or M + 8 digits (e.g., C12345678, M12345678).
|
// Chinese Hong Kong and Macau Travel Permit format: C or M + 8 digits (e.g., C12345678, M12345678).
|
||||||
// Play: https://go.dev/play/p/TODO
|
// Play: https://go.dev/play/p/xKG6spQTcY0
|
||||||
func IsChineseHMPassport(hmPassport string) bool {
|
func IsChineseHMPassport(hmPassport string) bool {
|
||||||
return chineseHMPassportMatcher.MatchString(hmPassport)
|
return chineseHMPassportMatcher.MatchString(hmPassport)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user