luminyra

fp overview

Model optional values, fallible results, and two possible value types with Option, Result, and Either.

When reading configuration, parsing input, or transforming data in stages, use Option for a value that may be absent, Result for an operation that may fail, and Either for one of two possible values. Methods return a new container, and each step in a chain can change the value type.

Installation

You need Go 1.27 or later. Run the installation command in your Go module.

go get github.com/luminyra/fp

Choose a container

Use caseTypeCommon entry points
Look up a value that may be absentOptionWrap the lookup with OptionOf and supply a fallback with Option.Or.
Chain operations that may return errorsResultPass the value and error to ResultOf and continue with Result.AndThen.
Process two types of values in one flowEitherUse Left and Right to hold each type and Either.Fold to produce a result.

Go standard library integration

Public APIs can keep returning Go's usual value and presence flag or value and error. For chained operations, ResultOf accepts (value, error), Option.All works with standard-library iterators, and generic methods express type changes directly on the receiver.

Performance

A Result chain takes about 24 ns, on par with the equivalent if err != nil checks. Both allocations occur inside strconv. An Option chain takes about 6 ns and allocates no memory.

Follow the quickstart or learn how to handle missing values in the Option guide.

References

On this page