Option[T] for values that may be absent.
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::option::Option
as closely as Go generics allow. A few Go-only helpers exist where Rust has no
equivalent: Get, FromOK, FromPtr, Ptr, and OkOr.
Requires Go 1.27 or later.
go get github.com/wlrgo/option/v2package main
import (
"fmt"
"strconv"
"github.com/wlrgo/option/v2"
)
func main() {
opt := option.FromOK(strconv.Atoi("2"))
if v, ok := opt.Get(); ok {
fmt.Println(v)
}
fmt.Println(opt.Map(func(n int) int { return n * 10 }).UnwrapOr(-1))
}The zero value is None. Some and None construct an option; FromOK and
FromPtr convert from (T, error) and *T. Combinators such as Map, And,
and Or are methods. Compare, Flatten, and Collect remain package-level
functions.
| Group | Highlights |
|---|---|
| Query | IsSome, IsNone, IsSomeAnd, IsNoneOr |
| Extract | Unwrap, Expect, UnwrapOr, Get |
| Combine | And, AndThen, Or, OrElse, Xor |
| Transform | Map, Filter, Flatten, ZipWith, Reduce |
| Compare | Compare, Equal (None < Some) |
| Modify | Insert, Take, TakeIf, Replace |
| Iterate | Seq, Collect |
| Convert | FromOK, FromPtr, Ptr, OkOr |
See pkg.go.dev/github.com/wlrgo/option/v2 for the full API and package contract.