Skip to content

Commit ff1e0c4

Browse files
author
yangyile
committed
添加数组相关的函数
1 parent f01a9de commit ff1e0c4

File tree

6 files changed

+327
-56
lines changed

6 files changed

+327
-56
lines changed

README.md

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,38 +9,38 @@
99

1010
**rese** simplifies Go error handling and result extraction for multi-value function calls. It combines error and result checks into a single operation.
1111

12-
**rese** stands for **res** (result) + **err** (error).
12+
Example, the common way to handle errors in Go is like this:
13+
```
14+
num, err := run()
15+
if err != nil {
16+
panic(err)
17+
}
1318
14-
---
19+
ans, err := run2()
20+
if err != nil {
21+
panic(err)
22+
}
1523
16-
## CHINESE README
24+
res := num + ans
25+
fmt.Println(res)
26+
```
1727

18-
[中文说明](README.zh.md)
28+
Simple(Use **rese** package):
29+
```
30+
res := rese.V1(run()) + rese.V1(run2())
31+
fmt.Println(res)
32+
```
33+
34+
**rese** stands for **res** (result) + **err** (error).
1935

2036
---
2137

22-
## Installation
38+
## CHINESE README
2339

24-
```bash
25-
go get github.com/yyle88/rese
26-
```
40+
[中文说明](README.zh.md)
2741

2842
---
2943

30-
## Functions
31-
32-
| Function | Purpose | Returns |
33-
|-----------------------------------------------------------|---------------------------------------------------------------------------------------------|----------------------------------------------------------|
34-
| `V0(err error)` | Checks the error and panics if it's not `nil`. No return value. | None |
35-
| `V1[T1 any](v1 T1, err error) T1` | Checks the error, and if no error, returns `v1`. | Returns the value of type `T1` |
36-
| `V2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2)` | Checks the error, and if no error, returns `v1` and `v2`. | Returns `v1` of type `T1` and `v2` of type `T2` |
37-
| `P0(err error)` | Checks the error and panics if it's not `nil`. No return value. | None |
38-
| `P1[T1 any](v1 *T1, err error) *T1` | Checks the error, checks that `v1` is non-`nil`, and returns `v1`. | Returns a pointer to `v1` of type `T1` |
39-
| `P2[T1, T2 any](v1 *T1, v2 *T2, err error) (*T1, *T2)` | Checks the error, checks that `v1` and `v2` are non-`nil`, and returns `v1` and `v2`. | Returns pointers to `v1` and `v2` of types `T1` and `T2` |
40-
| `C0(err error)` | Checks the error and panics if it's not `nil`. No return value. | None |
41-
| `C1[T1 comparable](v1 T1, err error) T1` | Checks the error, checks that `v1` is not a zero value, and returns `v1`. | Returns `v1` of type `T1` |
42-
| `C2[T1, T2 comparable](v1 T1, v2 T2, err error) (T1, T2)` | Checks the error, checks that `v1` and `v2` are not zero values, and returns `v1` and `v2`. | Returns `v1` of type `T1` and `v2` of type `T2` |
43-
4444
## Examples
4545

