1
1
/*
2
- Package try is a package for try.ToX functions that implement the error
3
- checking. try.ToX functions check 'if err != nil' and if it throws the err to the
2
+ Package try is a package for [To], [To1], and [To2] functions that implement the error
3
+ checking. [To] functions check 'if err != nil' and if it throws the err to the
4
4
error handlers, which are implemented by the err2 package. More information
5
5
about err2 and try packager roles can be seen in the FileCopy example:
6
6
@@ -20,31 +20,33 @@ about err2 and try packager roles can be seen in the FileCopy example:
20
20
21
21
# try.To — Fast Checking
22
22
23
- All of the try.To functions are as fast as the simple 'if err != nil {'
23
+ All of the [To] functions are as fast as the simple 'if err != nil {'
24
24
statement, thanks to the compiler inlining and optimization.
25
25
26
- Note that try.ToX function names end to a number (x) because:
26
+ We have three error check functions: [To], [To1], and [To2] because:
27
27
28
28
"No variadic type parameters. There is no support for variadic type parameters,
29
29
which would permit writing a single generic function that takes different
30
30
numbers of both type parameters and regular parameters." - Go Generics
31
31
32
- The leading number at the end of the To2 tells that To2 takes two different
33
- non-error arguments, and the third one must be an error value.
32
+ For example, the leading number at the end of the [ To2] tells that [ To2] takes
33
+ two different non-error arguments, and the third one must be an error value.
34
34
35
- Looking at the FileCopy example again, you see that all the functions
36
- are directed to try. To1 are returning (type1, error) tuples. All of these
37
- tuples are the correct input to try. To1. However, if you have a function that
38
- returns (type1, type2, error), you must use try. To2 function to check the error.
39
- Currently the try. To3 takes (3 + 1) return values which is the greatest amount.
35
+ Looking at the [CopyFile] example again, you see that all the functions
36
+ are directed to [ To1] are returning (type1, error) tuples. All of these
37
+ tuples are the correct input to [ To1] . However, if you have a function that
38
+ returns (type1, type2, error), you must use [ To2] function to check the error.
39
+ Currently the [ To3] takes (3 + 1) return values which is the greatest amount.
40
40
If more is needed, let us know.
41
41
42
42
# try.Out — Error Handling Language
43
43
44
- The try package offers an error handling DSL. It's for cases where you want to
45
- do something specific after error returing function call. For example, you might
46
- want to ignore the specific error and use a default value. That's possible with
47
- the following code:
44
+ The try package offers an error handling DSL that's based on [Out], [Out1], and
45
+ [Out2] functions and their corresponding return values [Result], [Result1], and
46
+ [Result2]. DSL is for the cases where you want to do something specific after
47
+ error returning function call. Those cases are rare. But you might want, for
48
+ example, to ignore the specific error and use a default value without any
49
+ special error handling. That's possible with the following code:
48
50
49
51
number := try.Out1(strconv.Atoi(str)).Catch(100)
50
52
@@ -71,7 +73,7 @@ import (
71
73
// check the value. If an error occurs, it panics the error so that err2
72
74
// handlers can catch it if needed. Note! If no err2.Handle or err2.Catch exist
73
75
// in the call stack and To panics an error, the error is not handled, and the
74
- // app will crash. When using try. To functions you should always have proper
76
+ // app will crash. When using To function you should always have proper
75
77
// err2.Handle or err2.Catch statements in the call stack.
76
78
//
77
79
// defer err2.Handle(&err)
@@ -87,7 +89,7 @@ func To(err error) {
87
89
// and check the error value. If an error occurs, it panics the error so that
88
90
// err2 handlers can catch it if needed. Note! If no err2.Handle or err2.Catch
89
91
// exist in the call stack and To1 panics an error, the error is not handled,
90
- // and the app will crash. When using try. To1 functions you should always have
92
+ // and the app will crash. When using To1 function you should always have
91
93
// proper err2.Handle or err2.Catch statements in the call stack.
92
94
//
93
95
// defer err2.Handle(&err)
@@ -102,7 +104,7 @@ func To1[T any](v T, err error) T {
102
104
// and check the error value. If an error occurs, it panics the error so that
103
105
// err2 handlers can catch it if needed. Note! If no err2.Handle or err2.Catch
104
106
// exist in the call stack and To2 panics an error, the error is not handled,
105
- // and the app will crash. When using try. To2 functions you should always have
107
+ // and the app will crash. When using To2 function you should always have
106
108
// proper err2.Handle or err2.Catch statements in the call stack.
107
109
//
108
110
// defer err2.Handle(&err)
@@ -117,7 +119,7 @@ func To2[T, U any](v1 T, v2 U, err error) (T, U) {
117
119
// error) and check the error value. If an error occurs, it panics the error so
118
120
// that err2 handlers can catch it if needed. Note! If no err2.Handle or
119
121
// err2.Catch exist in the call stack and To3 panics an error, the error is
120
- // not handled, and the app will crash. When using try. To3 functions you should
122
+ // not handled, and the app will crash. When using To3 function you should
121
123
// always have proper err2.Handle or err2.Catch statements in the call stack.
122
124
func To3 [T , U , V any ](v1 T , v2 U , v3 V , err error ) (T , U , V ) {
123
125
To (err )
0 commit comments