https://github.com/go-libfp/try
A go-friendly error monad.
https://github.com/go-libfp/try
error-handling functional-programming go golang monad productivity
Last synced: about 2 months ago
JSON representation
A go-friendly error monad.
- Host: GitHub
- URL: https://github.com/go-libfp/try
- Owner: go-libfp
- License: mit
- Created: 2023-02-22T23:01:29.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-04-17T15:06:29.000Z (almost 3 years ago)
- Last Synced: 2024-06-20T12:41:23.297Z (almost 2 years ago)
- Topics: error-handling, functional-programming, go, golang, monad, productivity
- Language: Go
- Homepage:
- Size: 11.7 KB
- Stars: 4
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://pkg.go.dev/github.com/go-libfp/try)
# try
A lightweight go-friendly error monad.
Use this if you hate rewriting / copy pasting basic error handling patterns.
## Usage
```go
import (
"github.com/go-libfp/try"
"encoding/json"
"fmt"
"log"
)
type Foo struct { Bar string}
func jsonDecode[T any](data []byte) (T, error) {
var out T
err := json.Unmarshal(data, &out)
return out, err
}
t := try.WrapErr( json.Marshal(Foo{"hello"}) )
// bind ET will run a function that returns err tuple
t1 := try.BindET(t, jsonDecode[Foo])
t2 := try.Map(t1, func(x Foo) string {
return x.Bar
} )
t2.OnSuccess(func(x string) {
fmt.Println(x)
} )
t2.OnErr(func(e error) {
log.Println(e)
})
_x, _err := t2.Get()
```