mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
doc: update for release v2.3.5
This commit is contained in:
40
README.md
40
README.md
@@ -508,22 +508,22 @@ import "github.com/duke-git/lancet/v2/datetime"
|
||||
[[play](https://go.dev/play/p/nT1heB1KUUK)]
|
||||
- **<big>AddWeek</big>** : 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)]
|
||||
- **<big>AddMonth</big>** : 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)]
|
||||
- **<big>AddYear</big>** : 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)]
|
||||
- **<big>AddDaySafe</big>** : 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)]
|
||||
- **<big>AddMonthSafe</big>** : 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)]
|
||||
- **<big>AddYearSafe</big>** : 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)]
|
||||
- **<big>BeginOfMinute</big>** : 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"
|
||||
|
||||
- **<big>NewEventBus</big>** : 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)]
|
||||
- **<big>Subscribe</big>** : 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-)]
|
||||
- **<big>Unsubscribe</big>** : 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)]
|
||||
- **<big>Publish</big>** : 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)]
|
||||
- **<big>ClearListeners</big>** : 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)]
|
||||
- **<big>ClearListenersByTopic</big>** : 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)]
|
||||
- **<big>GetListenersCount</big>** : 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)]
|
||||
- **<big>GetAllListenersCount</big>** : 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)]
|
||||
- **<big>GetEvents</big>** : 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)]
|
||||
- **<big>SetErrorHandler</big>** : 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)]
|
||||
|
||||
<h3 id="fileutil"> 9. Fileutil package implements some basic functions for file operations. <a href="#index">index</a></h3>
|
||||
|
||||
@@ -1479,7 +1479,7 @@ import "github.com/duke-git/lancet/v2/slice"
|
||||
[[play](https://go.dev/play/p/b9iygtgsHI1)]
|
||||
- **<big>EqualUnordered</big>** : 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)]
|
||||
- **<big>Every</big>** : 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)]
|
||||
- **<big>ShuffleCopy</big>** : 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)]
|
||||
- **<big>IsAscending</big>** : 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)]
|
||||
- **<big>ReverseCopy</big>** : 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-)]
|
||||
- **<big>Range</big>** : 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)]
|
||||
- **<big>FindAllOccurrences</big>** : 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)]
|
||||
|
||||
<h3 id="system"> 22. System package contain some functions about os, runtime, shell command. <a href="#index">index</a></h3>
|
||||
|
||||
@@ -2218,7 +2218,7 @@ import "github.com/duke-git/lancet/v2/validator"
|
||||
[[play](https://go.dev/play/p/AHA0r0AzIdC)]
|
||||
- **<big>IsIpPort</big>** : 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)]
|
||||
- **<big>IsStrongPassword</big>** : 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)]
|
||||
|
||||
@@ -509,22 +509,22 @@ import "github.com/duke-git/lancet/v2/datetime"
|
||||
[[play](https://go.dev/play/p/nT1heB1KUUK)]
|
||||
- **<big>AddWeek</big>** : 将日期加/减星期数.
|
||||
[[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)]
|
||||
- **<big>AddMonth</big>** : 将日期加/减月数.
|
||||
[[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)]
|
||||
- **<big>AddYear</big>** : 将日期加/减分年数。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/datetime.md#AddYear)]
|
||||
[[play](https://go.dev/play/p/MqW2ujnBx10)]
|
||||
- **<big>AddDaySafe</big>** : 增加/减少指定的天数,并确保日期是有效日期。
|
||||
[[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)]
|
||||
- **<big>AddMonthSafe</big>** : 增加/减少指定的月份,并确保日期是有效日期。
|
||||
[[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)]
|
||||
- **<big>AddYearSafe</big>** : 增加/减少指定的年份,并确保日期是有效日期。
|
||||
[[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)]
|
||||
- **<big>BeginOfMinute</big>** : 返回指定时间的分钟开始时间。
|
||||
[[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"
|
||||
|
||||
- **<big>NewEventBus</big>** : 创建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)]
|
||||
- **<big>Subscribe</big>** : 订阅具有特定事件主题和监听函数的事件。支持异步,事件优先级,事件过滤器。
|
||||
[[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-)]
|
||||
- **<big>Unsubscribe</big>** : 取消订阅具有特定事件主题的事件。
|
||||
[[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)]
|
||||
- **<big>Publish</big>** : 发布一个带有特定事件主题和数据负载的事件。
|
||||
[[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)]
|
||||
- **<big>ClearListeners</big>** : 清空所有事件监听器。
|
||||
[[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)]
|
||||
- **<big>ClearListenersByTopic</big>** : 清空特定事件监听器。
|
||||
[[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)]
|
||||
- **<big>GetListenersCount</big>** : 获取特定事件的监听器数量。
|
||||
[[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)]
|
||||
- **<big>GetAllListenersCount</big>** : 获取所有事件的监听器数量。
|
||||
[[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)]
|
||||
- **<big>GetEvents</big>** : 获取所有事件的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)]
|
||||
- **<big>SetErrorHandler</big>** : 设置事件的错误处理函数。
|
||||
[[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)]
|
||||
|
||||
<h3 id="fileutil"> 10. fileutil 包含文件基本操作。 <a href="#index">回到目录</a></h3>
|
||||
|
||||
@@ -1477,7 +1477,7 @@ import "github.com/duke-git/lancet/v2/slice"
|
||||
[[play](https://go.dev/play/p/b9iygtgsHI1)]
|
||||
- **<big>EqualUnordered</big>** : 检查两个切片是否相等,元素数量相同,值相等,不考虑元素顺序。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#EqualUnordered)]
|
||||
[[play](todo)]
|
||||
[[play](https://go.dev/play/p/n8fSc2w8ZgX)]
|
||||
- **<big>Every</big>** : 如果切片中的所有值都通过谓词函数,则返回 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)]
|
||||
- **<big>ReverseCopy</big>** : 反转切片中的元素顺序, 不改变原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-)]
|
||||
- **<big>Reduce<sup>deprecated</sup></big>** : 将切片中的元素依次运行 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)]
|
||||
- **<big>ShuffleCopy</big>** : 随机打乱切片中的元素顺序, 不改变原切片。
|
||||
[[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)]
|
||||
- **<big>IsAscending</big>** : 检查切片元素是否按升序排列。
|
||||
[[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)]
|
||||
- **<big>FindAllOccurrences</big>** : 返回子字符串在字符串中所有出现的位置。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/strutil.md#FindAllOccurrences)]
|
||||
[[play](todo)]
|
||||
[[play](https://go.dev/play/p/uvyA6azGLB1)]
|
||||
|
||||
|
||||
<h3 id="system"> 23. system 包含 os, runtime, shell command 的相关函数。 <a href="#index">回到目录</a></h3>
|
||||
@@ -2220,7 +2220,7 @@ import "github.com/duke-git/lancet/v2/validator"
|
||||
[[play](https://go.dev/play/p/AHA0r0AzIdC)]
|
||||
- **<big>IsIpPort</big>** : 检查字符串是否是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)]
|
||||
- **<big>IsStrongPassword</big>** : 验证字符串是否是强密码:(字母+数字+特殊字符)。
|
||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/validator.md#IsStrongPassword)]
|
||||
[[play](https://go.dev/play/p/QHdVcSQ3uDg)]
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -153,7 +153,7 @@ func main() {
|
||||
func AddWeek(t time.Time, weeks int64) time.Time
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/M9TqdMiaA2p)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -189,7 +189,7 @@ func main() {
|
||||
func AddMonth(t time.Time, months int64) time.Time
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/DLoiOnpLvsN)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -333,7 +333,7 @@ func main() {
|
||||
func AddDaySafe(t time.Time, days int) time.Time
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/JTohZFpoDJ3)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -380,7 +380,7 @@ func main() {
|
||||
func AddMonthSafe(t time.Time, months int) time.Time
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/KLw0lo6mbVW)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -417,7 +417,7 @@ func main() {
|
||||
func AddYearSafe(t time.Time, years int) time.Time
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/KVGXWZZ54ZH)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
@@ -48,7 +48,7 @@ import (
|
||||
func NewEventBus[T any]() *EventBus[T]
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/gHbOPV_NUOJ)</span></b>
|
||||
|
||||
```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)
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/EYGf_8cHei-)</span></b>
|
||||
|
||||
```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))
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/Tmh7Ttfvprf)</span></b>
|
||||
|
||||
```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() {
|
||||
<b>函数签名:</b>
|
||||
|
||||
```go
|
||||
func (eb *EventBus[T]) Publish(event Event[T])
|
||||
func (eb *EventBus[T]) Publish(event eventbus.Event[T])
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/gHTtVexFSH9)</span></b>
|
||||
|
||||
```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()
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/KBfBYlKPgqD)</span></b>
|
||||
|
||||
```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)
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/gvMljmJOZmU)</span></b>
|
||||
|
||||
```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
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/j6yaJ2xAmFz)</span></b>
|
||||
|
||||
```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
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/PUlr0xcpEOz)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
@@ -338,7 +344,7 @@ func main() {
|
||||
func (eb *EventBus[T]) GetEvents() []string
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/etgjjcOtAjX)</span></b>
|
||||
|
||||
```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))
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/gmB0gnFe5mc)</span></b>
|
||||
|
||||
```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
|
||||
|
||||
@@ -887,7 +887,7 @@ func main() {
|
||||
func EqualUnordered[T comparable](slice1, slice2 []T) bool
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/n8fSc2w8ZgX)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
@@ -1772,7 +1772,7 @@ func main() {
|
||||
func ReverseCopy[T any](slice []T) []T
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/c9arEaP7Cg-)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
@@ -2065,7 +2065,7 @@ func main() {
|
||||
func ShuffleCopy[T any](slice []T) []T
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/vqDa-Gs1vT0)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
|
||||
@@ -1772,7 +1772,7 @@ func main() {
|
||||
func FindAllOccurrences(str, substr string) []int
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/uvyA6azGLB1)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
|
||||
@@ -1001,7 +1001,7 @@ func main() {
|
||||
func IsIpPort(str string) bool
|
||||
```
|
||||
|
||||
<b>示例:<span style="float:right;display:inline-block">[运行](todo)</span></b>
|
||||
<b>示例:<span style="float:right;display:inline-block">[运行](https://go.dev/play/p/xUmls_b9L29)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
|
||||
@@ -155,7 +155,7 @@ func main() {
|
||||
func AddWeek(t time.Time, weeks int64) time.Time
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/M9TqdMiaA2p)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -191,7 +191,7 @@ func main() {
|
||||
func AddMonth(t time.Time, months int64) time.Time
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/DLoiOnpLvsN)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -335,7 +335,7 @@ func main() {
|
||||
func AddDaySafe(t time.Time, days int) time.Time
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/JTohZFpoDJ3)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -382,7 +382,7 @@ func main() {
|
||||
func AddMonthSafe(t time.Time, months int) time.Time
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/KLw0lo6mbVW)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
@@ -419,7 +419,7 @@ func main() {
|
||||
func AddYearSafe(t time.Time, years int) time.Time
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/KVGXWZZ54ZH)</span></b>
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
@@ -48,7 +48,7 @@ import (
|
||||
func NewEventBus[T any]() *EventBus[T]
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/gHbOPV_NUOJ)</span></b>
|
||||
|
||||
```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)
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/EYGf_8cHei-)</span></b>
|
||||
|
||||
```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))
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/Tmh7Ttfvprf)</span></b>
|
||||
|
||||
```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() {
|
||||
<b>Signature:</b>
|
||||
|
||||
```go
|
||||
func (eb *EventBus[T]) Publish(event Event[T])
|
||||
func (eb *EventBus[T]) Publish(event eventbus.Event[T])
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/gHTtVexFSH9)</span></b>
|
||||
|
||||
```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()
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/KBfBYlKPgqD)</span></b>
|
||||
|
||||
```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)
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/gvMljmJOZmU)</span></b>
|
||||
|
||||
```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
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/j6yaJ2xAmFz)</span></b>
|
||||
|
||||
```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
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/PUlr0xcpEOz)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
@@ -338,7 +344,7 @@ func main() {
|
||||
func (eb *EventBus[T]) GetEvents() []string
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/etgjjcOtAjX)</span></b>
|
||||
|
||||
```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))
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/gmB0gnFe5mc)</span></b>
|
||||
|
||||
```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
|
||||
|
||||
@@ -852,7 +852,7 @@ func main() {
|
||||
func EqualUnordered[T comparable](slice1, slice2 []T) bool
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[运行](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/n8fSc2w8ZgX)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
@@ -1768,7 +1768,7 @@ func main() {
|
||||
func ReverseCopy[T any](slice []T) []T
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/c9arEaP7Cg-)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
@@ -2062,7 +2062,7 @@ func main() {
|
||||
func ShuffleCopy[T any](slice []T) []T
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/vqDa-Gs1vT0)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
|
||||
@@ -1774,7 +1774,7 @@ func main() {
|
||||
func FindAllOccurrences(str, substr string) []int
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/uvyA6azGLB1)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
|
||||
@@ -1003,7 +1003,7 @@ func main() {
|
||||
func IsIpPort(str string) bool
|
||||
```
|
||||
|
||||
<b>Example:<span style="float:right;display:inline-block">[Run](todo)</span></b>
|
||||
<b>Example:<span style="float:right;display:inline-block">[Run](https://go.dev/play/p/xUmls_b9L29)</span></b>
|
||||
|
||||
```go
|
||||
import (
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user