Skip to content

go-composites/result

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-composites/result

result

ci

The result composite of go-composites — the value every fallible operation returns. A Result pairs a payload with an Error, so success and failure travel together as one value: no (value, ok), no panics, no bare nil. A fresh Result.New() defaults to a Null payload and a NullError, so it represents a successful, empty outcome.

Install

go get github.com/go-composites/result

API

symbol returns notes
New(opts...) Result.Interface payload defaults to Null, error to NullError
WithPayload(v) Option functional option setting the payload
WithError(e) Option functional option setting the Error.Interface
Payload() interface{} the carried payload
HasError() bool !Error.IsNull()true only when a real error is attached
Error() Error.Interface the carried error (a NullError when there is none)

Usage

package main

import (
	"fmt"

	Result "github.com/go-composites/result/src"
	Error "github.com/go-composites/error/src"
)

func main() {
	ok := Result.New(Result.WithPayload(42))
	fmt.Println(ok.HasError(), ok.Payload()) // false 42

	bad := Result.New(Result.WithError(Error.New("key not found")))
	fmt.Println(bad.HasError(), bad.Error().Message()) // true key not found

	empty := Result.New()
	fmt.Println(empty.HasError()) // false  (Null payload + NullError)
}

License

BSD-3-Clause © the go-composites/result authors.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages