Documentation
¶
Index ¶
- func GetOf[T any](op ...Option[T]) T
- func IsNone(nn ...Noneable) (ok bool)
- func IsSome(ss ...Someable) (ok bool)
- func IsZero(zz ...Zeroable) (ok bool)
- func PickOf[T any](op ...Option[T]) []T
- type Bool
- type Complex64
- type Complex128
- type Duration
- type Float32
- type Float64
- type Int
- type Int8
- type Int16
- type Int32
- type Int64
- type Noneable
- type Option
- func (o Option[T]) Get() T
- func (o Option[T]) GetOr(value T) T
- func (o Option[T]) GetOrFunc(getter func() T) T
- func (o Option[T]) GetOrNil() *T
- func (o Option[T]) GetOrZero() T
- func (o Option[T]) GoString() string
- func (o Option[T]) IsNone() bool
- func (o Option[T]) IsSome() bool
- func (o Option[T]) IsZero() bool
- func (o Option[T]) MarshalJSON() (b []byte, err error)
- func (o Option[T]) String() string
- func (o *Option[T]) UnmarshalJSON(b []byte) (err error)
- type Someable
- type String
- type Time
- type Uint
- type Uint8
- type Uint16
- type Uint32
- type Uint64
- type Zeroable
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetOf ¶
GetOf returns value of first some value. If there are no some value, then return zero value.
func IsNone ¶
func IsNone(nn ...Noneable) (ok bool)
IsNone returns a true if all passed values is none.
func IsSome ¶
func IsSome(ss ...Someable) (ok bool)
IsSome returns a true if all passed values is some.
Types ¶
type Complex128 ¶
type Complex128 = Option[complex128]
type Option ¶
type Option[T any] struct { // contains filtered or unexported fields }
Example (States) ¶
var value option.Option[string]
fmt.Println("value is zero, same as undefined:", value.IsZero())
value = option.None[string]()
fmt.Println("value is none, defined and same as null:", value.IsNone())
value = option.Some[string]("hello world")
fmt.Println("value is some, defined and not null:", value.IsSome())
Output: value is zero, same as undefined: true value is none, defined and same as null: true value is some, defined and not null: true
func Maybe ¶
Maybe returns some if the value is non zero and nil.
Example: log.Println(Maybe(0)) // None[int]() log.Println(Maybe(1)) // Some[int](1) log.Println(Maybe((*string)(nil))) // None[*string]() ptr := "foo" log.Println(Maybe(&ptr)) // Some[*string](foo)
func SomeOf ¶
SomeOf returns first some option. If there are no some options, then returns none value.
func (Option[T]) Get ¶
func (o Option[T]) Get() T
Get returns a value if it some, in other case panics.
func (Option[T]) GetOr ¶
func (o Option[T]) GetOr(value T) T
GetOr returns the value if the option is none.
Example ¶
fmt.Println("none", option.None[int]().GetOr(1))
fmt.Println("some", option.Some(2).GetOr(1))
fmt.Println("zero", option.Option[int]{}.GetOr(3))
Output: none 1 some 2 zero 3
func (Option[T]) GetOrFunc ¶
func (o Option[T]) GetOrFunc(getter func() T) T
GetOrFunc retunrs value from getter if the option is none
Example ¶
fmt.Println("none", option.None[int]().GetOrFunc(func() int { return 1 }))
fmt.Println("some", option.Some(2).GetOrFunc(func() int { return 1 }))
fmt.Println("zero", option.Option[int]{}.GetOrFunc(func() int { return 3 }))
Output: none 1 some 2 zero 3
func (Option[T]) GetOrNil ¶
func (o Option[T]) GetOrNil() *T
GetOrNil returns the nil value if the option is none. Pointer is refers to a copy of the origin value, so that means any changes to the pointer don't affect the value of the option.
Example ¶
fmt.Println("none", option.None[int]().GetOrNil())
fmt.Printf("some %v\n", *option.Some("1").GetOrNil())
fmt.Println("zero", option.Option[int]{}.GetOrNil())
Output: none <nil> some 1 zero <nil>
func (Option[T]) GetOrZero ¶
func (o Option[T]) GetOrZero() T
GetOrZero returns the zero value if the option is none.
Example ¶
fmt.Println("none", option.None[int]().GetOrZero())
fmt.Println("some", option.Some(1).GetOrZero())
fmt.Println("zero", option.Option[int]{}.GetOrZero())
Output: none 0 some 1 zero 0
func (Option[T]) MarshalJSON ¶
MarshalJSON is a implementation of the json.Marshaler.
func (*Option[T]) UnmarshalJSON ¶
UnmarshalJSON is a implementation of the json.Unmarshaler.