Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/leavez/go-either
An Either type for golang with generics
https://github.com/leavez/go-either
Last synced: about 1 month ago
JSON representation
An Either type for golang with generics
- Host: GitHub
- URL: https://github.com/leavez/go-either
- Owner: leavez
- License: mit
- Created: 2022-04-08T12:51:18.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-04-08T13:52:58.000Z (over 2 years ago)
- Last Synced: 2024-06-20T02:04:33.966Z (5 months ago)
- Language: Go
- Size: 2.93 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: LICENSE
Awesome Lists containing this project
README
# go-either
An Either type for golang with generics
```golang
e := either.NewLeft[int, string](1) // either.Type[int, string]l, r, isLeft := e.Unwrap()
if isLeft { // true
print(l) // l is 1
} else {
print(r)
}e.EitherDo(func(l int) {
print(l)
}, func(r string) {
print(r)
})either.MapLeft(e, func(left int) *int {
return &left
}) // either.Type[*int, string]// either.MapRight
// either.Map
```and the Result type, based on `either.Type`
```golang
r := result.New(123) // result.Type[int]
r.IsError() // falseif w, err := r.Unwrap(); err != nil {
print(w) // 123
}result.Map(r, func(t int) string {
return "123"
}) // result.Type[string]r.MapError(func(err error) error {
return fmt.Errorf("faile: %w", err)
}) // equals r, as MapError will only execute when r is error result
```# License
MIT