Result is a go structure to represent a Result type. It can represent either a success or a failure.
Using Result, we can build APIs like:
latestCommitMessage := openRepository(url)
.flatMap(func (repo interface{}) { return repo.headReference() })
.flatMap(func (ref interface{}) { return ref.commit() })
.flatMap(func (commit interface{}) { return commit.message() })
fmt.Printf("%+v\n", latestCommitMessage)
Instead of like:
repository, err := openRepository(url)
if err != nil {
return
}
reference, err := repository.headReference()
if err != nil {
return
}
commit, err := reference.commit()
if err != nil {
return
}
message, err := commit.message()
if err != nil {
return
}
fmt.Printf("%s\n", message)
result := NewSuccess(5)
result := NewFailure(err)
Constructing a result from the "result" of the Open
standard os function.
func Open(name string) (file *File, err error)
result := NewResult(Open("HelloWorld.txt"))
This example will transform a the original result and return a result with a value that has doubled, when the original result is a failure, the failure will be returned.
result := result.FlatMap(func(value interface{}) Result {
return NewSuccess(value.(int) * 2)
})
value, err := result.Dematerialize()
Result in Go was heavily inspired by Result in Swift and parts of the README we're inspired by Gift, a Swift git binding.
Result is licensed under the BSD license. See LICENSE for more info.