https://github.com/dnmfarrell/go-errval
Provides a generic error catching interface to reduce boilerplate and fragile error checking code
https://github.com/dnmfarrell/go-errval
Last synced: 9 months ago
JSON representation
Provides a generic error catching interface to reduce boilerplate and fragile error checking code
- Host: GitHub
- URL: https://github.com/dnmfarrell/go-errval
- Owner: dnmfarrell
- Created: 2021-12-03T20:57:04.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-12-06T14:31:55.000Z (over 4 years ago)
- Last Synced: 2025-03-06T05:43:48.276Z (over 1 year ago)
- Language: Go
- Size: 1.95 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
errval
------
errval is a go package that uses generics to return a single value which contains either an error or a value. The `Catch` method is used to handle the error or return the value:
if ok, val := foo().Catch(logger); ok {
fmt.Println(val)
// Output: bar
}
This requires callers to explictly handle potential errors. However whilst experimenting with errval I found it didn't help shorten or de-obfuscate code with sequential calls that must return the first error found ([example](https://go.googlesource.com/proposal/+/master/design/go2draft-error-handling-overview.md)).
The root problem of golang's mundane error handling is its strict differentiation between expressions and statements: since only statements control program execution, do not compose (and `goto` is neutured), it requires new statement types to be added to the language to provide try/catch style exception handling.
The current [Go2 proposal](https://go.googlesource.com/proposal/+/master/design/go2draft-error-handling-overview.md) describes a check/handle mechanism which is similar to try/catch.