diff --git a/README.md b/README.md index 9699630..a67c772 100644 --- a/README.md +++ b/README.md @@ -214,6 +214,30 @@ import "github.com/duke-git/lancet/v2/concurrency" - **Tee** : split one chanel into two channels, until cancel the context. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#Tee)] [[play](https://go.dev/play/p/3TQPKnCirrP)] +- **NewKeyedLocker** : KeyedLocker is a simple implementation of a keyed locker that allows for non-blocking lock acquisition. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#NewKeyedLocker)] + [[play](https://go.dev/play/p/todo)] +- **Do** :acquires a lock for the specified key and executes the provided function. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#Do)] + [[play](https://go.dev/play/p/todo)] +- **NewRWKeyedLocker** :RRWKeyedLocker is a read-write version of KeyedLocker. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#NewRWKeyedLocker)] + [[play](https://go.dev/play/p/todo)] +- **RLock** : acquires a read lock for the specified key and executes the provided function. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#RLock)] + [[play](https://go.dev/play/p/todo)] +- **Lock** : acquires a write lock for the specified key and executes the provided function. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#Lock)] + [[play](https://go.dev/play/p/todo)] +- **NewTryKeyedLocker** : TryKeyedLocker is a non-blocking version of KeyedLocker. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#NewTryKeyedLocker)] + [[play](https://go.dev/play/p/todo)] +- **TryLock** : TryLock tries to acquire a lock for the specified key. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#TryLock)] + [[play](https://go.dev/play/p/todo)] +- **Unlock** : Unlock releases the lock for the specified key. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/concurrency.md#Unlock)] + [[play](https://go.dev/play/p/todo)]
为指定的键获取锁并执行提供的函数。
@@ -591,6 +590,7 @@ func main() { ```go func NewRWKeyedLocker[K comparable](ttl time.Duration) *RWKeyedLocker[K] ``` + 示例:[运行](https://go.dev/play/p/todo) ```go @@ -630,7 +630,7 @@ func main() { } ``` -### RWKeyedLocker_RLock +### RLockRLock为指定的键获取读锁并执行提供的函数。
@@ -639,6 +639,7 @@ func main() { ```go func (l *RWKeyedLocker[K]) RLock(ctx context.Context, key K, fn func()) error ``` + 示例:[运行](https://go.dev/play/p/todo) ```go @@ -678,15 +679,16 @@ func main() { } ``` -### RWKeyedLocker_Lock +### Lock -RLock为指定的键获取锁并执行提供的函数。
+Lock为指定的键获取锁并执行提供的函数。
函数签名: ```go func (l *RWKeyedLocker[K]) Lock(ctx context.Context, key K, fn func()) error ``` + 示例:[运行](https://go.dev/play/p/todo) ```go @@ -733,8 +735,9 @@ func main() { 函数签名: ```go -func NewTryKeyedLocker[K comparable]() *TryKeyedLocker[K] +func NewTryKeyedLocker[K comparable]() *TryKeyedLocker[K] ``` + 示例:[运行](https://go.dev/play/p/todo) ```go @@ -766,7 +769,7 @@ func main() { } ``` -### TryKeyedLocker_TryLock +### TryLockTryLock尝试获取指定键的锁。如果锁成功获取,则返回true,否则返回false。
@@ -807,7 +810,7 @@ func main() { } ``` -### TryKeyedLocker_Unlock +### Unlock释放指定键的锁。
@@ -846,4 +849,4 @@ func main() { //Lock acquired //Lock released } -``` \ No newline at end of file +``` diff --git a/docs/api/packages/netutil.md b/docs/api/packages/netutil.md index 60d2067..0818d90 100644 --- a/docs/api/packages/netutil.md +++ b/docs/api/packages/netutil.md @@ -48,7 +48,6 @@ import ( - [UploadFile](#UploadFile) - [IsPingConnected](#IsPingConnected) - [IsTelnetConnected](#IsTelnetConnected) -- [IsTelnetConnected](#IsTelnetConnected) - [BuildUrl](#BuildUrl) - [AddQueryParams](#AddQueryParams) @@ -1113,4 +1112,4 @@ func main() { // https://example.com/query?a=foo&a=bar&b=baz //验证字符串是字母或数字。
+ +函数签名: + +```go +func IsAlphaNumeric(s string) bool +``` + +示例:[运行](https://go.dev/play/p/todo) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsAlphaNumeric("ABC") + result2 := validator.IsAlphaNumeric("123") + result3 := validator.IsAlphaNumeric("abc123") + result4 := validator.IsAlphaNumeric("abc123@#$") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // true + // false +} +``` + ### IsJSON验证字符串是否是有效json。
diff --git a/docs/en/api/packages/concurrency.md b/docs/en/api/packages/concurrency.md index 0d2b66c..0ba938a 100644 --- a/docs/en/api/packages/concurrency.md +++ b/docs/en/api/packages/concurrency.md @@ -1,16 +1,18 @@ # Concurrency + Package concurrency contain some functions to support concurrent programming. eg, goroutine, channel. ## Source: -- [https://github.com/duke-git/lancet/blob/main/concurrency/channel.go](https://github.com/duke-git/lancet/blob/main/concurrency/channel.go) +- [https://github.com/duke-git/lancet/blob/main/concurrency/channel.go](https://github.com/duke-git/lancet/blob/main/concurrency/channel.go) - [https://github.com/duke-git/lancet/blob/main/concurrency/keyed_locker.go](https://github.com/duke-git/lancet/blob/main/concurrency/keyed_locker.go) ## Usage: + ```go import ( "github.com/duke-git/lancet/v2/concurrency" @@ -23,34 +25,36 @@ import ( ### Channel -- [NewChannel](#NewChannel) -- [Bridge](#Bridge) -- [FanIn](#FanIn) -- [Generate](#Generate) -- [Or](#Or) -- [OrDone](#OrDone) -- [Repeat](#Repeat) -- [RepeatFn](#RepeatFn) -- [Take](#Take) -- [Tee](#Tee) +- [NewChannel](#NewChannel) +- [Bridge](#Bridge) +- [FanIn](#FanIn) +- [Generate](#Generate) +- [Or](#Or) +- [OrDone](#OrDone) +- [Repeat](#Repeat) +- [RepeatFn](#RepeatFn) +- [Take](#Take) +- [Tee](#Tee) ### KeyedLocker - [NewKeyedLocker](#NewKeyedLocker) -- [KeyedLocker_Do](#Do) +- [Do](#Do) - [NewRWKeyedLocker](#NewRWKeyedLocker) -- [RWKeyedLocker_RLock](#RLock) -- [RWKeyedLocker_Lock](#Lock) +- [RLock](#RLock) +- [Lock](#Lock) - [NewTryKeyedLocker](#NewTryKeyedLocker) -- [TryKeyedLocker_TryLock](#TryLock) -- [TryKeyedLocker_Unlock](#Unlock) +- [TryLock](#TryLock) +- [Unlock](#Unlock) ## Documentation ## Channel + ### NewChannel +Create a Channel pointer instance.
Signature: @@ -59,6 +63,7 @@ import ( type Channel[T any] struct func NewChannel[T any]() *Channel[T] ``` + Example: [Run](https://go.dev/play/p/7aB4KyMMp9A) ```go @@ -83,6 +88,7 @@ func main() { ```go func (c *Channel[T]) Bridge(ctx context.Context, chanStream <-chan <-chan T) <-chan T ``` + Example: [Run](https://go.dev/play/p/qmWSy1NVF-Y) ```go @@ -135,6 +141,7 @@ func main() { ```go func (c *Channel[T]) FanIn(ctx context.Context, channels ...<-chan T) <-chan T ``` + Example: [Run](https://go.dev/play/p/2VYFMexEvTm) ```go @@ -174,6 +181,7 @@ func main() { ```go func (c *Channel[T]) Repeat(ctx context.Context, values ...T) <-chan T ``` + Example: ```go @@ -213,6 +221,7 @@ func main() { ```go func (c *Channel[T]) Generate(ctx context.Context, values ...T) <-chan T ``` + Example: [Run](https://go.dev/play/p/7aB4KyMMp9A) ```go @@ -251,6 +260,7 @@ func main() { ```go func (c *Channel[T]) RepeatFn(ctx context.Context, fn func() T) <-chan T ``` + Example: ```go @@ -293,6 +303,7 @@ func main() { ```go func (c *Channel[T]) Or(channels ...<-chan T) <-chan T ``` + Example: ```go @@ -336,6 +347,7 @@ func main() { ```go func (c *Channel[T]) OrDone(ctx context.Context, channel <-chan T) <-chan T ``` + Example: ```go @@ -374,6 +386,7 @@ func main() { ```go func (c *Channel[T]) Take(ctx context.Context, valueStream <-chan T, number int) <-chan T ``` + Example: ```go @@ -420,6 +433,7 @@ func main() { ```go func (c *Channel[T]) Tee(ctx context.Context, in <-chan T) (<-chan T, <-chan T) ``` + Example: ```go @@ -444,7 +458,7 @@ func main() { fmt.Println(v) fmt.Println(<-ch2) } - + // Output: // 1 // 1 @@ -512,7 +526,7 @@ func main() { } ``` -### KeyedLocker_Do +### DoAcquires a lock for the specified key and executes the provided function.
@@ -578,6 +592,7 @@ func main() { ```go func NewRWKeyedLocker[K comparable](ttl time.Duration) *RWKeyedLocker[K] ``` + Example:[Run](https://go.dev/play/p/todo) ```go @@ -617,7 +632,7 @@ func main() { } ``` -### RWKeyedLocker_RLock +### RLockAcquires a read lock for the specified key and executes the provided function.
@@ -626,6 +641,7 @@ func main() { ```go func (l *RWKeyedLocker[K]) RLock(ctx context.Context, key K, fn func()) error ``` + Example:[Run](https://go.dev/play/p/todo) ```go @@ -665,7 +681,7 @@ func main() { } ``` -### RWKeyedLocker_Lock +### LockAcquires a write lock for the specified key and executes the provided function.
@@ -674,6 +690,7 @@ func main() { ```go func (l *RWKeyedLocker[K]) Lock(ctx context.Context, key K, fn func()) error ``` + Example:[Run](https://go.dev/play/p/todo) ```go @@ -720,8 +737,9 @@ func main() { Signature: ```go -func NewTryKeyedLocker[K comparable]() *TryKeyedLocker[K] +func NewTryKeyedLocker[K comparable]() *TryKeyedLocker[K] ``` + Example:[Run](https://go.dev/play/p/todo) ```go @@ -753,7 +771,7 @@ func main() { } ``` -### TryKeyedLocker_TryLock +### TryLockTryLock tries to acquire a lock for the specified key.
@@ -794,7 +812,7 @@ func main() { } ``` -### TryKeyedLocker_Unlock +### UnlockUnlock releases the lock for the specified key.
@@ -833,4 +851,4 @@ func main() { //Lock acquired //Lock released } -``` \ No newline at end of file +``` diff --git a/docs/en/api/packages/validator.md b/docs/en/api/packages/validator.md index a35b482..093893b 100644 --- a/docs/en/api/packages/validator.md +++ b/docs/en/api/packages/validator.md @@ -826,6 +826,43 @@ func main() { } ``` +### IsAlphaNumeric + +Check if the string is alphanumeric.
+ +Signature: + +```go +func IsAlphaNumeric(s string) bool +``` + +Example:[Run](https://go.dev/play/p/todo) + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/validator" +) + +func main() { + result1 := validator.IsAlphaNumeric("ABC") + result2 := validator.IsAlphaNumeric("123") + result3 := validator.IsAlphaNumeric("abc123") + result4 := validator.IsAlphaNumeric("abc123@#$") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // true + // false +} +``` + ### IsJSONCheck if the string is valid JSON.
diff --git a/validator/validator.go b/validator/validator.go index dd7b68e..bbc2566 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -182,7 +182,7 @@ func IsJSON(str string) bool { return json.Unmarshal([]byte(str), &js) == nil } -// IsAlphaNumericStr check if the string is alphanumeric. +// IsAlphaNumeric check if the string is alphanumeric. // Play: todo func IsAlphaNumeric(s string) bool { return alphaNumericMatcher.MatchString(s) diff --git a/validator/validator_example_test.go b/validator/validator_example_test.go index c1e736a..088cb86 100644 --- a/validator/validator_example_test.go +++ b/validator/validator_example_test.go @@ -665,3 +665,21 @@ func ExampleIsChinaUnionPay() { // true // false } + +func ExampleIsAlphaNumeric() { + result1 := IsAlphaNumeric("ABC") + result2 := IsAlphaNumeric("123") + result3 := IsAlphaNumeric("abc123") + result4 := IsAlphaNumeric("abc123@#$") + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + fmt.Println(result4) + + // Output: + // true + // true + // true + // false +}