Skip to content

Commit c6b8b1e

Browse files
committed
doc: adding examples
1 parent 835fc01 commit c6b8b1e

File tree

8 files changed

+832
-4
lines changed

8 files changed

+832
-4
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
@samber: I sometimes forget to update this file. Ping me on [Twitter](https://twitter.com/samuelberthe) or open an issue in case of error. We need to keep a clear changelog for easier lib upgrade.
44

5+
## 1.30.1 (2022-10-06)
6+
7+
Fix:
8+
9+
- lo.Try1: remove generic type
10+
- lo.Validate: format error properly
11+
512
## 1.30.0 (2022-10-04)
613

714
Adding:

README.md

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,14 +1137,16 @@ sub := len("hellô")
11371137
Creates a tuple from a list of values.
11381138

11391139
```go
1140-
tuple1 := lo.T2[string, int]("x", 1)
1140+
tuple1 := lo.T2("x", 1)
11411141
// Tuple2[string, int]{A: "x", B: 1}
11421142

11431143
func example() (string, int) { return "y", 2 }
1144-
tuple2 := lo.T2[string, int](example())
1144+
tuple2 := lo.T2(example())
11451145
// Tuple2[string, int]{A: "y", B: 2}
11461146
```
11471147

1148+
[[play](https://go.dev/play/p/IllL3ZO4BQm)]
1149+
11481150
### Unpack2 -> Unpack9
11491151

11501152
Returns values contained in tuple.
@@ -1154,6 +1156,8 @@ r1, r2 := lo.Unpack2[string, int](lo.Tuple2[string, int]{"a", 1})
11541156
// "a", 1
11551157
```
11561158

1159+
[[play](https://go.dev/play/p/xVP_k0kJ96W)]
1160+
11571161
### Zip2 -> Zip9
11581162

11591163
Zip creates a slice of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on.
@@ -1165,6 +1169,8 @@ tuples := lo.Zip2[string, int]([]string{"a", "b"}, []int{1, 2})
11651169
// []Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B: 2}}
11661170
```
11671171

1172+
[[play](https://go.dev/play/p/jujaA6GaJTp)]
1173+
11681174
### Unzip2 -> Unzip9
11691175

11701176
Unzip accepts an array of grouped elements and creates an array regrouping the elements to their pre-zip configuration.
@@ -1175,6 +1181,8 @@ a, b := lo.Unzip2[string, int]([]Tuple2[string, int]{{A: "a", B: 1}, {A: "b", B:
11751181
// []int{1, 2}
11761182
```
11771183

1184+
[[play](https://go.dev/play/p/ciHugugvaAW)]
1185+
11781186
### ChannelDispatcher
11791187

11801188
Distributes messages from input channels into N child channels. Close events are propagated to children.
@@ -1982,6 +1990,8 @@ iter, err := lo.Attempt(0, func(i int) error {
19821990

19831991
For more advanced retry strategies (delay, exponential backoff...), please take a look on [cenkalti/backoff](https://github.com/cenkalti/backoff).
19841992

1993+
[[play](https://go.dev/play/p/3ggJZ2ZKcMj)]
1994+
19851995
### AttemptWithDelay
19861996

19871997
Invokes a function N times until it returns valid output, with a pause between each call. Returning either the caught error or nil.
@@ -2003,6 +2013,8 @@ iter, duration, err := lo.AttemptWithDelay(5, 2*time.Second, func(i int, duratio
20032013

20042014
For more advanced retry strategies (delay, exponential backoff...), please take a look on [cenkalti/backoff](https://github.com/cenkalti/backoff).
20052015

2016+
[[play](https://go.dev/play/p/tVs6CygC7m1)]
2017+
20062018
### Debounce
20072019

20082020
`NewDebounce` creates a debounced instance that delays invoking functions given until after wait milliseconds have elapsed, until `cancel` is called.
@@ -2021,6 +2033,8 @@ time.Sleep(1 * time.Second)
20212033
cancel()
20222034
```
20232035

2036+
[[play](https://go.dev/play/p/mz32VMK2nqe)]
2037+
20242038
### Synchronize
20252039

20262040
Wraps the underlying callback in a mutex. It receives an optional mutex.
@@ -2094,6 +2108,8 @@ val := lo.Validate(len(slice) == 0, "Slice should be empty but contains %v", sli
20942108
// nil
20952109
```
20962110

2111+
[[play](https://go.dev/play/p/vPyh51XpCBt)]
2112+
20972113
### Must
20982114

20992115
Wraps a function call to panics if second argument is `error` or `false`, returns the value otherwise.
@@ -2106,6 +2122,8 @@ val := lo.Must(time.Parse("2006-01-02", "bad-value"))
21062122
// panics
21072123
```
21082124

2125+
[[play](https://go.dev/play/p/TMoWrRp3DyC)]
2126+
21092127
### Must{0->6}
21102128

21112129
Must\* has the same behavior than Must, but returns multiple values.
@@ -2152,6 +2170,8 @@ lo.Must0(lo.Contains[int](list, item), "'%s' must always contain '%s'", list, it
21522170
...
21532171
```
21542172

2173+
[[play](https://go.dev/play/p/TMoWrRp3DyC)]
2174+
21552175
### Try
21562176

21572177
Calls the function and return false in case of error and on panic.
@@ -2174,6 +2194,8 @@ ok := lo.Try(func() error {
21742194
// false
21752195
```
21762196

2197+
[[play](https://go.dev/play/p/mTyyWUvn9u4)]
2198+
21772199
### Try{0->6}
21782200

21792201
The same behavior than `Try`, but callback returns 2 variables.
@@ -2186,6 +2208,8 @@ ok := lo.Try2(func() (string, error) {
21862208
// false
21872209
```
21882210

2211+
[[play](https://go.dev/play/p/mTyyWUvn9u4)]
2212+
21892213
### TryOr
21902214

21912215
Calls the function and return a default value in case of error and on panic.
@@ -2211,6 +2235,8 @@ ok := lo.TryOr(func() error {
22112235
// false
22122236
```
22132237

2238+
[[play](https://go.dev/play/p/B4F7Wg2Zh9X)]
2239+
22142240
### TryOr{0->6}
22152241

22162242
The same behavior than `TryOr`, but callback returns 2 variables.
@@ -2225,6 +2251,8 @@ str, nbr, ok := lo.TryOr2(func() (string, int, error) {
22252251
// false
22262252
```
22272253

2254+
[[play](https://go.dev/play/p/B4F7Wg2Zh9X)]
2255+
22282256
### TryWithErrorValue
22292257

22302258
The same behavior than `Try`, but also returns value passed to panic.
@@ -2237,6 +2265,8 @@ err, ok := lo.TryWithErrorValue(func() error {
22372265
// "error", false
22382266
```
22392267

2268+
[[play](https://go.dev/play/p/Kc7afQIT2Fs)]
2269+
22402270
### TryCatch
22412271

22422272
The same behavior than `Try`, but calls the catch function in case of error.
@@ -2254,6 +2284,8 @@ ok := lo.TryCatch(func() error {
22542284
// caught == true
22552285
```
22562286

2287+
[[play](https://go.dev/play/p/PnOON-EqBiU)]
2288+
22572289
### TryCatchWithErrorValue
22582290

22592291
The same behavior than `TryWithErrorValue`, but calls the catch function in case of error.
@@ -2271,6 +2303,8 @@ ok := lo.TryCatchWithErrorValue(func() error {
22712303
// caught == true
22722304
```
22732305

2306+
[[play](https://go.dev/play/p/8Pc9gwX_GZO)]
2307+
22742308
### ErrorsAs
22752309

22762310
A shortcut for:
@@ -2294,6 +2328,8 @@ if rateLimitErr, ok := lo.ErrorsAs[*RateLimitError](err); ok {
22942328
}
22952329
```
22962330

2331+
[[play](https://go.dev/play/p/8wk5rH8UfrE)]
2332+
22972333
## 🛩 Benchmark
22982334

22992335
We executed a simple benchmark with the a dead-simple `lo.Map` loop:

errors.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ import (
77
)
88

99
// Validate is a helper that creates an error when a condition is not met.
10+
// Play: https://go.dev/play/p/vPyh51XpCBt
1011
func Validate(ok bool, format string, args ...any) error {
1112
if !ok {
12-
return errors.New(fmt.Sprint(format, args))
13+
return errors.New(fmt.Sprintf(format, args...))
1314
}
1415
return nil
1516
}
@@ -59,46 +60,54 @@ func must(err any, messageArgs ...interface{}) {
5960

6061
// Must is a helper that wraps a call to a function returning a value and an error
6162
// and panics if err is error or false.
63+
// Play: https://go.dev/play/p/TMoWrRp3DyC
6264
func Must[T any](val T, err any, messageArgs ...interface{}) T {
6365
must(err, messageArgs...)
6466
return val
6567
}
6668

6769
// Must0 has the same behavior than Must, but callback returns no variable.
70+
// Play: https://go.dev/play/p/TMoWrRp3DyC
6871
func Must0(err any, messageArgs ...interface{}) {
6972
must(err, messageArgs...)
7073
}
7174

7275
// Must1 is an alias to Must
76+
// Play: https://go.dev/play/p/TMoWrRp3DyC
7377
func Must1[T any](val T, err any, messageArgs ...interface{}) T {
7478
return Must(val, err, messageArgs...)
7579
}
7680

7781
// Must2 has the same behavior than Must, but callback returns 2 variables.
82+
// Play: https://go.dev/play/p/TMoWrRp3DyC
7883
func Must2[T1 any, T2 any](val1 T1, val2 T2, err any, messageArgs ...interface{}) (T1, T2) {
7984
must(err, messageArgs...)
8085
return val1, val2
8186
}
8287

8388
// Must3 has the same behavior than Must, but callback returns 3 variables.
89+
// Play: https://go.dev/play/p/TMoWrRp3DyC
8490
func Must3[T1 any, T2 any, T3 any](val1 T1, val2 T2, val3 T3, err any, messageArgs ...interface{}) (T1, T2, T3) {
8591
must(err, messageArgs...)
8692
return val1, val2, val3
8793
}
8894

8995
// Must4 has the same behavior than Must, but callback returns 4 variables.
96+
// Play: https://go.dev/play/p/TMoWrRp3DyC
9097
func Must4[T1 any, T2 any, T3 any, T4 any](val1 T1, val2 T2, val3 T3, val4 T4, err any, messageArgs ...interface{}) (T1, T2, T3, T4) {
9198
must(err, messageArgs...)
9299
return val1, val2, val3, val4
93100
}
94101

95102
// Must5 has the same behavior than Must, but callback returns 5 variables.
103+
// Play: https://go.dev/play/p/TMoWrRp3DyC
96104
func Must5[T1 any, T2 any, T3 any, T4 any, T5 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, err any, messageArgs ...interface{}) (T1, T2, T3, T4, T5) {
97105
must(err, messageArgs...)
98106
return val1, val2, val3, val4, val5
99107
}
100108

101109
// Must6 has the same behavior than Must, but callback returns 6 variables.
110+
// Play: https://go.dev/play/p/TMoWrRp3DyC
102111
func Must6[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any](val1 T1, val2 T2, val3 T3, val4 T4, val5 T5, val6 T6, err any, messageArgs ...interface{}) (T1, T2, T3, T4, T5, T6) {
103112
must(err, messageArgs...)
104113
return val1, val2, val3, val4, val5, val6
@@ -123,6 +132,7 @@ func Try(callback func() error) (ok bool) {
123132
}
124133

125134
// Try0 has the same behavior than Try, but callback returns no variable.
135+
// Play: https://go.dev/play/p/mTyyWUvn9u4
126136
func Try0(callback func()) bool {
127137
return Try(func() error {
128138
callback()
@@ -131,11 +141,13 @@ func Try0(callback func()) bool {
131141
}
132142

133143
// Try1 is an alias to Try.
134-
func Try1[T any](callback func() error) bool {
144+
// Play: https://go.dev/play/p/mTyyWUvn9u4
145+
func Try1(callback func() error) bool {
135146
return Try(callback)
136147
}
137148

138149
// Try2 has the same behavior than Try, but callback returns 2 variables.
150+
// Play: https://go.dev/play/p/mTyyWUvn9u4
139151
func Try2[T any](callback func() (T, error)) bool {
140152
return Try(func() error {
141153
_, err := callback()
@@ -144,6 +156,7 @@ func Try2[T any](callback func() (T, error)) bool {
144156
}
145157

146158
// Try3 has the same behavior than Try, but callback returns 3 variables.
159+
// Play: https://go.dev/play/p/mTyyWUvn9u4
147160
func Try3[T, R any](callback func() (T, R, error)) bool {
148161
return Try(func() error {
149162
_, _, err := callback()
@@ -152,6 +165,7 @@ func Try3[T, R any](callback func() (T, R, error)) bool {
152165
}
153166

154167
// Try4 has the same behavior than Try, but callback returns 4 variables.
168+
// Play: https://go.dev/play/p/mTyyWUvn9u4
155169
func Try4[T, R, S any](callback func() (T, R, S, error)) bool {
156170
return Try(func() error {
157171
_, _, _, err := callback()
@@ -160,6 +174,7 @@ func Try4[T, R, S any](callback func() (T, R, S, error)) bool {
160174
}
161175

162176
// Try5 has the same behavior than Try, but callback returns 5 variables.
177+
// Play: https://go.dev/play/p/mTyyWUvn9u4
163178
func Try5[T, R, S, Q any](callback func() (T, R, S, Q, error)) bool {
164179
return Try(func() error {
165180
_, _, _, _, err := callback()
@@ -168,6 +183,7 @@ func Try5[T, R, S, Q any](callback func() (T, R, S, Q, error)) bool {
168183
}
169184

170185
// Try6 has the same behavior than Try, but callback returns 6 variables.
186+
// Play: https://go.dev/play/p/mTyyWUvn9u4
171187
func Try6[T, R, S, Q, U any](callback func() (T, R, S, Q, U, error)) bool {
172188
return Try(func() error {
173189
_, _, _, _, _, err := callback()
@@ -176,11 +192,13 @@ func Try6[T, R, S, Q, U any](callback func() (T, R, S, Q, U, error)) bool {
176192
}
177193

178194
// TryOr has the same behavior than Must, but returns a default value in case of error.
195+
// Play: https://go.dev/play/p/B4F7Wg2Zh9X
179196
func TryOr[A any](callback func() (A, error), fallbackA A) (A, bool) {
180197
return TryOr1(callback, fallbackA)
181198
}
182199

183200
// TryOr1 has the same behavior than Must, but returns a default value in case of error.
201+
// Play: https://go.dev/play/p/B4F7Wg2Zh9X
184202
func TryOr1[A any](callback func() (A, error), fallbackA A) (A, bool) {
185203
ok := false
186204

@@ -196,6 +214,7 @@ func TryOr1[A any](callback func() (A, error), fallbackA A) (A, bool) {
196214
}
197215

198216
// TryOr2 has the same behavior than Must, but returns a default value in case of error.
217+
// Play: https://go.dev/play/p/B4F7Wg2Zh9X
199218
func TryOr2[A any, B any](callback func() (A, B, error), fallbackA A, fallbackB B) (A, B, bool) {
200219
ok := false
201220

@@ -212,6 +231,7 @@ func TryOr2[A any, B any](callback func() (A, B, error), fallbackA A, fallbackB
212231
}
213232

214233
// TryOr3 has the same behavior than Must, but returns a default value in case of error.
234+
// Play: https://go.dev/play/p/B4F7Wg2Zh9X
215235
func TryOr3[A any, B any, C any](callback func() (A, B, C, error), fallbackA A, fallbackB B, fallbackC C) (A, B, C, bool) {
216236
ok := false
217237

@@ -229,6 +249,7 @@ func TryOr3[A any, B any, C any](callback func() (A, B, C, error), fallbackA A,
229249
}
230250

231251
// TryOr4 has the same behavior than Must, but returns a default value in case of error.
252+
// Play: https://go.dev/play/p/B4F7Wg2Zh9X
232253
func TryOr4[A any, B any, C any, D any](callback func() (A, B, C, D, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D) (A, B, C, D, bool) {
233254
ok := false
234255

@@ -247,6 +268,7 @@ func TryOr4[A any, B any, C any, D any](callback func() (A, B, C, D, error), fal
247268
}
248269

249270
// TryOr5 has the same behavior than Must, but returns a default value in case of error.
271+
// Play: https://go.dev/play/p/B4F7Wg2Zh9X
250272
func TryOr5[A any, B any, C any, D any, E any](callback func() (A, B, C, D, E, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D, fallbackE E) (A, B, C, D, E, bool) {
251273
ok := false
252274

@@ -266,6 +288,7 @@ func TryOr5[A any, B any, C any, D any, E any](callback func() (A, B, C, D, E, e
266288
}
267289

268290
// TryOr6 has the same behavior than Must, but returns a default value in case of error.
291+
// Play: https://go.dev/play/p/B4F7Wg2Zh9X
269292
func TryOr6[A any, B any, C any, D any, E any, F any](callback func() (A, B, C, D, E, F, error), fallbackA A, fallbackB B, fallbackC C, fallbackD D, fallbackE E, fallbackF F) (A, B, C, D, E, F, bool) {
270293
ok := false
271294

@@ -286,6 +309,7 @@ func TryOr6[A any, B any, C any, D any, E any, F any](callback func() (A, B, C,
286309
}
287310

288311
// TryWithErrorValue has the same behavior than Try, but also returns value passed to panic.
312+
// Play: https://go.dev/play/p/Kc7afQIT2Fs
289313
func TryWithErrorValue(callback func() error) (errorValue any, ok bool) {
290314
ok = true
291315

@@ -306,20 +330,23 @@ func TryWithErrorValue(callback func() error) (errorValue any, ok bool) {
306330
}
307331

308332
// TryCatch has the same behavior than Try, but calls the catch function in case of error.
333+
// Play: https://go.dev/play/p/PnOON-EqBiU
309334
func TryCatch(callback func() error, catch func()) {
310335
if !Try(callback) {
311336
catch()
312337
}
313338
}
314339

315340
// TryCatchWithErrorValue has the same behavior than TryWithErrorValue, but calls the catch function in case of error.
341+
// Play: https://go.dev/play/p/8Pc9gwX_GZO
316342
func TryCatchWithErrorValue(callback func() error, catch func(any)) {
317343
if err, ok := TryWithErrorValue(callback); !ok {
318344
catch(err)
319345
}
320346
}
321347

322348
// ErrorsAs is a shortcut for errors.As(err, &&T).
349+
// Play: https://go.dev/play/p/8wk5rH8UfrE
323350
func ErrorsAs[T error](err error) (T, bool) {
324351
var t T
325352
ok := errors.As(err, &t)

0 commit comments

Comments
 (0)