diff --git a/README.md b/README.md index 1092009..fc190ba 100644 --- a/README.md +++ b/README.md @@ -508,22 +508,22 @@ import "github.com/duke-git/lancet/v2/datetime" [[play](https://go.dev/play/p/nT1heB1KUUK)] - **AddWeek** : add or sub week to time. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/datetime.md#AddWeek)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/M9TqdMiaA2p)] - **AddMonth** : add or sub months to time. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/datetime.md#AddMonth)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/DLoiOnpLvsN)] - **AddYear** : add or sub year to the time. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/datetime.md#AddYear)] [[play](https://go.dev/play/p/MqW2ujnBx10)] - **AddDaySafe** : add or sub days to the time and ensure that the returned date does not exceed the valid date of the target year and month. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/datetime.md#AddDaySafe)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/JTohZFpoDJ3)] - **AddMonthSafe** : add or sub months to the time and ensure that the returned date does not exceed the valid date of the target year and month. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/datetime.md#AddMonthSafe)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/KLw0lo6mbVW)] - **AddYearSafe** : Add or sub years to the time and ensure that the returned date does not exceed the valid date of the target year and month. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/datetime.md#AddYearSafe)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/KVGXWZZ54ZH)] - **BeginOfMinute** : return the date time at the begin of minute of specific date. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/datetime.md#BeginOfMinute)] [[play](https://go.dev/play/p/ieOLVJ9CiFT)] @@ -705,34 +705,34 @@ import "github.com/duke-git/lancet/v2/eventbus" - **NewEventBus** : Create an EventBus instance. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/eventbus.md#NewEventBus)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/gHbOPV_NUOJ)] - **Subscribe** : subscribes to an event with a specific event topic and listener function. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/eventbus.md#Subscribe)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/EYGf_8cHei-)] - **Unsubscribe** : unsubscribes from an event with a specific event topic and listener function. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/eventbus.md#Unsubscribe)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/Tmh7Ttfvprf)] - **Publish** : publishes an event with a specific event topic and data payload. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/eventbus.md#Publish)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/gHTtVexFSH9)] - **ClearListeners** : clears all the listeners. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/eventbus.md#ClearListeners)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/KBfBYlKPgqD)] - **ClearListenersByTopic** : clears all the listeners by topic. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/eventbus.md#ClearListenersByTopic)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/gvMljmJOZmU)] - **GetListenersCount** : returns the number of listeners for a specific event topic. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/eventbus.md#GetListenersCount)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/8VPJsMQgStM)] - **GetAllListenersCount** : returns the total number of all listeners. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/eventbus.md#GetAllListenersCount)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/PUlr0xcpEOz)] - **GetEvents** : returns all the events topics. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/eventbus.md#GetEvents)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/etgjjcOtAjX)] - **SetErrorHandler** : sets the error handler function. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/eventbus.md#SetErrorHandler)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/gmB0gnFe5mc)]

9. Fileutil package implements some basic functions for file operations.        index

@@ -1479,7 +1479,7 @@ import "github.com/duke-git/lancet/v2/slice" [[play](https://go.dev/play/p/b9iygtgsHI1)] - **EqualUnordered** : Checks if two slices are equal: the same length and all elements value are equal (unordered). [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#EqualUnordered)] - [[play](todo)] + [[play](https://go.dev/play/p/n8fSc2w8ZgX)] - **Every** : return true if all of the values in the slice pass the predicate function. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#Every)] [[play](https://go.dev/play/p/R8U6Sl-j8cD)] @@ -1581,7 +1581,7 @@ import "github.com/duke-git/lancet/v2/slice" [[play](https://go.dev/play/p/YHvhnWGU3Ge)] - **ShuffleCopy** : return a new slice with elements shuffled. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#ShuffleCopy)] - [[play](todo)] + [[play](https://go.dev/play/p/vqDa-Gs1vT0)] - **IsAscending** : Checks if a slice is ascending order. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#IsAscending)] [[play](https://go.dev/play/p/9CtsFjet4SH)] @@ -1730,7 +1730,7 @@ import "github.com/duke-git/lancet/v2/stream" [[play](https://go.dev/play/p/A8_zkJnLHm4)] - **ReverseCopy** : returns a new slice of element order is reversed to the given slice. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/stream.md#ReverseCopy)] - [[play](todo)] + [[play](https://go.dev/play/p/c9arEaP7Cg-)] - **Range** : returns a stream whose elements are in the range from start(included) to end(excluded) original stream. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/stream.md#Range)] [[play](https://go.dev/play/p/indZY5V2f4j)] @@ -1962,7 +1962,7 @@ import "github.com/duke-git/lancet/v2/strutil" [[play](https://go.dev/play/p/Ay9UIk7Rum9)] - **FindAllOccurrences** : Returns the positions of all occurrences of a substring in a string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/strutil.md#FindAllOccurrences)] - [[play](todo)] + [[play](https://go.dev/play/p/uvyA6azGLB1)]

22. System package contain some functions about os, runtime, shell command.        index

@@ -2218,7 +2218,7 @@ import "github.com/duke-git/lancet/v2/validator" [[play](https://go.dev/play/p/AHA0r0AzIdC)] - **IsIpPort** : check if the string is ip:port. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsIpPort)] - [[play](todo)] + [[play](https://go.dev/play/p/xUmls_b9L29)] - **IsStrongPassword** : check if the string is strong password. [[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/validator.md#IsStrongPassword)] [[play](https://go.dev/play/p/QHdVcSQ3uDg)] diff --git a/README_zh-CN.md b/README_zh-CN.md index b865b9c..162cc4f 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -509,22 +509,22 @@ import "github.com/duke-git/lancet/v2/datetime" [[play](https://go.dev/play/p/nT1heB1KUUK)] - **AddWeek** : 将日期加/减星期数. [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/datetime.md#AddWeek)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/M9TqdMiaA2p)] - **AddMonth** : 将日期加/减月数. [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/datetime.md#AddMonth)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/DLoiOnpLvsN)] - **AddYear** : 将日期加/减分年数。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/datetime.md#AddYear)] [[play](https://go.dev/play/p/MqW2ujnBx10)] - **AddDaySafe** : 增加/减少指定的天数,并确保日期是有效日期。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/datetime.md#AddDaySafe)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/JTohZFpoDJ3)] - **AddMonthSafe** : 增加/减少指定的月份,并确保日期是有效日期。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/datetime.md#AddMonthSafe)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/KLw0lo6mbVW)] - **AddYearSafe** : 增加/减少指定的年份,并确保日期是有效日期。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/datetime.md#AddYearSafe)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/KVGXWZZ54ZH)] - **BeginOfMinute** : 返回指定时间的分钟开始时间。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/datetime.md#BeginOfMinute)] [[play](https://go.dev/play/p/ieOLVJ9CiFT)] @@ -704,34 +704,34 @@ import "github.com/duke-git/lancet/v2/eventbus" - **NewEventBus** : 创建EventBus实例。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/eventbus.md#NewEventBus)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/gHbOPV_NUOJ)] - **Subscribe** : 订阅具有特定事件主题和监听函数的事件。支持异步,事件优先级,事件过滤器。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/eventbus.md#Subscribe)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/EYGf_8cHei-)] - **Unsubscribe** : 取消订阅具有特定事件主题的事件。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/eventbus.md#Unsubscribe)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/Tmh7Ttfvprf)] - **Publish** : 发布一个带有特定事件主题和数据负载的事件。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/eventbus.md#Publish)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/gHTtVexFSH9)] - **ClearListeners** : 清空所有事件监听器。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/eventbus.md#ClearListeners)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/KBfBYlKPgqD)] - **ClearListenersByTopic** : 清空特定事件监听器。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/eventbus.md#ClearListenersByTopic)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/gvMljmJOZmU)] - **GetListenersCount** : 获取特定事件的监听器数量。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/eventbus.md#GetListenersCount)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/8VPJsMQgStM)] - **GetAllListenersCount** : 获取所有事件的监听器数量。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/eventbus.md#GetAllListenersCount)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/PUlr0xcpEOz)] - **GetEvents** : 获取所有事件的topic。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/eventbus.md#GetEvents)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/etgjjcOtAjX)] - **SetErrorHandler** : 设置事件的错误处理函数。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/eventbus.md#SetErrorHandler)] - [[play](https://go.dev/play/p/todo)] + [[play](https://go.dev/play/p/gmB0gnFe5mc)]

10. fileutil 包含文件基本操作。       回到目录

@@ -1477,7 +1477,7 @@ import "github.com/duke-git/lancet/v2/slice" [[play](https://go.dev/play/p/b9iygtgsHI1)] - **EqualUnordered** : 检查两个切片是否相等,元素数量相同,值相等,不考虑元素顺序。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#EqualUnordered)] - [[play](todo)] + [[play](https://go.dev/play/p/n8fSc2w8ZgX)] - **Every** : 如果切片中的所有值都通过谓词函数,则返回 true。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#Every)] [[play](https://go.dev/play/p/R8U6Sl-j8cD)] @@ -1555,7 +1555,7 @@ import "github.com/duke-git/lancet/v2/slice" [[play](https://go.dev/play/p/8uI8f1lwNrQ)] - **ReverseCopy** : 反转切片中的元素顺序, 不改变原slice。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/stream.md#ReverseCopy)] - [[play](todo)] + [[play](https://go.dev/play/p/c9arEaP7Cg-)] - **Reducedeprecated** : 将切片中的元素依次运行 iteratee 函数,返回运行结果。(废弃:建议使用 ReduceBy) [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#Reduce)] [[play](https://go.dev/play/p/_RfXJJWIsIm)] @@ -1582,7 +1582,7 @@ import "github.com/duke-git/lancet/v2/slice" [[play](https://go.dev/play/p/YHvhnWGU3Ge)] - **ShuffleCopy** : 随机打乱切片中的元素顺序, 不改变原切片。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#ShuffleCopy)] - [[play](todo)] + [[play](https://go.dev/play/p/vqDa-Gs1vT0)] - **IsAscending** : 检查切片元素是否按升序排列。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#IsAscending)] [[play](https://go.dev/play/p/9CtsFjet4SH)] @@ -1962,7 +1962,7 @@ import "github.com/duke-git/lancet/v2/strutil" [[play](https://go.dev/play/p/Ay9UIk7Rum9)] - **FindAllOccurrences** : 返回子字符串在字符串中所有出现的位置。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#FindAllOccurrences)] - [[play](todo)] + [[play](https://go.dev/play/p/uvyA6azGLB1)]

23. system 包含 os, runtime, shell command 的相关函数。       回到目录

@@ -2220,7 +2220,7 @@ import "github.com/duke-git/lancet/v2/validator" [[play](https://go.dev/play/p/AHA0r0AzIdC)] - **IsIpPort** : 检查字符串是否是ip:port格式。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsIpPort)] - [[play](todo)] + [[play](https://go.dev/play/p/xUmls_b9L29)] - **IsStrongPassword** : 验证字符串是否是强密码:(字母+数字+特殊字符)。 [[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsStrongPassword)] [[play](https://go.dev/play/p/QHdVcSQ3uDg)] diff --git a/datetime/datetime.go b/datetime/datetime.go index 090caf1..3ea3a0c 100644 --- a/datetime/datetime.go +++ b/datetime/datetime.go @@ -83,13 +83,13 @@ func AddDay(t time.Time, days int64) time.Time { } // AddWeek add or sub weeks to the time. -// play: todo +// play: https://go.dev/play/p/M9TqdMiaA2p func AddWeek(t time.Time, weeks int64) time.Time { return t.Add(7 * 24 * time.Hour * time.Duration(weeks)) } // AddMonth add or sub months to the time. -// Play: todo +// Play: https://go.dev/play/p/DLoiOnpLvsN func AddMonth(t time.Time, months int64) time.Time { return t.AddDate(0, int(months), 0) } @@ -101,7 +101,7 @@ func AddYear(t time.Time, year int64) time.Time { } // AddDaySafe add or sub days to the time and ensure that the returned date does not exceed the valid date of the target year and month. -// Play: todo +// Play: https://go.dev/play/p/JTohZFpoDJ3 func AddDaySafe(t time.Time, days int) time.Time { t = t.AddDate(0, 0, days) year, month, day := t.Date() @@ -116,7 +116,7 @@ func AddDaySafe(t time.Time, days int) time.Time { } // AddMonthSafe add or sub months to the time and ensure that the returned date does not exceed the valid date of the target year and month. -// Play: todo +// Play: https://go.dev/play/p/KLw0lo6mbVW func AddMonthSafe(t time.Time, months int) time.Time { year := t.Year() month := int(t.Month()) + months @@ -141,7 +141,7 @@ func AddMonthSafe(t time.Time, months int) time.Time { } // AddYearSafe add or sub years to the time and ensure that the returned date does not exceed the valid date of the target year and month. -// Play: todo +// Play: https://go.dev/play/p/KVGXWZZ54ZH func AddYearSafe(t time.Time, years int) time.Time { year, month, day := t.Date() year += years diff --git a/docs/api/packages/datetime.md b/docs/api/packages/datetime.md index 43b9e29..cce2a43 100644 --- a/docs/api/packages/datetime.md +++ b/docs/api/packages/datetime.md @@ -153,7 +153,7 @@ func main() { func AddWeek(t time.Time, weeks int64) time.Time ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/M9TqdMiaA2p) ```go package main @@ -189,7 +189,7 @@ func main() { func AddMonth(t time.Time, months int64) time.Time ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/DLoiOnpLvsN) ```go package main @@ -333,7 +333,7 @@ func main() { func AddDaySafe(t time.Time, days int) time.Time ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/JTohZFpoDJ3) ```go package main @@ -380,7 +380,7 @@ func main() { func AddMonthSafe(t time.Time, months int) time.Time ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/KLw0lo6mbVW) ```go package main @@ -417,7 +417,7 @@ func main() { func AddYearSafe(t time.Time, years int) time.Time ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/KVGXWZZ54ZH) ```go package main diff --git a/docs/api/packages/eventbus.md b/docs/api/packages/eventbus.md index 7e0a293..9d26304 100644 --- a/docs/api/packages/eventbus.md +++ b/docs/api/packages/eventbus.md @@ -48,7 +48,7 @@ import ( func NewEventBus[T any]() *EventBus[T] ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/gHbOPV_NUOJ) ```go import ( @@ -65,7 +65,7 @@ func main() { } eb.Subscribe("event1", listener, false, 0, nil) - eb.Publish(Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(eventbus.Event[int]{Topic: "event1", Payload: 1}) fmt.Println(receivedData) @@ -84,7 +84,7 @@ func main() { func (eb *EventBus[T]) Subscribe(topic string, listener func(eventData T), async bool, priority int, filter func(eventData T) bool) ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/EYGf_8cHei-) ```go import ( @@ -106,8 +106,8 @@ func main() { eb.Subscribe("event1", listener, false, 0, filter) - eb.Publish(Event[int]{Topic: "event1", Payload: 1}) - eb.Publish(Event[int]{Topic: "event1", Payload: 2}) + eb.Publish(eventbus.Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(eventbus.Event[int]{Topic: "event1", Payload: 2}) fmt.Println(receivedData) @@ -126,7 +126,7 @@ func main() { func (eb *EventBus[T]) Unsubscribe(topic string, listener func(eventData T)) ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/Tmh7Ttfvprf) ```go import ( @@ -145,7 +145,7 @@ func main() { eb.Subscribe("event1", listener, false, 0, nil) eb.Unsubscribe("event1", listener) - eb.Publish(Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(eventbus.Event[int]{Topic: "event1", Payload: 1}) fmt.Println(receivedData) @@ -161,10 +161,10 @@ func main() { 函数签名: ```go -func (eb *EventBus[T]) Publish(event Event[T]) +func (eb *EventBus[T]) Publish(event eventbus.Event[T]) ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/gHTtVexFSH9) ```go import ( @@ -179,7 +179,7 @@ func main() { fmt.Println(eventData) }, false, 0, nil) - eb.Publish(Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(eventbus.Event[int]{Topic: "event1", Payload: 1}) // Output: // 1 @@ -196,7 +196,7 @@ func main() { func (eb *EventBus[T]) ClearListeners() ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/KBfBYlKPgqD) ```go import ( @@ -213,9 +213,12 @@ func main() { } eb.Subscribe("event1", listener, false, 0, nil) + eb.Subscribe("event2", listener, false, 0, nil) + eb.ClearListeners() - eb.Publish(Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(eventbus.Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(eventbus.Event[int]{Topic: "event2", Payload: 2}) fmt.Println(receivedData) @@ -234,7 +237,7 @@ func main() { func (eb *EventBus[T]) ClearListenersByTopic(topic string) ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/gvMljmJOZmU) ```go import ( @@ -251,14 +254,17 @@ func main() { } eb.Subscribe("event1", listener, false, 0, nil) + eb.Subscribe("event2", listener, false, 0, nil) + eb.ClearListenersByTopic("event1") - eb.Publish(Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(eventbus.Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(eventbus.Event[int]{Topic: "event2", Payload: 2}) fmt.Println(receivedData) // Output: - // 0 + // 2 } ``` @@ -272,7 +278,7 @@ func main() { func (eb *EventBus[T]) GetListenersCount(topic string) int ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/j6yaJ2xAmFz) ```go import ( @@ -284,14 +290,14 @@ func main() { eb := eventbus.NewEventBus[int]() eb.Subscribe("event1", func(eventData int) {}, false, 0, nil) - eb.Subscribe("event1", func(eventData int) {}, false, 0, nil) + eb.Subscribe("event2", func(eventData int) {}, false, 0, nil) count := eb.GetListenersCount("event1") fmt.Println(count) // Output: - // 2 + // 1 } ``` @@ -305,7 +311,7 @@ func main() { func (eb *EventBus[T]) GetAllListenersCount() int ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/PUlr0xcpEOz) ```go import ( @@ -338,7 +344,7 @@ func main() { func (eb *EventBus[T]) GetEvents() []string ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/etgjjcOtAjX) ```go import ( @@ -359,8 +365,8 @@ func main() { } // Output: - // event1 // event2 + // event1 } ``` @@ -374,7 +380,7 @@ func main() { func (eb *EventBus[T]) SetErrorHandler(handler func(err error)) ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/gmB0gnFe5mc) ```go import ( @@ -393,7 +399,7 @@ func main() { panic("error") }, false, 0, nil) - eb.Publish(Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(eventbus.Event[int]{Topic: "event1", Payload: 1}) // Output: // error diff --git a/docs/api/packages/slice.md b/docs/api/packages/slice.md index b09ab17..7787c3d 100644 --- a/docs/api/packages/slice.md +++ b/docs/api/packages/slice.md @@ -887,7 +887,7 @@ func main() { func EqualUnordered[T comparable](slice1, slice2 []T) bool ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/n8fSc2w8ZgX) ```go import ( @@ -1772,7 +1772,7 @@ func main() { func ReverseCopy[T any](slice []T) []T ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/c9arEaP7Cg-) ```go import ( @@ -2065,7 +2065,7 @@ func main() { func ShuffleCopy[T any](slice []T) []T ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/vqDa-Gs1vT0) ```go import ( diff --git a/docs/api/packages/strutil.md b/docs/api/packages/strutil.md index 5dc5058..b5d370b 100644 --- a/docs/api/packages/strutil.md +++ b/docs/api/packages/strutil.md @@ -1772,7 +1772,7 @@ func main() { func FindAllOccurrences(str, substr string) []int ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/uvyA6azGLB1) ```go import ( diff --git a/docs/api/packages/validator.md b/docs/api/packages/validator.md index d15fb73..5384829 100644 --- a/docs/api/packages/validator.md +++ b/docs/api/packages/validator.md @@ -1001,7 +1001,7 @@ func main() { func IsIpPort(str string) bool ``` -示例:[运行](todo) +示例:[运行](https://go.dev/play/p/xUmls_b9L29) ```go import ( diff --git a/docs/en/api/packages/datetime.md b/docs/en/api/packages/datetime.md index 0fc934d..db8b0f8 100644 --- a/docs/en/api/packages/datetime.md +++ b/docs/en/api/packages/datetime.md @@ -155,7 +155,7 @@ func main() { func AddWeek(t time.Time, weeks int64) time.Time ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/M9TqdMiaA2p) ```go package main @@ -191,7 +191,7 @@ func main() { func AddMonth(t time.Time, months int64) time.Time ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/DLoiOnpLvsN) ```go package main @@ -335,7 +335,7 @@ func main() { func AddDaySafe(t time.Time, days int) time.Time ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/JTohZFpoDJ3) ```go package main @@ -382,7 +382,7 @@ func main() { func AddMonthSafe(t time.Time, months int) time.Time ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/KLw0lo6mbVW) ```go package main @@ -419,7 +419,7 @@ func main() { func AddYearSafe(t time.Time, years int) time.Time ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/KVGXWZZ54ZH) ```go package main diff --git a/docs/en/api/packages/eventbus.md b/docs/en/api/packages/eventbus.md index 08a27ff..1742766 100644 --- a/docs/en/api/packages/eventbus.md +++ b/docs/en/api/packages/eventbus.md @@ -48,7 +48,7 @@ import ( func NewEventBus[T any]() *EventBus[T] ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/gHbOPV_NUOJ) ```go import ( @@ -65,7 +65,7 @@ func main() { } eb.Subscribe("event1", listener, false, 0, nil) - eb.Publish(Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(eventbus.Event[int]{Topic: "event1", Payload: 1}) fmt.Println(receivedData) @@ -84,7 +84,7 @@ func main() { func (eb *EventBus[T]) Subscribe(topic string, listener func(eventData T), async bool, priority int, filter func(eventData T) bool) ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/EYGf_8cHei-) ```go import ( @@ -106,8 +106,8 @@ func main() { eb.Subscribe("event1", listener, false, 0, filter) - eb.Publish(Event[int]{Topic: "event1", Payload: 1}) - eb.Publish(Event[int]{Topic: "event1", Payload: 2}) + eb.Publish(eventbus.Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(eventbus.Event[int]{Topic: "event1", Payload: 2}) fmt.Println(receivedData) @@ -126,7 +126,7 @@ func main() { func (eb *EventBus[T]) Unsubscribe(topic string, listener func(eventData T)) ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/Tmh7Ttfvprf) ```go import ( @@ -145,7 +145,7 @@ func main() { eb.Subscribe("event1", listener, false, 0, nil) eb.Unsubscribe("event1", listener) - eb.Publish(Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(eventbus.Event[int]{Topic: "event1", Payload: 1}) fmt.Println(receivedData) @@ -161,10 +161,10 @@ func main() { Signature: ```go -func (eb *EventBus[T]) Publish(event Event[T]) +func (eb *EventBus[T]) Publish(event eventbus.Event[T]) ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/gHTtVexFSH9) ```go import ( @@ -179,7 +179,7 @@ func main() { fmt.Println(eventData) }, false, 0, nil) - eb.Publish(Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(eventbus.Event[int]{Topic: "event1", Payload: 1}) // Output: // 1 @@ -196,7 +196,7 @@ func main() { func (eb *EventBus[T]) ClearListeners() ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/KBfBYlKPgqD) ```go import ( @@ -213,9 +213,12 @@ func main() { } eb.Subscribe("event1", listener, false, 0, nil) + eb.Subscribe("event2", listener, false, 0, nil) + eb.ClearListeners() - eb.Publish(Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(eventbus.Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(eventbus.Event[int]{Topic: "event2", Payload: 2}) fmt.Println(receivedData) @@ -234,7 +237,7 @@ func main() { func (eb *EventBus[T]) ClearListenersByTopic(topic string) ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/gvMljmJOZmU) ```go import ( @@ -251,14 +254,17 @@ func main() { } eb.Subscribe("event1", listener, false, 0, nil) + eb.Subscribe("event2", listener, false, 0, nil) + eb.ClearListenersByTopic("event1") - eb.Publish(Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(eventbus.Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(eventbus.Event[int]{Topic: "event2", Payload: 2}) fmt.Println(receivedData) // Output: - // 0 + // 2 } ``` @@ -272,7 +278,7 @@ func main() { func (eb *EventBus[T]) GetListenersCount(topic string) int ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/j6yaJ2xAmFz) ```go import ( @@ -284,14 +290,14 @@ func main() { eb := eventbus.NewEventBus[int]() eb.Subscribe("event1", func(eventData int) {}, false, 0, nil) - eb.Subscribe("event1", func(eventData int) {}, false, 0, nil) + eb.Subscribe("event2", func(eventData int) {}, false, 0, nil) count := eb.GetListenersCount("event1") fmt.Println(count) // Output: - // 2 + // 1 } ``` @@ -305,7 +311,7 @@ func main() { func (eb *EventBus[T]) GetAllListenersCount() int ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/PUlr0xcpEOz) ```go import ( @@ -338,7 +344,7 @@ func main() { func (eb *EventBus[T]) GetEvents() []string ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/etgjjcOtAjX) ```go import ( @@ -359,8 +365,8 @@ func main() { } // Output: - // event1 // event2 + // event1 } ``` @@ -374,7 +380,7 @@ func main() { func (eb *EventBus[T]) SetErrorHandler(handler func(err error)) ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/gmB0gnFe5mc) ```go import ( @@ -393,7 +399,7 @@ func main() { panic("error") }, false, 0, nil) - eb.Publish(Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(eventbus.Event[int]{Topic: "event1", Payload: 1}) // Output: // error diff --git a/docs/en/api/packages/slice.md b/docs/en/api/packages/slice.md index c842b78..243981c 100644 --- a/docs/en/api/packages/slice.md +++ b/docs/en/api/packages/slice.md @@ -852,7 +852,7 @@ func main() { func EqualUnordered[T comparable](slice1, slice2 []T) bool ``` -Example:[运行](todo) +Example:[运行](https://go.dev/play/p/n8fSc2w8ZgX) ```go import ( @@ -1768,7 +1768,7 @@ func main() { func ReverseCopy[T any](slice []T) []T ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/c9arEaP7Cg-) ```go import ( @@ -2062,7 +2062,7 @@ func main() { func ShuffleCopy[T any](slice []T) []T ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/vqDa-Gs1vT0) ```go import ( diff --git a/docs/en/api/packages/strutil.md b/docs/en/api/packages/strutil.md index 28c7ef8..90e2d99 100644 --- a/docs/en/api/packages/strutil.md +++ b/docs/en/api/packages/strutil.md @@ -1774,7 +1774,7 @@ func main() { func FindAllOccurrences(str, substr string) []int ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/uvyA6azGLB1) ```go import ( diff --git a/docs/en/api/packages/validator.md b/docs/en/api/packages/validator.md index 8abc25b..a35b482 100644 --- a/docs/en/api/packages/validator.md +++ b/docs/en/api/packages/validator.md @@ -1003,7 +1003,7 @@ func main() { func IsIpPort(str string) bool ``` -Example:[Run](todo) +Example:[Run](https://go.dev/play/p/xUmls_b9L29) ```go import ( diff --git a/eventbus/eventbus.go b/eventbus/eventbus.go index 93b55fc..8fe9b25 100644 --- a/eventbus/eventbus.go +++ b/eventbus/eventbus.go @@ -33,7 +33,7 @@ type EventListener[T any] struct { } // NewEventBus creates a new EventBus. -// Play: todo +// Play: https://go.dev/play/p/gHbOPV_NUOJ func NewEventBus[T any]() *EventBus[T] { return &EventBus[T]{ listeners: sync.Map{}, @@ -41,7 +41,7 @@ func NewEventBus[T any]() *EventBus[T] { } // Subscribe subscribes to an event with a specific event topic and listener function. -// Play: todo +// Play: https://go.dev/play/p/EYGf_8cHei- func (eb *EventBus[T]) Subscribe(topic string, listener func(eventData T), async bool, priority int, filter func(eventData T) bool) { eb.mu.Lock() defer eb.mu.Unlock() @@ -65,7 +65,7 @@ func (eb *EventBus[T]) Subscribe(topic string, listener func(eventData T), async } // Unsubscribe unsubscribes from an event with a specific event topic and listener function. -// Play: todo +// Play: https://go.dev/play/p/Tmh7Ttfvprf func (eb *EventBus[T]) Unsubscribe(topic string, listener func(eventData T)) { eb.mu.Lock() defer eb.mu.Unlock() @@ -89,7 +89,7 @@ func (eb *EventBus[T]) Unsubscribe(topic string, listener func(eventData T)) { } // Publish publishes an event with a specific event topic and data payload. -// Play: todo +// Play: https://go.dev/play/p/gHTtVexFSH9 func (eb *EventBus[T]) Publish(event Event[T]) { eb.mu.RLock() defer eb.mu.RUnlock() @@ -125,12 +125,13 @@ func (eb *EventBus[T]) publishToListener(listener *EventListener[T], event Event } // SetErrorHandler sets the error handler function. +// Play: https://go.dev/play/p/gmB0gnFe5mc func (eb *EventBus[T]) SetErrorHandler(handler func(err error)) { eb.errorHandler = handler } // ClearListeners clears all the listeners. -// Play: todo +// Play: https://go.dev/play/p/KBfBYlKPgqD func (eb *EventBus[T]) ClearListeners() { eb.mu.Lock() defer eb.mu.Unlock() @@ -139,7 +140,7 @@ func (eb *EventBus[T]) ClearListeners() { } // ClearListenersByTopic clears all the listeners by topic. -// Play: todo +// Play: https://go.dev/play/p/gvMljmJOZmU func (eb *EventBus[T]) ClearListenersByTopic(topic string) { eb.mu.Lock() defer eb.mu.Unlock() @@ -148,7 +149,7 @@ func (eb *EventBus[T]) ClearListenersByTopic(topic string) { } // GetListenersCount returns the number of listeners for a specific event topic. -// Play: todo +// Play: https://go.dev/play/p/8VPJsMQgStM func (eb *EventBus[T]) GetListenersCount(topic string) int { eb.mu.RLock() defer eb.mu.RUnlock() @@ -163,7 +164,7 @@ func (eb *EventBus[T]) GetListenersCount(topic string) int { } // GetAllListenersCount returns the total number of listeners. -// Play: todo +// Play: https://go.dev/play/p/PUlr0xcpEOz func (eb *EventBus[T]) GetAllListenersCount() int { eb.mu.RLock() defer eb.mu.RUnlock() @@ -178,7 +179,7 @@ func (eb *EventBus[T]) GetAllListenersCount() int { } // GetEvents returns all the events topics. -// Play: todo +// Play: https://go.dev/play/p/etgjjcOtAjX func (eb *EventBus[T]) GetEvents() []string { eb.mu.RLock() defer eb.mu.RUnlock() diff --git a/eventbus/eventbus_example_test.go b/eventbus/eventbus_example_test.go index 9975bbc..a9b124b 100644 --- a/eventbus/eventbus_example_test.go +++ b/eventbus/eventbus_example_test.go @@ -136,9 +136,12 @@ func ExampleEventBus_ClearListeners() { } eb.Subscribe("event1", listener, false, 0, nil) + eb.Subscribe("event2", listener, false, 0, nil) + eb.ClearListeners() eb.Publish(Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(Event[int]{Topic: "event2", Payload: 2}) fmt.Println(receivedData) @@ -155,28 +158,31 @@ func ExampleEventBus_ClearListenersByTopic() { } eb.Subscribe("event1", listener, false, 0, nil) + eb.Subscribe("event2", listener, false, 0, nil) + eb.ClearListenersByTopic("event1") eb.Publish(Event[int]{Topic: "event1", Payload: 1}) + eb.Publish(Event[int]{Topic: "event2", Payload: 2}) fmt.Println(receivedData) // Output: - // 0 + // 2 } func ExampleEventBus_GetListenersCount() { eb := NewEventBus[int]() eb.Subscribe("event1", func(eventData int) {}, false, 0, nil) - eb.Subscribe("event1", func(eventData int) {}, false, 0, nil) + eb.Subscribe("event2", func(eventData int) {}, false, 0, nil) count := eb.GetListenersCount("event1") fmt.Println(count) // Output: - // 2 + // 1 } func ExampleEventBus_SetErrorHandler() { diff --git a/slice/slice.go b/slice/slice.go index 6a27b51..ae7681e 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -221,7 +221,7 @@ func EqualWith[T, U any](slice1 []T, slice2 []U, comparator func(T, U) bool) boo } // EqualUnordered checks if two slices are equal: the same length and all elements' value are equal (unordered). -// Play: todo +// Play: https://go.dev/play/p/n8fSc2w8ZgX func EqualUnordered[T comparable](slice1, slice2 []T) bool { if len(slice1) != len(slice2) { return false @@ -1003,7 +1003,7 @@ func Reverse[T any](slice []T) { } // ReverseCopy return a new slice of element order is reversed to the given slice. -// Play: todo +// Play: https://go.dev/play/p/c9arEaP7Cg- func ReverseCopy[T any](slice []T) []T { result := make([]T, len(slice)) @@ -1027,7 +1027,7 @@ func Shuffle[T any](slice []T) []T { } // ShuffleCopy return a new slice with elements shuffled. -// Play: todo +// Play: https://go.dev/play/p/vqDa-Gs1vT0 func ShuffleCopy[T any](slice []T) []T { result := make([]T, len(slice)) copy(result, slice) diff --git a/strutil/string.go b/strutil/string.go index d33522d..a716cb4 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -758,7 +758,7 @@ func ExtractContent(str, start, end string) []string { } // FindAllOccurrences returns the positions of all occurrences of a substring in a string. -// Play: todo +// Play: https://go.dev/play/p/uvyA6azGLB1 func FindAllOccurrences(str, substr string) []int { var positions []int diff --git a/validator/validator.go b/validator/validator.go index 90f2d9f..02a3e4e 100644 --- a/validator/validator.go +++ b/validator/validator.go @@ -208,7 +208,7 @@ func IsIp(ipstr string) bool { } // IsIpPort check if the string is ip:port. -// Play: +// Play: https://go.dev/play/p/xUmls_b9L29 func IsIpPort(str string) bool { host, port, err := net.SplitHostPort(str) if err != nil {