Skip to content

go-libfp/try

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Docs

try

A lightweight go-friendly error monad.

Use this if you hate rewriting / copy pasting basic error handling patterns.

Usage

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()