Categorygithub.com/neocotic/go-optional
modulepackage
0.1.2
Repository: https://github.com/neocotic/go-optional.git
Documentation: pkg.go.dev

# README

go-optional

Go Reference Build Status Release License

Easy-to-use generic optional values for Go (golang).

Inspired by Java's Optional class, it enables the ability to differentiate a value that has its zero value due to not being set from having a zero value that was explicitly set, drastically reducing the need for pointers in a lot of use cases.

Installation

Install using go install:

go install github.com/neocotic/go-optional

Then import the package into your own code:

import "github.com/neocotic/go-optional"

Documentation

Documentation is available on pkg.go.dev. It contains an overview and reference.

Example

Basic usage:

opt := optional.Of(0)
opt.IsPresent()  // true
opt.Get()        // 0, true

opt := optional.Of(123)
opt.IsPresent()  // true
opt.Get()        // 123, true

var external []int
opt := optional.OfNillable(external)
opt.IsPresent()      // false
opt.Get()            // nil, false
opt.OrElse([]int{})  // []int{}

opt := optional.Empty[string]()
opt.IsPresent()  // false
opt.Get()        // "", false

Optionals are also intended to be used as struct fields and support JSON, XML, and YAML marshaling and unmarshaling out-of-the-box. That said; the very nature of optionals leans more towards unmarshaling.

type Example struct {
    Number Optional[int]    `json:"number"`
    Text   Optional[string] `json:"text"`
}

var example Example
err := json.Unmarshal([]byte(`{"text": "abc"}`, &example)
if err != nil {
    panic(err)
}
example.Number.Get()  // 0, false
example.Text.Get()    // "abc", true

There's a load of other functions and methods on Optional (along with its own sort sub-package) to explore, all with documented examples.

Issues

If you have any problems or would like to see changes currently in development you can do so here.

Contributors

If you want to contribute, you're a legend! Information on how you can do so can be found in CONTRIBUTING.md. We want your suggestions and pull requests!

A list of contributors can be found in AUTHORS.md.

License

Copyright © 2024 neocotic

See LICENSE.md for more information on our MIT license.

# Packages

Package sort provides basic support for sorting slices of optional.Optional.

# Functions

Compare returns the following: - -1 if x has not value present and y does; or if both have a value present and the value of x is less than that of y - 0 if neither x nor y have a value present; or if both have a value present that are equal - +1 if x has a value present and y does not; or if both have a value present and the value of x is greater than that of y For floating-point types, a NaN is considered less than any non-NaN, a NaN is considered equal to a NaN, and -0.0 is equal to 0.0.
Empty returns an Optional with no value.
Equal returns whether a given Optional is equal to another.
Find returns the first given Optional that has a value present, otherwise an empty Optional.
FlatMap calls the given function and returns the Optional returned by it if the Optional provided has a value present, otherwise an empty Optional is returned.
GetAny returns a slice containing only the values of any given Optional that has a value present, where possible.
Map returns an Optional whose value is mapped from the Optional provided using the given function, if present, otherwise an empty Optional.
MustFind returns the value of the first given Optional that has a value present, otherwise panics.
Of returns an Optional with the given value present.
OfNillable returns an Optional with the given value present only if value is nil.
OfPointer returns an Optional with the given value present as a pointer.
OfZeroable returns an Optional with the given value present only if value does not equal the zero value for T.
RequireAny returns a slice containing only the values of any given Optional that has a value present, panicking only if no Optional could be found with a value present.
TryFlatMap calls the given function and returns the Optional returned by it if the Optional provided has a value present, otherwise an empty Optional is returned.
TryMap returns an Optional whose value is mapped from the Optional provided using the given function, if present, otherwise an empty Optional.

# Structs

Optional contains an immutable value as well as an indication whether it was explicitly set.