Result[T, E] for success or failure.
This is a wlrgo package. wlrgo ports Rust standard-library types to Go and intentionally copies the Rust API instead of reshaping it into an idiomatic Go design. Later packages in the org follow the same rule.
Names, combinators, and eager vs lazy evaluation follow
std::result::Result
as closely as Go generics allow. A few Go-only helpers exist where Rust has no
equivalent: From, Get, GetErr, and Unpack.
Requires Go 1.27 or later.
go get github.com/wlrgo/result/v2package main
import (
"fmt"
"strconv"
"github.com/wlrgo/result/v2"
)
func main() {
res := result.From(strconv.Atoi("2"))
if v, ok := res.Get(); ok {
fmt.Println(v)
}
fmt.Println(res.Map(func(n int) int { return n * 10 }).UnwrapOr(-1))
}The zero value is Err. Ok and Err construct a result; From converts
from (T, error). Combinators such as Map, And, and Or are methods.
Compare, Flatten, and Collect remain package-level functions.
| Group | Highlights |
|---|---|
| Query | IsOk, IsErr, IsOkAnd, IsErrAnd |
| Extract | Unwrap, Expect, UnwrapOr, Get |
| Combine | And, AndThen, Or, OrElse |
| Transform | Map, MapErr, Flatten, Inspect |
| Compare | Compare, Equal (Ok < Err) |
| Iterate | Seq, Collect |
| Convert | From, Get, GetErr, Unpack |
See pkg.go.dev/github.com/wlrgo/result/v2 for the full API and package contract.