4646
### Example 1: Simple error and result checking with `V1`
@@ -59,7 +59,7 @@ func run() (string, error) {
5959

6060
func main() {
6161
// Using V1 to check for error and get the result
62-
result := rese.V1(run())
62+
result := rese.V1(run())
6363
fmt.Println(result) // Outputs: Hello, World!
6464
}
6565
```
@@ -81,7 +81,7 @@ func getSomething() (*int64, error) {
8181

8282
func main() {
8383
// Using P1 to check error and ensure non-nil pointer
84-
ptr := rese.P1(getSomething())
84+
ptr := rese.P1(getSomething())
8585
fmt.Println(*ptr) // Outputs: 42
8686
}
8787
```
@@ -107,6 +107,32 @@ func main() {
107107
}
108108
```
109109

110+
---
111+
112+
## Installation
113+
114+
```bash
115+
go get github.com/yyle88/rese
116+
```
117+
118+
---
119+
120+
## Functions
121+
122+
| Function | Purpose | Returns |
123+
|-----------------------------------------------------------|---------------------------------------------------------------------------------------------|----------------------------------------------------------|
124+
| `V0(err error)` | Checks the error and panics if it's not `nil`. No return value. | None |
125+
| `V1[T1 any](v1 T1, err error) T1` | Checks the error, and if no error, returns `v1`. | Returns the value of type `T1` |
126+
| `V2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2)` | Checks the error, and if no error, returns `v1` and `v2`. | Returns `v1` of type `T1` and `v2` of type `T2` |
127+
| `P0(err error)` | Checks the error and panics if it's not `nil`. No return value. | None |
128+
| `P1[T1 any](v1 *T1, err error) *T1` | Checks the error, checks that `v1` is non-`nil`, and returns `v1`. | Returns a pointer to `v1` of type `T1` |
129+
| `P2[T1, T2 any](v1 *T1, v2 *T2, err error) (*T1, *T2)` | Checks the error, checks that `v1` and `v2` are non-`nil`, and returns `v1` and `v2`. | Returns pointers to `v1` and `v2` of types `T1` and `T2` |
130+
| `C0(err error)` | Checks the error and panics if it's not `nil`. No return value. | None |
131+
| `C1[T1 comparable](v1 T1, err error) T1` | Checks the error, checks that `v1` is not a zero value, and returns `v1`. | Returns `v1` of type `T1` |
132+
| `C2[T1, T2 comparable](v1 T1, v2 T2, err error) (T1, T2)` | Checks the error, checks that `v1` and `v2` are not zero values, and returns `v1` and `v2`. | Returns `v1` of type `T1` and `v2` of type `T2` |
133+
134+
---
135+
110136
## License
111137

112138
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

README.zh.md

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,36 @@
11
# rese
22

3-
**rese** 简化了 Go 中的错误处理和多值函数返回值提取。它将错误检查和结果提取合并为一个操作
3+
**rese** 简化 Go 中的错误处理,使您可以直接使用结果而无需检查错误
44

5-
**rese** 代表 **res**(结果)+ **err**(错误)。
6-
7-
## English README
5+
比如,在Go中比较常见的错误处理方式是这样的:
6+
```
7+
num, err := run()
8+
if err != nil {
9+
panic(err)
10+
}
811
9-
[English documentation](README.md)
12+
ans, err := run2()
13+
if err != nil {
14+
panic(err)
15+
}
1016
11-
## 安装
17+
res := num + ans
18+
fmt.Println(res)
19+
```
1220

13-
```bash
14-
go get github.com/yyle88/rese
21+
简化逻辑(使用 **rese** 工具):
22+
```
23+
res := rese.V1(run()) + rese.V1(run2())
24+
fmt.Println(res)
1525
```
1626

17-
---
27+
**rese** 代表 **res**(结果)+ **err**(错误)。
1828

19-
## 函数
29+
## English README
2030

