opt

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 3 Imported by: 0

README

opt Go Test

a way to encapsulate possibly absent values in Go.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

func Equal[T comparable](a, b Option[T]) bool

Equal will compare the value in two options and check if their equal. If both are none, that is interpretted as "equal."

Types

type Option

type Option[T any] struct {
	// contains filtered or unexported fields
}

Option represents an optional value. Every Option has either has something or has none. If None() reports true, then calls to Unwrap() will panic. To prevent this, Option[T] should be treated like a pointer and Unwrap() like a dereference.

UnwrapOr() is a handy function to reduce the verbosity of checking an Option[T]'s state. UnwrapOr() will return the contained value if present, OR it will return the value provided.

Option[T] is immutable once created.

The zero-value of Option[T] is safe and will just report None() => true

Inspired by Rust's Option<T>: https://doc.rust-lang.org/std/option/index.html

func Coalesce

func Coalesce[T any](os ...Option[T]) Option[T]

Coalesce will take 0 or more Option and will return the first one that is Some value.

func FromMaybe

func FromMaybe[T any](v T, ok bool) Option[T]

FromMaybe will take a tuple of a value and a bool representing the value's status and convert it to an Option. If true, a Some[T] is returned, otherwise None[T] is returned.

func FromPointer

func FromPointer[T any](v *T) Option[T]

FromPointer will take in a pointer to a value and dereference it if it is not nil and return a Some[T](), if it is nil it will return None[T]().

func Join

func Join[A, B, R any](a Option[A], b Option[B], joinfn func(A, B) R) Option[R]

Join allows for two Options to be used to create a new value if they are both present. If either is not present, then a None[R] will be returned.

func Map

func Map[I, O any](in Option[I], mapfn func(I) Option[O]) Option[O]

Map allows a function to be run on the present value of an option if it is actually present and then optionally return something else from that value.

func None

func None[T any]() Option[T]

None will return an Option[T] that has no value

func Some

func Some[T any](v T) Option[T]

Some will return an Option[T] that contains the given value

func (Option[T]) MarshalJSON

func (o Option[T]) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler

func (Option[T]) MaybeUnwrap

func (o Option[T]) MaybeUnwrap() (T, bool)

MaybeUnwrap allows the underlying value to be retrieved in a more idiomatic way by returning a tuple of the possible underlying value and a bool that will be `true` if the value was present. The returned value will be the zeroed value of T if it is not present.

func (Option[T]) None

func (o Option[T]) None() bool

None is just the opposite of Some(). True means Unwrap() panicks. False means Unwrap() will not panic.

func (Option[T]) Some

func (o Option[T]) Some() bool

Some reports whether there is a value contained or not. A returned value of `true` means there is a value and it can be retrieved with Unwrap() without it panicking. A returned value of `false` means there is no value and any call to Unwrap() will cause a panic.

func (*Option[T]) UnmarshalJSON

func (o *Option[T]) UnmarshalJSON(data []byte) error

UnmarshalJSON implements json.Unmarshaler

func (Option[T]) Unwrap

func (o Option[T]) Unwrap() T

Unwrap retrieves the underlying value if there is one. Unwrap WILL PANIC if there is no value.

func (Option[T]) UnwrapOr

func (o Option[T]) UnwrapOr(v T) T

UnwrapOr is a safer version of Unwrap() that will return the provided fallback value if the Option[T] does not contain a value.

func (Option[T]) UnwrapOrZero

func (o Option[T]) UnwrapOrZero() T

UnwrapOrZero allows the either the underlying value or a zeroed value of T to be returned. If the value is present it will be returned, otherwise a zero value will be returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL