An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

          

[![Docs](https://img.shields.io/badge/godoc-docs-blue.svg?label=&logo=go)](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()

```