21-
| 函数 | 作用 | 返回值 |
22-
|-----------------------------------------------------------|--------------------------------------------------|-------------------------------------|
23-
| `V0(err error)` | 检查错误,如果错误不为 `nil` 则触发 panic。没有返回值。 ||
24-
| `V1[T1 any](v1 T1, err error) T1` | 检查错误,如果没有错误,返回 `v1`| 返回类型为 `T1``v1` |
25-
| `V2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2)` | 检查错误,如果没有错误,返回 `v1``v2`| 返回类型为 `T1``v1` 和 类型为 `T2``v2` |
26-
| `P0(err error)` | 检查错误,如果错误不为 `nil` 则触发 panic。没有返回值。 ||
27-
| `P1[T1 any](v1 *T1, err error) *T1` | 检查错误,检查 `v1` 是否为非 `nil`,并返回 `v1`| 返回类型为 `T1``v1` 的指针 |
28-
| `P2[T1, T2 any](v1 *T1, v2 *T2, err error) (*T1, *T2)` | 检查错误,检查 `v1``v2` 是否为非 `nil`,并返回 `v1``v2`| 返回类型为 `T1``T2``v1``v2` 的指针 |
29-
| `C0(err error)` | 检查错误,如果错误不为 `nil` 则触发 panic。没有返回值。 ||
30-
| `C1[T1 comparable](v1 T1, err error) T1` | 检查错误,检查 `v1` 是否为零值,如果不是零值,返回 `v1`| 返回类型为 `T1``v1` |
31-
| `C2[T1, T2 comparable](v1 T1, v2 T2, err error) (T1, T2)` | 检查错误,检查 `v1``v2` 是否为零值,如果不是零值,返回 `v1``v2`| 返回类型为 `T1``v1` 和 类型为 `T2``v2` |
31+
[English documentation](README.md)
32+
33+
---
3234

3335
## 示例
3436

