57 lines
934 B
Markdown
57 lines
934 B
Markdown
# gotry
|
|
|
|
This package is a fork of lainio's err2 package with an improved code generator
|
|
|
|
## Add custom try package to your project
|
|
|
|
```
|
|
go install code.thetadev.de/ThetaDev/gotry/gotry_generate@0.1.0
|
|
|
|
gotry_generate -init
|
|
```
|
|
|
|
### Add type definitions
|
|
|
|
```
|
|
📂 try/types.csv
|
|
|
|
<Handler method name>;<Type>;<Import#1>;<Import#2>;...
|
|
|
|
# Example:
|
|
DB;*gorm.DB;gorm.io/gorm
|
|
|
|
# Add +s to the method name to generate 2 handlers for single values and slices
|
|
String+s;string
|
|
```
|
|
|
|
### Generate
|
|
|
|
```sh
|
|
go generate ./try
|
|
```
|
|
|
|
## Handle errors
|
|
|
|
Instead of the idiomatic go way of handling errors, which involves if-conditions for every possible
|
|
error, ...
|
|
|
|
```go
|
|
func myFunction() (err error) {
|
|
b, err := ioutil.ReadAll(r)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
```
|
|
|
|
you can wrap the error-throwing function in a try wrapper.
|
|
|
|
```go
|
|
func myFunction() (err error) {
|
|
defer err2.Return(&err)
|
|
|
|
b := try.Bytes(ioutil.ReadAll(r))
|
|
|
|
return
|
|
}
|
|
```
|