Help us shape the future of Go! Take the 2026 Go Developer Survey

option

package
v1.58.7 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: MPL-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CmpOpts added in v1.33.0

func CmpOpts() []cmp.Option

CmpOpts returns the options to use to compare options by checking the unexported fields. For testing purposes.

func Contains

func Contains[T comparable](option Option[T], matches T) bool

Contains returns true if the option is present and matches the given value

func Equal added in v1.52.4

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

Equal returns true if both Options are equal.

func Fold

func Fold[T, R any](option Option[T], defaultValue R, f func(T) R) R

Fold returns the result of f applied to the value if present, otherwise returns the defaultValue

func FoldLeft

func FoldLeft[T, R any](option Option[T], zero R, f func(accum R, value T) R) R

FoldLeft applies the binary operator f to the value if present, otherwise returns the zero value

func ToNullBool added in v1.52.3

func ToNullBool(o Option[bool]) sql.NullBool

func ToNullString added in v1.52.3

func ToNullString(o Option[string]) sql.NullString

func ToNullTime added in v1.52.3

func ToNullTime(o Option[time.Time]) sql.NullTime

Types

type Option

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

Option is a type that represents a value that may or may not be present

It is different a normal value as it can distinguish between a zero value and a missing value even on pointer types

func AsOptional

func AsOptional[T comparable](v T) Option[T]

AsOptional returns an Option where a zero value T is considered None and any other value is considered Some

i.e.

AsOptional(nil) == None()
AsOptional(0) == None()
AsOptional(false) == None()
AsOptional("") == None()
AsOptional(&MyStruct{}) == Some(&MyStruct{})
AsOptional(1) == Some(1)
AsOptional(true) == Some(true)

func CommaOk

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

CommaOk is a helper function to convert a comma ok idiom into an Option. If ok is true it returns Some(v), otherwise it returns None.

func FlatMap

func FlatMap[T, R any](option Option[T], f func(T) Option[R]) Option[R]

FlatMap returns an Option with the value mapped by the given function if present, otherwise returns None

func FromErr added in v1.52.4

func FromErr(err error) Option[string]

FromErr returns an Option[string] where a nil error is considered None and any other value is considered Some, with the error message as the value.

func FromPointer added in v1.21.1

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

FromPointer returns an Option where a nil pointer is considered None and any other value is considered Some, with the value dereferenced.

func Map

func Map[T, R any](option Option[T], f func(T) R) Option[R]

Map returns an Option with the value mapped by the given function if present, otherwise returns None

func None

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

None returns an Option with no value set

func Some

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

Some returns an Option with the given value and present set to true

This means Some(nil) is a valid present Option and Some(nil) != None()

func (Option[T]) Contains

func (o Option[T]) Contains(predicate func(v T) bool) bool

Contains returns true if the Option is present and the given predicate returns true on the value otherwise returns false

func (Option[T]) Empty

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

Empty returns true if the Option has no value set

func (Option[T]) ForAll

func (o Option[T]) ForAll(f func(v T))

ForAll calls the given function with the value if present

func (Option[T]) ForEach

func (o Option[T]) ForEach(predicate func(v T) bool) bool

ForEach returns true if the Option is empty or the given predicate returns true on the value

func (Option[T]) Get

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

Get gets the option value and returns ok==true if present.

func (Option[T]) GetOrElse

func (o Option[T]) GetOrElse(alternative T) T

GetOrElse returns the value if present, otherwise returns the alternative value

func (Option[T]) GetOrElseF

func (o Option[T]) GetOrElseF(alternative func() T) T

GetOrElseF returns the value if present, otherwise returns the alternative value

func (Option[T]) MarshalJSON added in v1.33.0

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

func (Option[T]) MustGet

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

MustGet returns the value if present, otherwise panics

func (Option[T]) OrElse

func (o Option[T]) OrElse(alternative T) Option[T]

OrElse returns an Option with the value if present, otherwise returns the alternative value

func (Option[T]) Present

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

Present returns true if the Option has a value set

func (Option[T]) PtrOrNil added in v1.19.0

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

PtrOrNil returns the value as a pointer, if present, or nil otherwise.

func (Option[T]) String

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

func (*Option[T]) UnmarshalJSON added in v1.33.0

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

Jump to

Keyboard shortcuts

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