1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

Compare commits

..

4 Commits

Author SHA1 Message Date
dudaodong
889d0cc3d6 doc: update readme file 2025-11-01 21:45:56 +08:00
dudaodong
aa05027cab doc: update readme file 2025-11-01 21:44:03 +08:00
dudaodong
fabc3483ed doc: update readme file 2025-11-01 21:37:15 +08:00
dudaodong
30363242bb doc: update document for version v2.3.8 2025-11-01 21:25:10 +08:00
18 changed files with 584 additions and 168 deletions

118
README.md
View File

@@ -84,6 +84,8 @@ func main() {
- [Cryptor](#user-content-cryptor) - [Cryptor](#user-content-cryptor)
- [Datetime](#user-content-datetime) - [Datetime](#user-content-datetime)
- [Datastructure](#user-content-datastructure) - [Datastructure](#user-content-datastructure)
- [EventBus](#user-content-eventbus)
- [Enum](#user-content-enum)
- [Fileutil](#user-content-fileutil) - [Fileutil](#user-content-fileutil)
- [Formatter](#user-content-formatter) - [Formatter](#user-content-formatter)
- [Function](#user-content-function) - [Function](#user-content-function)
@@ -313,6 +315,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 +735,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,7 +768,70 @@ 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="fileutil"> 9. Fileutil package implements some basic functions for file operations. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="enum"> 10. Package enum provides a simple enum implementation. &nbsp; &nbsp; &nbsp; &nbsp;<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.
[[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.
[[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.
[[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.
[[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.
[[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.
[[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.
[[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.
[[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.
[[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.
[[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.
[[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.
[[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.
[[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"> 11. Fileutil package implements some basic functions for file operations. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/fileutil" import "github.com/duke-git/lancet/v2/fileutil"
@@ -859,7 +933,7 @@ import "github.com/duke-git/lancet/v2/fileutil"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#GetExeOrDllVersion)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/fileutil.md#GetExeOrDllVersion)]
[[play](https://go.dev/play/p/iLRrDBhE38E)] [[play](https://go.dev/play/p/iLRrDBhE38E)]
<h3 id="formatter"> 10. Formatter contains some functions for data formatting. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="formatter"> 12. Formatter contains some functions for data formatting. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/formatter" import "github.com/duke-git/lancet/v2/formatter"
@@ -889,7 +963,7 @@ import "github.com/duke-git/lancet/v2/formatter"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/formatter.md#ParseBinaryBytes)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/formatter.md#ParseBinaryBytes)]
[[play](https://go.dev/play/p/69v1tTT62x8)] [[play](https://go.dev/play/p/69v1tTT62x8)]
<h3 id="function"> 11. Function package can control the flow of function execution and support part of functional programming.&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="function"> 13. Function package can control the flow of function execution and support part of functional programming.&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/function" import "github.com/duke-git/lancet/v2/function"
@@ -952,7 +1026,7 @@ import "github.com/duke-git/lancet/v2/function"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Watcher)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/function.md#Watcher)]
[[play](https://go.dev/play/p/l2yrOpCLd1I)] [[play](https://go.dev/play/p/l2yrOpCLd1I)]
<h3 id="maputil"> 12. Maputil package includes some functions to manipulate map.&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="maputil"> 14. Maputil package includes some functions to manipulate map.&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/maputil" import "github.com/duke-git/lancet/v2/maputil"
@@ -1127,9 +1201,9 @@ 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. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="mathutil"> 15. Mathutil package implements some functions for math calculation. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/mathutil" import "github.com/duke-git/lancet/v2/mathutil"
@@ -1240,7 +1314,7 @@ import "github.com/duke-git/lancet/v2/mathutil"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#Combination)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/mathutil.md#Combination)]
[[play](https://go.dev/play/p/ENFQRDQUFi9)] [[play](https://go.dev/play/p/ENFQRDQUFi9)]
<h3 id="netutil"> 14. Netutil package contains functions to get net information and send http request. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="netutil"> 16. Netutil package contains functions to get net information and send http request. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/netutil" import "github.com/duke-git/lancet/v2/netutil"
@@ -1319,7 +1393,7 @@ import "github.com/duke-git/lancet/v2/netutil"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/netutil.md#AddQueryParams)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/netutil.md#AddQueryParams)]
[[play](https://go.dev/play/p/JLXl1hZK7l4)] [[play](https://go.dev/play/p/JLXl1hZK7l4)]
<h3 id="pointer"> 15. Pointer package contains some util functions to operate go pointer. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="pointer"> 17. Pointer package contains some util functions to operate go pointer. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/pointer" import "github.com/duke-git/lancet/v2/pointer"
@@ -1343,7 +1417,7 @@ import "github.com/duke-git/lancet/v2/pointer"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/pointer.md#UnwrapOrDefault)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/pointer.md#UnwrapOrDefault)]
[[play](https://go.dev/play/p/ZnGIHf8_o4E)] [[play](https://go.dev/play/p/ZnGIHf8_o4E)]
<h3 id="random"> 16. Random package implements some basic functions to generate random int and string. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="random"> 18. Random package implements some basic functions to generate random int and string. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/random" import "github.com/duke-git/lancet/v2/random"
@@ -1409,7 +1483,7 @@ import "github.com/duke-git/lancet/v2/random"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/random.md#RandNumberOfLength)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/random.md#RandNumberOfLength)]
[[play](https://go.dev/play/p/oyZbuV7bu7b)] [[play](https://go.dev/play/p/oyZbuV7bu7b)]
<h3 id="retry"> 17. Retry package is for executing a function repeatedly until it was successful or canceled by the context. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="retry"> 19. Retry package is for executing a function repeatedly until it was successful or canceled by the context. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/retry" import "github.com/duke-git/lancet/v2/retry"
@@ -1444,7 +1518,7 @@ import "github.com/duke-git/lancet/v2/retry"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#RetryWithExponentialWithJitterBackoff)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#RetryWithExponentialWithJitterBackoff)]
[[play](https://go.dev/play/p/xp1avQmn16X)] [[play](https://go.dev/play/p/xp1avQmn16X)]
<h3 id="slice"> 18. Slice contains some functions to manipulate slice. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="slice"> 20. Slice contains some functions to manipulate slice. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/slice" import "github.com/duke-git/lancet/v2/slice"
@@ -1716,7 +1790,7 @@ import "github.com/duke-git/lancet/v2/slice"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#ConcatBy)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#ConcatBy)]
[[play](https://go.dev/play/p/6QcUpcY4UMW)] [[play](https://go.dev/play/p/6QcUpcY4UMW)]
<h3 id="stream"> 19. Stream package implements a sequence of elements supporting sequential and operations. this package is an experiment to explore if stream in go can work as the way java does. its function is very limited. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="stream"> 21. Stream package implements a sequence of elements supporting sequential and operations. this package is an experiment to explore if stream in go can work as the way java does. its function is very limited. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/stream" import "github.com/duke-git/lancet/v2/stream"
@@ -1815,7 +1889,7 @@ import "github.com/duke-git/lancet/v2/stream"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/stream.md#LastIndexOf)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/stream.md#LastIndexOf)]
[[play](https://go.dev/play/p/CjeoNw2eac_G)] [[play](https://go.dev/play/p/CjeoNw2eac_G)]
<h3 id="structs"> 20. Structs package provides several high level functions to manipulate struct, tag, and field. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="structs"> 22. Structs package provides several high level functions to manipulate struct, tag, and field. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/structs" import "github.com/duke-git/lancet/v2/structs"
@@ -1867,9 +1941,9 @@ 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. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="strutil"> 23. Strutil package contains some functions to manipulate string. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/strutil" import "github.com/duke-git/lancet/v2/strutil"
@@ -2021,7 +2095,7 @@ import "github.com/duke-git/lancet/v2/strutil"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#FindAllOccurrences)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#FindAllOccurrences)]
[[play](https://go.dev/play/p/uvyA6azGLB1)] [[play](https://go.dev/play/p/uvyA6azGLB1)]
<h3 id="system"> 22. System package contain some functions about os, runtime, shell command. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="system"> 24. System package contain some functions about os, runtime, shell command. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/system" import "github.com/duke-git/lancet/v2/system"
@@ -2069,7 +2143,7 @@ import "github.com/duke-git/lancet/v2/system"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/system.md#GetProcessInfo)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/system.md#GetProcessInfo)]
[[play](https://go.dev/play/p/NQDVywEYYx7)] [[play](https://go.dev/play/p/NQDVywEYYx7)]
<h3 id="tuple"> 23. Tuple package implements tuple data type and some operations on it. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="tuple"> 25. Tuple package implements tuple data type and some operations on it. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/tuple" import "github.com/duke-git/lancet/v2/tuple"
@@ -2186,7 +2260,7 @@ import "github.com/duke-git/lancet/v2/tuple"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/tuple.md#Unzip10)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/tuple.md#Unzip10)]
[[play](https://go.dev/play/p/-taQB6Wfre_z)] [[play](https://go.dev/play/p/-taQB6Wfre_z)]
<h3 id="validator"> 24. Validator package contains some functions for data validation. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="validator"> 26. Validator package contains some functions for data validation. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/validator" import "github.com/duke-git/lancet/v2/validator"
@@ -2328,12 +2402,12 @@ 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. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3> <h3 id="xerror"> 27. Xerror package implements helpers for errors. &nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">index</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/xerror" import "github.com/duke-git/lancet/v2/xerror"

View File

@@ -83,6 +83,8 @@ func main() {
- [Cryptor](#user-content-cryptor) - [Cryptor](#user-content-cryptor)
- [Datetime](#user-content-datetime) - [Datetime](#user-content-datetime)
- [Datastructure](#user-content-datastructure) - [Datastructure](#user-content-datastructure)
- [EventBus](#user-content-eventbus)
- [Enum](#user-content-enum)
- [Fileutil](#user-content-fileutil) - [Fileutil](#user-content-fileutil)
- [Formatter](#user-content-formatter) - [Formatter](#user-content-formatter)
- [Function](#user-content-function) - [Function](#user-content-function)
@@ -312,6 +314,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,7 +778,70 @@ 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="fileutil"> 10. fileutil 包含文件基本操作。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="enum"> 10. Enum实现一个简单枚举工具包。. &nbsp; &nbsp; &nbsp; &nbsp;<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#NewItem)]
[[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>** : 返回枚举项的值。
[[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 接口。
[[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 定义了一个通用的枚举注册表结构体。
[[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>** : 向枚举注册表添加枚举项。
[[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>** : 在枚举注册表中删除枚举项。
[[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>** : 在枚举注册表中更新枚举项。
[[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>** : 在枚举注册表中通过值获取枚举项。
[[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>** : 在枚举注册表中通过名称获取枚举项。
[[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>** : 返回枚举注册表中的枚举项。
[[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>** : 检查注册表中是否存在具有给定值的枚举项。
[[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>** : 返回注册表中枚举项的数目。
[[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>** : 返回按给定比较函数排序的所有枚举项的切片。
[[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>** : 返回满足给定谓词函数的枚举项切片。
[[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"> 11. fileutil 包含文件基本操作。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/fileutil" import "github.com/duke-git/lancet/v2/fileutil"
@@ -869,7 +943,7 @@ import "github.com/duke-git/lancet/v2/fileutil"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/fileutil.md#GetExeOrDllVersion)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/fileutil.md#GetExeOrDllVersion)]
[[play](https://go.dev/play/p/iLRrDBhE38E)] [[play](https://go.dev/play/p/iLRrDBhE38E)]
<h3 id="formatter"> 11. formatter 格式化器包含一些数据格式化处理方法。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="formatter"> 12. formatter 格式化器包含一些数据格式化处理方法。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/formatter" import "github.com/duke-git/lancet/v2/formatter"
@@ -899,7 +973,7 @@ import "github.com/duke-git/lancet/v2/formatter"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/formatter.md#ParseBinaryBytes)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/formatter.md#ParseBinaryBytes)]
[[play](https://go.dev/play/p/69v1tTT62x8)] [[play](https://go.dev/play/p/69v1tTT62x8)]
<h3 id="function"> 12. function 函数包控制函数执行流程,包含部分函数式编程。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="function"> 13. function 函数包控制函数执行流程,包含部分函数式编程。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/function" import "github.com/duke-git/lancet/v2/function"
@@ -962,7 +1036,7 @@ import "github.com/duke-git/lancet/v2/function"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Watcher)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/function.md#Watcher)]
[[play](https://go.dev/play/p/l2yrOpCLd1I)] [[play](https://go.dev/play/p/l2yrOpCLd1I)]
<h3 id="maputil"> 13. maputil 包括一些操作 map 的函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="maputil"> 14. maputil 包括一些操作 map 的函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/maputil" import "github.com/duke-git/lancet/v2/maputil"
@@ -1137,9 +1211,9 @@ 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 包实现了一些数学计算的函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="mathutil"> 15. mathutil 包实现了一些数学计算的函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/mathutil" import "github.com/duke-git/lancet/v2/mathutil"
@@ -1250,7 +1324,7 @@ import "github.com/duke-git/lancet/v2/mathutil"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/mathutil.md#Combination)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/mathutil.md#Combination)]
[[play](https://go.dev/play/p/ENFQRDQUFi9)] [[play](https://go.dev/play/p/ENFQRDQUFi9)]
<h3 id="netutil"> 15. netutil 网络包支持获取 ip 地址,发送 http 请求。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="netutil"> 16. netutil 网络包支持获取 ip 地址,发送 http 请求。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/netutil" import "github.com/duke-git/lancet/v2/netutil"
@@ -1329,7 +1403,7 @@ import "github.com/duke-git/lancet/v2/netutil"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/netutil.md#AddQueryParams)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/netutil.md#AddQueryParams)]
[[play](https://go.dev/play/p/JLXl1hZK7l4)] [[play](https://go.dev/play/p/JLXl1hZK7l4)]
<h3 id="pointer"> 16. pointer 包支持一些指针类型的操作。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="pointer"> 17. pointer 包支持一些指针类型的操作。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/pointer" import "github.com/duke-git/lancet/v2/pointer"
@@ -1353,7 +1427,7 @@ import "github.com/duke-git/lancet/v2/pointer"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/pointer.md#UnwrapOrDefault)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/pointer.md#UnwrapOrDefault)]
[[play](https://go.dev/play/p/ZnGIHf8_o4E)] [[play](https://go.dev/play/p/ZnGIHf8_o4E)]
<h3 id="random"> 17. random 随机数生成器包,可以生成随机[]bytes, int, string。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="random"> 18. random 随机数生成器包,可以生成随机[]bytes, int, string。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/random" import "github.com/duke-git/lancet/v2/random"
@@ -1419,7 +1493,7 @@ import "github.com/duke-git/lancet/v2/random"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/random.md#RandNumberOfLength)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/random.md#RandNumberOfLength)]
[[play](https://go.dev/play/p/oyZbuV7bu7b)] [[play](https://go.dev/play/p/oyZbuV7bu7b)]
<h3 id="retry"> 18. retry 重试执行函数直到函数运行成功或被 context cancel。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="retry"> 19. retry 重试执行函数直到函数运行成功或被 context cancel。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/retry" import "github.com/duke-git/lancet/v2/retry"
@@ -1451,7 +1525,7 @@ import "github.com/duke-git/lancet/v2/retry"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#RetryWithExponentialWithJitterBackoff)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#RetryWithExponentialWithJitterBackoff)]
[[play](https://go.dev/play/p/xp1avQmn16X)] [[play](https://go.dev/play/p/xp1avQmn16X)]
<h3 id="slice"> 19. slice 包含操作切片的方法集合。&nbsp; &nbsp; &nbsp; &nbsp; <a href="#index">回到目录</a></h3> <h3 id="slice"> 20. slice 包含操作切片的方法集合。&nbsp; &nbsp; &nbsp; &nbsp; <a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/slice" import "github.com/duke-git/lancet/v2/slice"
@@ -1725,7 +1799,7 @@ import "github.com/duke-git/lancet/v2/slice"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#ConcatBy)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#ConcatBy)]
[[play](https://go.dev/play/p/6QcUpcY4UMW)] [[play](https://go.dev/play/p/6QcUpcY4UMW)]
<h3 id="stream"> 20. stream 流,该包仅验证简单的 stream 实现,功能有限。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="stream"> 21. stream 流,该包仅验证简单的 stream 实现,功能有限。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/stream" import "github.com/duke-git/lancet/v2/stream"
@@ -1821,7 +1895,7 @@ import "github.com/duke-git/lancet/v2/stream"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/stream.md#LastIndexOf)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/stream.md#LastIndexOf)]
[[play](https://go.dev/play/p/CjeoNw2eac_G)] [[play](https://go.dev/play/p/CjeoNw2eac_G)]
<h3 id="structs"> 21. structs 提供操作 struct, tag, field 的相关函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="structs"> 22. structs 提供操作 struct, tag, field 的相关函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/structs" import "github.com/duke-git/lancet/v2/structs"
@@ -1873,9 +1947,9 @@ 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 包含字符串处理的相关函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="strutil"> 23. strutil 包含字符串处理的相关函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/strutil" import "github.com/duke-git/lancet/v2/strutil"
@@ -2028,7 +2102,7 @@ import "github.com/duke-git/lancet/v2/strutil"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#FindAllOccurrences)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#FindAllOccurrences)]
[[play](https://go.dev/play/p/uvyA6azGLB1)] [[play](https://go.dev/play/p/uvyA6azGLB1)]
<h3 id="system"> 23. system 包含 os, runtime, shell command 的相关函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="system"> 24. system 包含 os, runtime, shell command 的相关函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/system" import "github.com/duke-git/lancet/v2/system"
@@ -2076,7 +2150,7 @@ import "github.com/duke-git/lancet/v2/system"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/system.md#GetProcessInfo)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/system.md#GetProcessInfo)]
[[play](https://go.dev/play/p/NQDVywEYYx7)] [[play](https://go.dev/play/p/NQDVywEYYx7)]
<h3 id="tuple"> 24. Tuple 包实现一个元组数据类型。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="tuple"> 25. Tuple 包实现一个元组数据类型。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/tuple" import "github.com/duke-git/lancet/v2/tuple"
@@ -2193,7 +2267,7 @@ import "github.com/duke-git/lancet/v2/tuple"
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/tuple.md#Unzip10)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/tuple.md#Unzip10)]
[[play](https://go.dev/play/p/-taQB6Wfre_z)] [[play](https://go.dev/play/p/-taQB6Wfre_z)]
<h3 id="validator"> 25. validator 验证器包,包含常用字符串格式验证函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3> <h3 id="validator"> 26. validator 验证器包,包含常用字符串格式验证函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/validator" import "github.com/duke-git/lancet/v2/validator"
@@ -2335,11 +2409,12 @@ 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 包实现一些错误处理函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
<h3 id="xerror"> 27. xerror 包实现一些错误处理函数。&nbsp; &nbsp; &nbsp; &nbsp;<a href="#index">回到目录</a></h3>
```go ```go
import "github.com/duke-git/lancet/v2/xerror" import "github.com/duke-git/lancet/v2/xerror"

View File

@@ -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 {

View File

@@ -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

View File

@@ -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>
@@ -1179,4 +1284,4 @@ func main() {
// Output: // Output:
// 9876543210 // 9876543210
} }
``` ```

View File

@@ -1,12 +1,12 @@
# Enum # Enum
Enum实现一个简单枚举工具包。 Enum 实现一个简单枚举工具包。
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 源码: ## 源码:
- [https://github.com/duke-git/lancet/blob/main/enum/enum.go](https://github.com/duke-git/lancet/blob/main/enum/enum.go) - [https://github.com/duke-git/lancet/blob/main/enum/enum.go](https://github.com/duke-git/lancet/blob/main/enum/enum.go)
<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
}) })
@@ -847,4 +847,4 @@ func main() {
// Output: // Output:
// Active 1 // Active 1
} }
``` ```

View File

@@ -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
@@ -2410,4 +2410,4 @@ func main() {
// |50000|Alice| // |50000|Alice|
// |60000|Bob| // |60000|Bob|
} }
``` ```

View File

@@ -69,7 +69,7 @@ func main() {
} }
p1 := &People{Name: "11"} p1 := &People{Name: "11"}
s := structs.New(p1) s := structs.New(p1)
fmt.Println(s.ToMap()) fmt.Println(s.ToMap())
// Output: // Output:
@@ -261,7 +261,7 @@ func main() {
tag := n.Tag() tag := n.Tag()
fmt.Println(tag.Name) fmt.Println(tag.Name)
// Output: // Output:
// name // name
} }
@@ -295,10 +295,10 @@ func main() {
s := structs.New(p1) s := structs.New(p1)
n, _ := s.Field("Name") n, _ := s.Field("Name")
fmt.Println(n.Value()) fmt.Println(n.Value())
// Output: // Output:
// 111 // 111
} }
``` ```
@@ -338,11 +338,11 @@ func main() {
s := structs.New(c1) s := structs.New(c1)
n, _ := s.Field("Name") n, _ := s.Field("Name")
a, _ := s.Field("Age") a, _ := s.Field("Age")
fmt.Println(n.IsEmbedded()) fmt.Println(n.IsEmbedded())
fmt.Println(a.IsEmbedded()) fmt.Println(a.IsEmbedded())
// Output: // Output:
// true // true
// false // false
} }
@@ -377,11 +377,11 @@ func main() {
s := structs.New(p1) s := structs.New(p1)
n, _ := s.Field("Name") n, _ := s.Field("Name")
a, _ := s.Field("age") a, _ := s.Field("age")
fmt.Println(n.IsExported()) fmt.Println(n.IsExported())
fmt.Println(a.IsExported()) fmt.Println(a.IsExported())
// Output: // Output:
// true // true
// false // false
} }
@@ -416,11 +416,11 @@ func main() {
s := structs.New(p1) s := structs.New(p1)
n, _ := s.Field("Name") n, _ := s.Field("Name")
a, _ := s.Field("Age") a, _ := s.Field("Age")
fmt.Println(n.IsZero()) fmt.Println(n.IsZero())
fmt.Println(a.IsZero()) fmt.Println(a.IsZero())
// Output: // Output:
// true // true
// false // false
} }
@@ -455,11 +455,11 @@ func main() {
s := structs.New(p1) s := structs.New(p1)
n, _ := s.Field("Name") n, _ := s.Field("Name")
a, _ := s.Field("Age") a, _ := s.Field("Age")
fmt.Println(n.Name()) fmt.Println(n.Name())
fmt.Println(a.Name()) fmt.Println(a.Name())
// Output: // Output:
// Name // Name
// Age // Age
} }
@@ -494,11 +494,11 @@ func main() {
s := structs.New(p1) s := structs.New(p1)
n, _ := s.Field("Name") n, _ := s.Field("Name")
a, _ := s.Field("Age") a, _ := s.Field("Age")
fmt.Println(n.Kind()) fmt.Println(n.Kind())
fmt.Println(a.Kind()) fmt.Println(a.Kind())
// Output: // Output:
// string // string
// int // int
} }
@@ -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
@@ -529,13 +529,13 @@ func main() {
Name string Name string
Age int Age int
} }
p := &Parent{Age: 11} p := &Parent{Age: 11}
s := structs.New(p1) s := structs.New(p)
fmt.Println(s.TypeName()) fmt.Println(s.TypeName())
// Output: // Output:
// Parent // Parent
} }
``` ```
@@ -569,10 +569,10 @@ func main() {
p1 := &Parent{arr: []int{1, 2, 3}} p1 := &Parent{arr: []int{1, 2, 3}}
s := structs.New(p1) s := structs.New(p1)
a, _ := s.Field("arr") a, _ := s.Field("arr")
fmt.Println(a.IsSlice()) fmt.Println(a.IsSlice())
// Output: // Output:
// true // true
} }
``` ```
@@ -608,12 +608,12 @@ func main() {
s := structs.New(p1) s := structs.New(p1)
n, _ := s.Field("Name") n, _ := s.Field("Name")
a, _ := s.Field("arr") a, _ := s.Field("arr")
fmt.Println(n.IsTargetType(reflect.String)) fmt.Println(n.IsTargetType(reflect.String))
fmt.Println(a.IsTargetType(reflect.Slice)) fmt.Println(a.IsTargetType(reflect.Slice))
// Output: // Output:
// true // true
// true // true
} }
``` ```

View File

@@ -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 (
@@ -1645,4 +1649,4 @@ func main() {
// false // false
// false // false
} }
``` ```

View File

@@ -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>
@@ -748,7 +851,7 @@ func main() {
// Output: // Output:
// true // true
// true // true
} }
``` ```
@@ -781,7 +884,7 @@ func main() {
// Output: // Output:
// true // true
// hello // hello
} }
``` ```
@@ -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>
@@ -1147,4 +1248,4 @@ func main() {
// Output: // Output:
// 9876543210 // 9876543210
} }
``` ```

View File

@@ -6,7 +6,7 @@ Package enum provides a simple enum implementation.
## Source: ## Source:
- [https://github.com/duke-git/lancet/blob/main/enum/enum.go](https://github.com/duke-git/lancet/blob/main/enum/enum.go) - [https://github.com/duke-git/lancet/blob/main/enum/enum.go](https://github.com/duke-git/lancet/blob/main/enum/enum.go)
<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>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
}) })
@@ -847,4 +847,4 @@ func main() {
// Output: // Output:
// Active 1 // Active 1
} }
``` ```

View File

@@ -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,10 +2423,10 @@ 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|
// |60000|Bob| // |60000|Bob|
} }
``` ```

View File

@@ -70,7 +70,7 @@ func main() {
} }
p1 := &People{Name: "11"} p1 := &People{Name: "11"}
s := structs.New(p1) s := structs.New(p1)
fmt.Println(s.ToMap()) fmt.Println(s.ToMap())
// Output: // Output:
@@ -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,11 +533,11 @@ 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())
// Output: // Output:
// Parent // Parent
} }
``` ```
@@ -610,12 +610,12 @@ func main() {
s := structs.New(p1) s := structs.New(p1)
n, _ := s.Field("Name") n, _ := s.Field("Name")
a, _ := s.Field("arr") a, _ := s.Field("arr")
fmt.Println(n.IsTargetType(reflect.String)) fmt.Println(n.IsTargetType(reflect.String))
fmt.Println(a.IsTargetType(reflect.Slice)) fmt.Println(a.IsTargetType(reflect.Slice))
// Output: // Output:
// true // true
// true // true
} }
``` ```

View File

@@ -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 (
@@ -1647,4 +1647,4 @@ func main() {
// false // false
// false // false
} }
``` ```

View File

@@ -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()

View File

@@ -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"

View File

@@ -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()
} }

View File

@@ -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)
} }