optional

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: MIT Imports: 6 Imported by: 10

Documentation

Overview

Package optional provides an Option type that can contain a value or nothing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

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

An Option type is a type that can contain a value or nothing.

func From added in v0.14.0

func From[T any](value T, ok bool) Option[T]

From returns an Option that contains a value if ok is true, otherwise None.

func Nil

func Nil[T any](ptr T) Option[T]

Nil returns an Option that is invalid if the value is nil, otherwise the value.

If the type is not nillable (slice, map, chan, ptr, interface) this will panic.

Unfortunately there's no way to do this without reflection.

func None

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

None returns an Option that contains nothing.

func Ptr

func Ptr[T any](ptr *T) Option[T]

Ptr returns an Option that is invalid if the pointer is nil, otherwise the dereferenced pointer.

func Some

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

Some returns an Option that contains a value.

func Zero

func Zero[T any](value T) Option[T]

Zero returns an Option that is invalid if the value is the zero value, otherwise the value.

func (Option[T]) Default

func (o Option[T]) Default(value T) T

Default returns the Option value if it is present, otherwise it returns the value passed.

func (Option[T]) Get

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

Get returns the value and a boolean indicating if the Option contains a value.

func (Option[T]) GoString

func (o Option[T]) GoString() string

func (Option[T]) MarshalJSON

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

func (Option[T]) MustGet

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

MustGet returns the value. It panics if the Option contains nothing.

func (Option[T]) Ok

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

Ok returns true if the Option contains a value.

func (Option[T]) Ptr

func (o Option[T]) Ptr() *T

Ptr returns a pointer to the value if the Option contains a value, otherwise nil.

func (*Option[T]) Scan

func (o *Option[T]) Scan(src any) error

func (Option[T]) String

func (o Option[T]) String() string

func (*Option[T]) UnmarshalJSON

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

func (Option[T]) Value

func (o Option[T]) Value() (driver.Value, error)

Jump to

Keyboard shortcuts

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