@@ -48,7 +50,7 @@ func run() (string, error) {
4850

4951
func main() {
5052
// 使用 V1 来检查错误并获取结果
51-
result := rese.V1(run())
53+
result := rese.V1(run())
5254
fmt.Println(result) // 输出: Hello, World!
5355
}
5456
```
@@ -70,7 +72,7 @@ func getSomething() (*int64, error) {
7072

7173
func main() {
7274
// 使用 P1 来检查错误并确保指针非 `nil`
73-
ptr := rese.P1(getSomething())
75+
ptr := rese.P1(getSomething())
7476
fmt.Println(*ptr) // 输出: 42
7577
}
7678
```
@@ -96,6 +98,30 @@ func main() {
9698
}
9799
```
98100

101+
## 安装
102+
103+
```bash
104+
go get github.com/yyle88/rese
105+
```
106+
107+
---
108+
109+
## 函数
110+
111+
| 函数 | 作用 | 返回值 |
112+
|-----------------------------------------------------------|--------------------------------------------------|-------------------------------------|
113+
| `V0(err error)` | 检查错误,如果错误不为 `nil` 则触发 panic。没有返回值。 ||
114+
| `V1[T1 any](v1 T1, err error) T1` | 检查错误,如果没有错误,返回 `v1`| 返回类型为 `T1``v1` |
115+
| `V2[T1, T2 any](v1 T1, v2 T2, err error) (T1, T2)` | 检查错误,如果没有错误,返回 `v1``v2`| 返回类型为 `T1``v1` 和 类型为 `T2``v2` |
116+
| `P0(err error)` | 检查错误,如果错误不为 `nil` 则触发 panic。没有返回值。 ||
117+
| `P1[T1 any](v1 *T1, err error) *T1` | 检查错误,检查 `v1` 是否为非 `nil`,并返回 `v1`| 返回类型为 `T1``v1` 的指针 |
118+
| `P2[T1, T2 any](v1 *T1, v2 *T2, err error) (*T1, *T2)` | 检查错误,检查 `v1``v2` 是否为非 `nil`,并返回 `v1``v2`| 返回类型为 `T1``T2``v1``v2` 的指针 |
119+
| `C0(err error)` | 检查错误,如果错误不为 `nil` 则触发 panic。没有返回值。 ||
120+
| `C1[T1 comparable](v1 T1, err error) T1` | 检查错误,检查 `v1` 是否为零值,如果不是零值,返回 `v1`| 返回类型为 `T1``v1` |
121+
| `C2[T1, T2 comparable](v1 T1, v2 T2, err error) (T1, T2)` | 检查错误,检查 `v1``v2` 是否为零值,如果不是零值,返回 `v1``v2`| 返回类型为 `T1``v1` 和 类型为 `T2``v2` |
122+
123+
---
124+
99125
## 许可协议
100126

101127
此项目采用 MIT 许可证,详情请参阅 [LICENSE](LICENSE) 文件。

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ go 1.22.8
44

55
require (
66
github.com/stretchr/testify v1.10.0
7-
github.com/yyle88/done v1.0.21
8-
github.com/yyle88/must v0.0.13
7+
github.com/yyle88/done v1.0.22
8+
github.com/yyle88/must v0.0.16
99
)
1010

1111
require (
1212
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
1313
github.com/pkg/errors v0.9.1 // indirect
1414
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
1515
github.com/yyle88/mutexmap v1.0.10 // indirect
16-
github.com/yyle88/zaplog v0.0.19 // indirect
16+
github.com/yyle88/zaplog v0.0.20 // indirect
1717
go.uber.org/multierr v1.11.0 // indirect
1818
go.uber.org/zap v1.27.0 // indirect
1919
gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU
1212
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
1313
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
1414
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
15-
github.com/yyle88/done v1.0.21 h1:KFmBJzHuPAfsOrQDz9YuwBZ9xSDH7QdkOPHJMQjZzZg=
16-
github.com/yyle88/done v1.0.21/go.mod h1:+MY1SL3TY4SrA2/rtsqeky82DHlDcsm1q1zfIMhANu4=
17-
github.com/yyle88/must v0.0.13 h1:VfhoGtm6M+lTBtLS+zYoth8HDczjTXELnasRbchKD1k=
18-
github.com/yyle88/must v0.0.13/go.mod h1:RI1P7b6Ao7EDYuBZgJYIuJcrwUxtxrTFA3UbU8xQZeg=
15+
github.com/yyle88/done v1.0.22 h1:iKiNY/yrSfdlsvX1bLXAZI18/p0I9KIzy/IMr3EP8zc=
16+
github.com/yyle88/done v1.0.22/go.mod h1:DaeF2NaYbUnvJKaNOAEQwT7PhVJbd8TNcT85ajIp1eU=
17+
github.com/yyle88/must v0.0.16 h1:0F6KDaMU5BCsH89U9mzLoVaeqXF7Zu3W0jeVj6C9LXI=
18+
github.com/yyle88/must v0.0.16/go.mod h1:eh05Aksb3JGi2tzb6OBgoY37icxT0OHmBQEM1dvMaVQ=
1919
github.com/yyle88/mutexmap v1.0.10 h1:PIsx9KTdK6h1yH5NzBrPl21RrZ/XsE4IbgulMlhJzZE=
2020
github.com/yyle88/mutexmap v1.0.10/go.mod h1:QUYDuARLPlGj414kHewQ5tt8jkDxQXoai8H3C4Gg+yc=
21-
github.com/yyle88/zaplog v0.0.19 h1:O4g7EO+uks7DkXbBXAje0LRvrBbkPuMD4gphuXw9jAo=
22-
github.com/yyle88/zaplog v0.0.19/go.mod h1:jN9/2IXYlpHOgoIyOTid1EPlUyGBWzFjr0dX9YKLkC0=
21+
github.com/yyle88/zaplog v0.0.20 h1:ByVj51j2C9p5tWw8wbIdmdrMK5IS6VECh/U746t321E=
22+
github.com/yyle88/zaplog v0.0.20/go.mod h1:jN9/2IXYlpHOgoIyOTid1EPlUyGBWzFjr0dX9YKLkC0=
2323
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
2424
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
2525
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=

rese.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,97 @@ func C9[T1, T2, T3, T4, T5, T6, T7, T8, T9 comparable](v1 T1, v2 T2, v3 T3, v4 T
241241
must.Nice(v9)
242242
return v1, v2, v3, v4, v5, v6, v7, v8, v9
243243
}
244+
245+
func A0(err error) {
246+
done.Done(err)
247+
}
248+
249+
func A1[T1 any](v1 []T1, err error) []T1 {
250+
done.Done(err)
251+
must.Have(v1)
252+
return v1
253+
}
254+
255+
func A2[T1, T2 any](v1 []T1, v2 []T2, err error) ([]T1, []T2) {
256+
done.Done(err)
257+
must.Have(v1)
258+
must.Have(v2)
259+
return v1, v2
260+
}
261+
262+
func A3[T1, T2, T3 any](v1 []T1, v2 []T2, v3 []T3, err error) ([]T1, []T2, []T3) {
263+
done.Done(err)
264+
must.Have(v1)
265+
must.Have(v2)
266+
must.Have(v3)
267+
return v1, v2, v3
268+
}
269+
270+
func A4[T1, T2, T3, T4 any](v1 []T1, v2 []T2, v3 []T3, v4 []T4, err error) ([]T1, []T2, []T3, []T4) {
271+
done.Done(err)
272+
must.Have(v1)
273+
must.Have(v2)
274+
must.Have(v3)
275+
must.Have(v4)
276+
return v1, v2, v3, v4
277+
}
278+
279+
func A5[T1, T2, T3, T4, T5 any](v1 []T1, v2 []T2, v3 []T3, v4 []T4, v5 []T5, err error) ([]T1, []T2, []T3, []T4, []T5) {
280+
done.Done(err)
281+
must.Have(v1)
282+
must.Have(v2)
283+
must.Have(v3)
284+
must.Have(v4)
285+
must.Have(v5)
286+
return v1, v2, v3, v4, v5
287+
}
288+
289+
func A6[T1, T2, T3, T4, T5, T6 any](v1 []T1, v2 []T2, v3 []T3, v4 []T4, v5 []T5, v6 []T6, err error) ([]T1, []T2, []T3, []T4, []T5, []T6) {
290+
done.Done(err)
291+
must.Have(v1)
292+
must.Have(v2)
293+
must.Have(v3)
294+
must.Have(v4)
295+
must.Have(v5)
296+
must.Have(v6)
297+
return v1, v2, v3, v4, v5, v6
298+
}
299+
300+
func A7[T1, T2, T3, T4, T5, T6, T7 any](v1 []T1, v2 []T2, v3 []T3, v4 []T4, v5 []T5, v6 []T6, v7 []T7, err error) ([]T1, []T2, []T3, []T4, []T5, []T6, []T7) {
301+
done.Done(err)
302+
must.Have(v1)
303+
must.Have(v2)
304+
must.Have(v3)
305+
must.Have(v4)
306+
must.Have(v5)
307+
must.Have(v6)
308+
must.Have(v7)
309+
return v1, v2, v3, v4, v5, v6, v7
310+
}
311+
312+
func A8[T1, T2, T3, T4, T5, T6, T7, T8 any](v1 []T1, v2 []T2, v3 []T3, v4 []T4, v5 []T5, v6 []T6, v7 []T7, v8 []T8, err error) ([]T1, []T2, []T3, []T4, []T5, []T6, []T7, []T8) {
313+
done.Done(err)
314+
must.Have(v1)
315+
must.Have(v2)
316+
must.Have(v3)
317+
must.Have(v4)
318+
must.Have(v5)
319+
must.Have(v6)
320+
must.Have(v7)
321+
must.Have(v8)
322+
return v1, v2, v3, v4, v5, v6, v7, v8
323+
}
324+
325+
func A9[T1, T2, T3, T4, T5, T6, T7, T8, T9 any](v1 []T1, v2 []T2, v3 []T3, v4 []T4, v5 []T5, v6 []T6, v7 []T7, v8 []T8, v9 []T9, err error) ([]T1, []T2, []T3, []T4, []T5, []T6, []T7, []T8, []T9) {
326+
done.Done(err)
327+
must.Have(v1)
328+
must.Have(v2)
329+
must.Have(v3)
330+
must.Have(v4)
331+
must.Have(v5)
332+
must.Have(v6)
333+
must.Have(v7)
334+
must.Have(v8)
335+
must.Have(v9)
336+
return v1, v2, v3, v4, v5, v6, v7, v8, v9
337+
}

0 commit comments

Comments
 (0)