A lightweight go-friendly error monad.
Use this if you hate rewriting / copy pasting basic error handling patterns.
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()