mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
doc: add play ground demo
This commit is contained in:
@@ -903,6 +903,7 @@ import "github.com/duke-git/lancet/v2/maputil"
|
|||||||
[[play](https://go.dev/play/p/isZZHOsDhFc)]
|
[[play](https://go.dev/play/p/isZZHOsDhFc)]
|
||||||
- **<big>GetOrSet</big>** : returns value of the given key or set the given value value if not present.
|
- **<big>GetOrSet</big>** : returns value of the given key or set the given value value if not present.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#GetOrSet)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#GetOrSet)]
|
||||||
|
[[play](https://go.dev/play/p/IVQwO1OkEJC)]
|
||||||
- **<big>MapToStruct</big>** : converts map to struct.
|
- **<big>MapToStruct</big>** : converts map to struct.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#MapToStruct)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/maputil.md#MapToStruct)]
|
||||||
[[play](https://go.dev/play/p/7wYyVfX38Dp)]
|
[[play](https://go.dev/play/p/7wYyVfX38Dp)]
|
||||||
@@ -1209,11 +1210,13 @@ import "github.com/duke-git/lancet/v2/retry"
|
|||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#BackoffStrategy)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#BackoffStrategy)]
|
||||||
- **<big>RetryWithCustomBackoff</big>** : set abitary custom backoff strategy.
|
- **<big>RetryWithCustomBackoff</big>** : set abitary custom backoff strategy.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#RetryWithCustomBackoff)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#RetryWithCustomBackoff)]
|
||||||
|
[[play](https://go.dev/play/p/jIm_o2vb5Y4)]
|
||||||
- **<big>RetryWithLinearBackoff</big>** : set linear strategy backoff.
|
- **<big>RetryWithLinearBackoff</big>** : set linear strategy backoff.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#RetryWithLinearBackoff)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#RetryWithLinearBackoff)]
|
||||||
|
[[play](https://go.dev/play/p/PDet2ZQZwcB)]
|
||||||
- **<big>RetryWithExponentialWithJitterBackoff</big>** : set exponential strategy backoff.
|
- **<big>RetryWithExponentialWithJitterBackoff</big>** : set exponential strategy backoff.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#RetryWithExponentialWithJitterBackoff)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/retry.md#RetryWithExponentialWithJitterBackoff)]
|
||||||
|
[[play](https://go.dev/play/p/xp1avQmn16X)]
|
||||||
|
|
||||||
|
|
||||||
<h3 id="slice"> 18. Slice contains some functions to manipulate slice. <a href="#index">index</a></h3>
|
<h3 id="slice"> 18. Slice contains some functions to manipulate slice. <a href="#index">index</a></h3>
|
||||||
@@ -1418,6 +1421,7 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
[[play](https://go.dev/play/p/UR323iZLDpv)]
|
[[play](https://go.dev/play/p/UR323iZLDpv)]
|
||||||
- **<big>UniqueByField</big>** : remove duplicate elements in struct slice by struct field.
|
- **<big>UniqueByField</big>** : remove duplicate elements in struct slice by struct field.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#UniqueByField)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#UniqueByField)]
|
||||||
|
[[play](https://go.dev/play/p/6cifcZSPIGu)]
|
||||||
- **<big>Union</big>** : creates a slice of unique elements, in order, from all given slices.
|
- **<big>Union</big>** : creates a slice of unique elements, in order, from all given slices.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#Union)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/en/api/packages/slice.md#Union)]
|
||||||
[[play](https://go.dev/play/p/hfXV1iRIZOf)]
|
[[play](https://go.dev/play/p/hfXV1iRIZOf)]
|
||||||
|
|||||||
@@ -902,9 +902,10 @@ import "github.com/duke-git/lancet/v2/maputil"
|
|||||||
[[play](https://go.dev/play/p/N9qgYg_Ho6f)]
|
[[play](https://go.dev/play/p/N9qgYg_Ho6f)]
|
||||||
- **<big>HasKey</big>** : 检查 map 是否包含某个 key。
|
- **<big>HasKey</big>** : 检查 map 是否包含某个 key。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#HasKey)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#HasKey)]
|
||||||
|
[[play](https://go.dev/play/p/isZZHOsDhFc)]
|
||||||
- **<big>GetOrSet</big>** : 返回给定键的值,如果不存在则设置该值。
|
- **<big>GetOrSet</big>** : 返回给定键的值,如果不存在则设置该值。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#GetOrSet)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#GetOrSet)]
|
||||||
[[play](https://go.dev/play/p/isZZHOsDhFc)]
|
[[play](https://go.dev/play/p/IVQwO1OkEJC)]
|
||||||
- **<big>MapToStruct</big>** : 将map转成struct。
|
- **<big>MapToStruct</big>** : 将map转成struct。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#MapToStruct)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/maputil.md#MapToStruct)]
|
||||||
[[play](https://go.dev/play/p/7wYyVfX38Dp)]
|
[[play](https://go.dev/play/p/7wYyVfX38Dp)]
|
||||||
@@ -1209,10 +1210,13 @@ import "github.com/duke-git/lancet/v2/retry"
|
|||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#BackoffStrategy)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#BackoffStrategy)]
|
||||||
- **<big>RetryWithCustomBackoff</big>** : 设置自定义退避策略。
|
- **<big>RetryWithCustomBackoff</big>** : 设置自定义退避策略。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#RetryWithCustomBackoff)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#RetryWithCustomBackoff)]
|
||||||
|
[[play](https://go.dev/play/p/jIm_o2vb5Y4)]
|
||||||
- **<big>RetryWithLinearBackoff</big>** : 设置线性策略退避。
|
- **<big>RetryWithLinearBackoff</big>** : 设置线性策略退避。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#RetryWithLinearBackoff)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#RetryWithLinearBackoff)]
|
||||||
|
[[play](https://go.dev/play/p/PDet2ZQZwcB)]
|
||||||
- **<big>RetryWithExponentialWithJitterBackoff</big>** : 设置指数策略退避。
|
- **<big>RetryWithExponentialWithJitterBackoff</big>** : 设置指数策略退避。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#RetryWithExponentialWithJitterBackoff)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/retry.md#RetryWithExponentialWithJitterBackoff)]
|
||||||
|
[[play](https://go.dev/play/p/xp1avQmn16X)]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1416,8 +1420,9 @@ import "github.com/duke-git/lancet/v2/slice"
|
|||||||
- **<big>UniqueBy</big>** : 对切片的每个元素调用 iteratee 函数,然后删除重复元素。
|
- **<big>UniqueBy</big>** : 对切片的每个元素调用 iteratee 函数,然后删除重复元素。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#UniqueBy)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#UniqueBy)]
|
||||||
[[play](https://go.dev/play/p/UR323iZLDpv)]
|
[[play](https://go.dev/play/p/UR323iZLDpv)]
|
||||||
- **<big>UniqueByField</big>** : 根据struct字段对struct切片去重复
|
- **<big>UniqueByField</big>** : 根据struct字段对struct切片去重复。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#UniqueByField)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#UniqueByField)]
|
||||||
|
[[play](https://go.dev/play/p/6cifcZSPIGu)]
|
||||||
- **<big>Union</big>** : 合并多个切片。
|
- **<big>Union</big>** : 合并多个切片。
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#Union)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/api/packages/slice.md#Union)]
|
||||||
[[play](https://go.dev/play/p/hfXV1iRIZOf)]
|
[[play](https://go.dev/play/p/hfXV1iRIZOf)]
|
||||||
|
|||||||
@@ -1497,7 +1497,7 @@ func main() {
|
|||||||
func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V
|
func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;"></span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/IVQwO1OkEJC)</span></span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
|||||||
@@ -331,7 +331,7 @@ func main() {
|
|||||||
func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option
|
func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:</b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/jIm_o2vb5Y4)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -384,7 +384,7 @@ func main() {
|
|||||||
func RetryWithLinearBackoff(interval time.Duration) Option
|
func RetryWithLinearBackoff(interval time.Duration) Option
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:</b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/PDet2ZQZwcB)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -429,7 +429,7 @@ func main() {
|
|||||||
func RetryWithExponentialWithJitterBackoff(interval time.Duration, base uint64, maxJitter time.Duration) Option
|
func RetryWithExponentialWithJitterBackoff(interval time.Duration, base uint64, maxJitter time.Duration) Option
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:</b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/xp1avQmn16X)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
|||||||
@@ -2323,7 +2323,7 @@ func main() {
|
|||||||
func UniqueByField[T any](slice []T, field string) ([]T, error)
|
func UniqueByField[T any](slice []T, field string) ([]T, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>示例:<span style="float:right;display:inline-block;"></span></b>
|
<b>示例:<span style="float:right;display:inline-block;">[运行](https://go.dev/play/p/6cifcZSPIGu)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
|
|||||||
@@ -1513,7 +1513,7 @@ func main() {
|
|||||||
func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V
|
func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;"></span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/IVQwO1OkEJC)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
|||||||
@@ -331,7 +331,7 @@ func main() {
|
|||||||
func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option
|
func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/jIm_o2vb5Y4)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -361,7 +361,7 @@ func main() {
|
|||||||
return errors.New("error occurs")
|
return errors.New("error occurs")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := retry,Retry(increaseNumber, retry.RetryWithCustomBackoff(&ExampleCustomBackoffStrategy{interval: time.Microsecond * 50}))
|
err := retry.Retry(increaseNumber, retry.RetryWithCustomBackoff(&ExampleCustomBackoffStrategy{interval: time.Microsecond * 50}))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -384,7 +384,7 @@ func main() {
|
|||||||
func RetryWithLinearBackoff(interval time.Duration) Option
|
func RetryWithLinearBackoff(interval time.Duration) Option
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/nk2XRmagfVF)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
@@ -429,7 +429,7 @@ func main() {
|
|||||||
func RetryWithExponentialWithJitterBackoff(interval time.Duration, base uint64, maxJitter time.Duration) Option
|
func RetryWithExponentialWithJitterBackoff(interval time.Duration, base uint64, maxJitter time.Duration) Option
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:</b>
|
<b>Example:<span style="float:right;display:inline-block;">[Run](https://go.dev/play/p/xp1avQmn16X)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
|||||||
@@ -2321,7 +2321,7 @@ func main() {
|
|||||||
func UniqueByField[T any](slice []T, field string) ([]T, error)
|
func UniqueByField[T any](slice []T, field string) ([]T, error)
|
||||||
```
|
```
|
||||||
|
|
||||||
<b>Example:<span style="float:right;display:inline-block;"></span></b>
|
<b>Example:<span style="float:right;display:inline-block;">[Runs](https://go.dev/play/p/6cifcZSPIGu)</span></b>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
import (
|
import (
|
||||||
|
|||||||
@@ -438,7 +438,7 @@ func ToSortedSlicesWithComparator[K comparable, V any](m map[K]V, comparator fun
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetOrSet returns value of the given key or set the given value value if not present.
|
// GetOrSet returns value of the given key or set the given value value if not present.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/IVQwO1OkEJC
|
||||||
func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V {
|
func GetOrSet[K comparable, V any](m map[K]V, key K, value V) V {
|
||||||
if v, ok := m[key]; ok {
|
if v, ok := m[key]; ok {
|
||||||
return v
|
return v
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ func RetryTimes(n uint) Option {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RetryWithCustomBackoff set abitary custom backoff strategy
|
// RetryWithCustomBackoff set abitary custom backoff strategy
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/jIm_o2vb5Y4
|
||||||
func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option {
|
func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option {
|
||||||
if backoffStrategy == nil {
|
if backoffStrategy == nil {
|
||||||
panic("programming error: backoffStrategy must be not nil")
|
panic("programming error: backoffStrategy must be not nil")
|
||||||
@@ -57,7 +57,7 @@ func RetryWithCustomBackoff(backoffStrategy BackoffStrategy) Option {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RetryWithLinearBackoff set linear strategy backoff
|
// RetryWithLinearBackoff set linear strategy backoff
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/PDet2ZQZwcB
|
||||||
func RetryWithLinearBackoff(interval time.Duration) Option {
|
func RetryWithLinearBackoff(interval time.Duration) Option {
|
||||||
if interval <= 0 {
|
if interval <= 0 {
|
||||||
panic("programming error: retry interval should not be lower or equal to 0")
|
panic("programming error: retry interval should not be lower or equal to 0")
|
||||||
@@ -71,7 +71,7 @@ func RetryWithLinearBackoff(interval time.Duration) Option {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RetryWithExponentialWithJitterBackoff set exponential strategy backoff
|
// RetryWithExponentialWithJitterBackoff set exponential strategy backoff
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/xp1avQmn16X
|
||||||
func RetryWithExponentialWithJitterBackoff(interval time.Duration, base uint64, maxJitter time.Duration) Option {
|
func RetryWithExponentialWithJitterBackoff(interval time.Duration, base uint64, maxJitter time.Duration) Option {
|
||||||
if interval <= 0 {
|
if interval <= 0 {
|
||||||
panic("programming error: retry interval should not be lower or equal to 0")
|
panic("programming error: retry interval should not be lower or equal to 0")
|
||||||
|
|||||||
@@ -797,7 +797,7 @@ func UniqueBy[T comparable](slice []T, iteratee func(item T) T) []T {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UniqueByField remove duplicate elements in struct slice by struct field.
|
// UniqueByField remove duplicate elements in struct slice by struct field.
|
||||||
// Play: todo
|
// Play: https://go.dev/play/p/6cifcZSPIGu
|
||||||
func UniqueByField[T any](slice []T, field string) ([]T, error) {
|
func UniqueByField[T any](slice []T, field string) ([]T, error) {
|
||||||
seen := map[any]struct{}{}
|
seen := map[any]struct{}{}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user