nilo

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2025 License: MIT Imports: 3 Imported by: 13

README

nilo

Rusty Go Option library for handling nil values, some errors and marshaling

Caveats

  • This library requires Go 1.23+

Intallation

go get -u github.com/javiorfo/nilo@latest

Example

Examples here
package main

import (
  "errors"
  "fmt"

  "github.com/javiorfo/nilo"
)

type User struct {
  Name string
}

// Implements nilo.Default interface
func (u User) Default() User {
  return User{"Default Implementation"}
}

func main() {
  var optUser = nilo.None[User]()

  fmt.Printf("User or default: %+v\n", optUser.UnwrapOrDefault())
  fmt.Printf("User or: %+v\n", optUser.UnwrapOr(*new(User)))
  fmt.Printf("User or else: %+v\n", optUser.UnwrapOrElse(func() User { return User{"else"} }))
  fmt.Printf("Map or: %+v\n", optUser.MapOr(User{"or"}, func(u User) User {
	  u.Name = "something"
	  return u
  }))

  nilo.FromResult(getUser(true)).OkAndResult(getUser2).Inspect(print)

  _, err := test(false).OkOr(errors.New("some err"))
  fmt.Println("Error:", err.Error())

  fmt.Println(test(true).MapToString(func(v string) string { return v + ", World" }).UnwrapOr("another string"))
}

func test(b bool) nilo.Option[string] {
  if b {
	  return nilo.Some("Hello")
  }
  return nilo.None[string]()
}

func print[T any](v *T) {
  fmt.Printf("Value: %#v\n", *v)
}

func getUser(b bool) (*int, error) {
  if b {
	  i := 1
	  return &i, nil
  }
  return nil, errors.New("error")
}

func getUser2(v *int) (*int, error) {
  if *v == 0 {
	  return nil, errors.New("error")
  }
  i := *v + 2
  return &i, nil
}
JSON marshal
package main

import (
  "encoding/json"
  "fmt"

  "github.com/javiorfo/nilo"
)

type User struct {
  Name string              `json:"name"`
  Code nilo.Option[string] `json:"code"`
}

func main() {
  var unmarshalUser User
  user := User{
	  Name: "Name",
	  Code: nilo.None[string](),
  }

  // Marshal
  jsonData, err := json.MarshalIndent(user, "", "  ")
  if err != nil {
	  fmt.Println("Error marshaling to JSON:", err)
	  return
  }

  fmt.Println(string(jsonData))
	
  // Unmarshal
  err = json.Unmarshal(jsonData, &unmarshalUser)
  if err != nil {
	  fmt.Println("Error unmarshaling:", err)
	  return
  }
	
  fmt.Printf("Unmarshaled User: %+v\n", unmarshalUser)
  if unmarshalUser.Code.IsNone() {
	  fmt.Printf("Code is None: %s\n", unmarshalUser.Code)
  }

  // Put Some in Code
  user.Code.Replace("some code")

  // Marshal
  jsonData, err = json.MarshalIndent(user, "", "  ")
  if err != nil {
	  fmt.Println("Error marshaling to JSON:", err)
	  return
  }

  fmt.Println(string(jsonData))

  // Unmarshal
  err = json.Unmarshal(jsonData, &unmarshalUser)
  if err != nil {
	  fmt.Println("Error unmarshaling:", err)
	  return
  }
	
  fmt.Printf("Unmarshaled User: %+v\n", unmarshalUser)
  if unmarshalUser.Code.IsSome() {
	  fmt.Printf("Code is Some with value: %s\n", unmarshalUser.Code.Unwrap())
  }
}
All methods
func (o Option[T]) Unwrap() T
func (o Option[T]) UnwrapOr(other T) T
func (o Option[T]) UnwrapUnchecked() *T
func (o Option[T]) UnwrapOrDefault() T
func (o Option[T]) UnwrapOrElse(supplier func() T) T
func (o Option[T]) OkOr(err error) (*T, error)
func (o Option[T]) OkOrElse(err func() error) (*T, error)
func (o Option[T]) OrElse(supplier func() Option[T]) Option[T]
func (o Option[T]) Filter(filter func(T) bool) Option[T]
func (o Option[T]) IsNone() bool
func (o Option[T]) IsSome() bool
func (o Option[T]) Inspect(consumer func(T)) Option[T]
func (o Option[T]) InspectOrElse(consumer func(T), or func())
func None[T any]() Option[T]
func Some[T any](value T) Option[T]
func SomePtr[T any](value *T) Option[T]
func (o Option[T]) AndThen(fn func(T) Option[T]) Option[T]
func (o Option[T]) And(other Option[T]) Option[T]
func (o Option[T]) Or(other Option[T]) Option[T]
func (o Option[T]) Xor(other Option[T]) Option[T]
func (o Option[T]) IsSomeAnd(predicate func(T) bool) bool
func (o Option[T]) IsNoneOr(predicate func(T) bool) bool
func (o Option[T]) Expect(msg string) T
func (o *Option[T]) Take() Option[T]
func (o *Option[T]) TakeIf(predicate func(T) bool) Option[T]
func (o *Option[T]) Replace(value T) Option[T]
func (o *Option[T]) Insert(value T)
func (o *Option[T]) GetOrInsert(value T) T
func (o *Option[T]) GetOrInsertWith(supplier func() T)
func (o *Option[T]) GetOrInsertDefault() T
func (o Option[T]) Map(mapper func(T) T) Option[T]
func (o Option[T]) MapToAny(mapper func(T) any) Option[any]
func (o Option[T]) MapToString(mapper func(T) string) Option[string]
func (o Option[T]) MapToInt(mapper func(T) int) Option[int]
func (o Option[T]) MapToBool(mapper func(T) bool) Option[bool]
func (o Option[T]) MapOr(def T, mapper func(T) T) T
func (o Option[T]) MapOrAny(def any, mapper func(T) any) any
func (o Option[T]) MapOrString(def string, mapper func(T) string) string
func (o Option[T]) MapOrInt(def int, mapper func(T) int) int
func (o Option[T]) MapOrBool(def bool, mapper func(T) bool) bool
func (o Option[T]) MapOrElse(supplier func() T, mapper func(T) T) T
func (o Option[T]) MapOrElseAny(supplier func() any, mapper func(T) any) any
func (o Option[T]) MapOrElseString(supplier func() string, mapper func(T) string) string
func (o Option[T]) MapOrElseInt(supplier func() int, mapper func(T) int) int
func (o Option[T]) MapOrElseBool(supplier func() bool, mapper func(T) bool) bool
func (o Option[T]) MapOrDefault(mapper func(T) T) T
func (o Option[T]) OkAndResult(apply func(T) (T, error)) Option[T]
func FromResult[T any](value T, err error) Option[T]
func (o Option[T]) MarshalJSON() ([]byte, error)
func (o *Option[T]) UnmarshalJSON(data []byte) error
func (o Option[T]) String() string

Donate
  • Bitcoin (QR) 1GqdJ63RDPE4eJKujHi166FAyigvHu5R7v
  • Paypal

Documentation

Overview

Package nilo provides a generic Option type that can be used to represent a value that may or may not be present.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Default added in v1.1.0

type Default[T any] interface {
	Default() T
}

Default is an interface that can be implemented by types that wish to provide a custom 'default' value. This is used by methods on `Option` such as `UnwrapOrDefault` or `GetOrInsertDefault` to create an instance of the type when a `None` `Option` is encountered.

Types that implement this interface can define their own logic for what constitutes a 'default' value, instead of relying on the Go language's zero value.

type Option added in v1.1.0

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

Option is a generic type that represents an option value. An `Option` can either be `Some`, containing a value of type `T`, or `None`, indicating the absence of a value.

func FromResult added in v1.1.0

func FromResult[T any](value T, err error) Option[T]

FromResult creates an `Option` from a Go function's return values.

It returns a `Some` `Option` containing `value` if `err` is `nil`. If `err` is not `nil`, it returns a `None` `Option`. This is a convenient way to bridge error-handling patterns in Go with the `Option` type.

Parameters:

  • value: The value to wrap in a `Some` `Option` if there is no error.
  • err: The error returned from a function.

func None added in v1.1.0

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

None returns an empty Option.

func Some added in v1.1.0

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

Some creates an Option containing the provided value.

func SomePtr added in v1.1.0

func SomePtr[T any](value *T) Option[T]

SomePtr creates an Option from a pointer to a value. If the pointer is nil, it returns an empty Option.

func (Option[T]) And added in v1.1.0

func (o Option[T]) And(other Option[T]) Option[T]

And combines two `Option`s.

If both the current `Option` and the `other` `Option` are `Some`, it returns the `other` `Option`. In all other cases (if either `Option` is `None`), it returns `None`. This can be used to perform a logical "AND" operation on `Option` values.

Parameters:

  • other: The other `Option` to combine with the current one.

func (Option[T]) AndThen added in v1.1.0

func (o Option[T]) AndThen(fn func(T) Option[T]) Option[T]

AndThen is a chaining method that applies a function to the contained value if the `Option` is `Some`, returning the result.

If the `Option` is `Some`, the `fn` is called with the unwrapped value, and the new `Option` returned by `fn` is the result. This allows for a sequence of fallible operations. If the `Option` is `None`, this method returns `None` without calling `fn`.

Parameters:

  • fn: A function that takes the `Option`'s value and returns a new `Option`.

func (Option[T]) Expect added in v1.1.0

func (o Option[T]) Expect(msg string) T

Expect returns the contained `Some` value, but panics with a custom message if the `Option` is `None`.

This method is used when a `None` value is considered an unrecoverable error in your program, and you want to crash with a descriptive message.

Parameters:

  • msg: The message to be used in the panic if the `Option` is `None`.

func (Option[T]) Filter added in v1.1.0

func (o Option[T]) Filter(filter func(T) bool) Option[T]

Filter calls a predicate function on the contained value if the `Option` is `Some`.

If the `Option` is `Some` and the predicate returns `true`, it returns a `Some` `Option` with the original value. Otherwise, it returns a `None` `Option`. This is useful for removing values that do not meet a certain condition.

Parameters:

  • filter: A predicate function that returns `true` or `false` based on the value.

func (*Option[T]) GetOrInsert added in v1.1.0

func (o *Option[T]) GetOrInsert(value T) T

GetOrInsert returns the contained value if the `Option` is `Some`. If the `Option` is `None`, it inserts the provided `value` into the `Option`, making it `Some`, and then returns the newly inserted value.

Parameters:

  • value: The value to insert if the `Option` is `None`.

func (*Option[T]) GetOrInsertDefault added in v1.1.0

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

GetOrInsertDefault returns the contained value if the `Option` is `Some`. If the `Option` is `None`, it inserts a default value, making it `Some`, and then returns the new value.

The default value is determined by the following:

  1. If the type `T` implements the `Default` interface, its `Default()` method is called to get the value.
  2. Otherwise, the Go language's zero value for type `T` is used.

func (*Option[T]) GetOrInsertWith added in v1.1.0

func (o *Option[T]) GetOrInsertWith(supplier func() T) T

GetOrInsertWith returns the contained value if the `Option` is `Some`. If the `Option` is `None`, it calls the `supplier` function, inserts the returned value into the `Option`, and then returns the new value.

This method is useful for providing a default value that is expensive to compute, as the `supplier` function is only called when needed.

Parameters:

  • supplier: A function that provides the value to be inserted if the `Option` is `None`.

func (*Option[T]) Insert added in v1.1.0

func (o *Option[T]) Insert(value T)

Insert replaces the contained value with a new one.

If the `Option` is currently `None`, it is changed to `Some` with the new value. If the `Option` is already `Some`, its existing value is replaced by the new one.

Parameters:

  • value: The new value to be inserted into the `Option`.

func (Option[T]) Inspect added in v1.1.0

func (o Option[T]) Inspect(consumer func(T)) Option[T]

Inspect calls a function on the contained value if the `Option` is `Some`, and then returns the original `Option`.

This is useful for debugging or logging the value without consuming the `Option`.

Parameters:

  • consumer: A function that takes the `Option`'s value.

func (Option[T]) InspectOrElse added in v1.1.0

func (o Option[T]) InspectOrElse(consumer func(T), or func())

InspectOrElse calls one of two functions depending on whether the `Option` is `Some` or `None`.

If the `Option` is `Some`, it calls the `consumer` function with the contained value. If the `Option` is `None`, it calls the `or` function. This is useful for debugging or performing side effects based on the `Option`'s state.

Parameters:

  • consumer: A function that takes the `Option`'s value, called if the `Option` is `Some`.
  • or: A function with no parameters, called if the `Option` is `None`.

func (Option[T]) IsNone added in v1.1.0

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

IsNone returns `true` if the `Option` is `None`.

func (Option[T]) IsNoneOr added in v1.1.0

func (o Option[T]) IsNoneOr(predicate func(T) bool) bool

IsNoneOr returns `true` if the `Option` is `None` or if the contained value satisfies the given `predicate`.

This acts as a logical "OR" on the state of the `Option`. If the `Option` is `None`, the predicate is not evaluated, and `true` is returned. If it is `Some`, the function returns the result of the `predicate`.

Parameters:

  • predicate: A function that takes the `Option`'s value and returns a boolean.

func (Option[T]) IsSome added in v1.1.0

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

IsSome returns `true` if the `Option` is `Some`.

func (Option[T]) IsSomeAnd added in v1.1.0

func (o Option[T]) IsSomeAnd(predicate func(T) bool) bool

IsSomeAnd returns `true` if the `Option` is `Some` and the value contained within it satisfies the given `predicate`.

If the `Option` is `None`, this method short-circuits and returns `false`. This is a convenient way to check for both presence and a condition in one call.

Parameters:

  • predicate: A function that takes the `Option`'s value and returns a boolean.

func (Option[T]) Map added in v1.1.0

func (o Option[T]) Map(mapper func(T) T) Option[T]

Map applies a function to the contained value of an Option if it is `Some` and returns a new `Option` containing the mapped value.

If the original `Option` is `None`, this method returns `None`.

Parameters:

  • mapper: The function to apply to the `Option`'s value. It takes a value of type `T` and returns a value of the same type `T`.

Returns:

  • A new `Option[T]` containing the result of the mapping, or `None` if the original `Option` was `None`.

func (Option[T]) MapOr added in v1.1.0

func (o Option[T]) MapOr(def T, mapper func(T) T) T

MapOr maps the `Option`'s value if it is `Some` and returns the result, otherwise returns a default value.

Parameters:

  • def: The default value to return if the `Option` is `None`.
  • mapper: A function to apply to the `Option`'s value if it is `Some`.

func (Option[T]) MapOrAny added in v1.2.0

func (o Option[T]) MapOrAny(def any, mapper func(T) any) any

MapOrAny maps the `Option`'s value to a string if it is `Some` and returns the result, otherwise returns a default any.

Parameters:

  • def: The default any to return if the `Option` is `None`.
  • mapper: A function to apply to the `Option`'s value to produce an any.

func (Option[T]) MapOrBool added in v1.1.0

func (o Option[T]) MapOrBool(def bool, mapper func(T) bool) bool

MapOrBool maps the `Option`'s value to a boolean if it is `Some` and returns the result, otherwise returns a default boolean.

Parameters:

  • def: The default boolean to return if the `Option` is `None`.
  • mapper: A function to apply to the `Option`'s value to produce a boolean.

func (Option[T]) MapOrDefault added in v1.1.0

func (o Option[T]) MapOrDefault(mapper func(T) T) T

MapOrDefault maps the `Option`'s value if it is `Some`, otherwise returns the zero value of the type.

Parameters:

  • mapper: A function to apply to the `Option`'s value if it is `Some`.

func (Option[T]) MapOrElse added in v1.1.0

func (o Option[T]) MapOrElse(supplier func() T, mapper func(T) T) T

MapOrElse maps the `Option`'s value if it is `Some` and returns the result, otherwise calls a supplier function to get the default value.

Parameters:

  the function signature).
- supplier: A function that provides the default value if the `Option` is `None`.
- mapper: A function to apply to the `Option`'s value if it is `Some`.

func (Option[T]) MapOrElseAny added in v1.2.0

func (o Option[T]) MapOrElseAny(supplier func() any, mapper func(T) any) any

MapOrElseAny maps the `Option`'s value to an any if it is `Some`, otherwise calls a supplier function to get the default any.

Parameters:

  the function signature).
- supplier: A function that provides the default any if the `Option` is `None`.
- mapper: A function to apply to the `Option`'s value to produce an any.

func (Option[T]) MapOrElseBool added in v1.1.0

func (o Option[T]) MapOrElseBool(supplier func() bool, mapper func(T) bool) bool

MapOrElseBool maps the `Option`'s value to a boolean if it is `Some`, otherwise calls a supplier function to get the default boolean.

Parameters:

  the function signature).
- supplier: A function that provides the default boolean if the `Option` is `None`.
- mapper: A function to apply to the `Option`'s value to produce a boolean.

func (Option[T]) MapOrElseInt added in v1.1.0

func (o Option[T]) MapOrElseInt(supplier func() int, mapper func(T) int) int

MapOrElseInt maps the `Option`'s value to an integer if it is `Some`, otherwise calls a supplier function to get the default integer.

Parameters:

  the function signature).
- supplier: A function that provides the default integer if the `Option` is `None`.
- mapper: A function to apply to the `Option`'s value to produce an integer.

func (Option[T]) MapOrElseString added in v1.1.0

func (o Option[T]) MapOrElseString(supplier func() string, mapper func(T) string) string

MapOrElseString maps the `Option`'s value to a string if it is `Some`, otherwise calls a supplier function to get the default string.

Parameters:

  the function signature).
- supplier: A function that provides the default string if the `Option` is `None`.
- mapper: A function to apply to the `Option`'s value to produce a string.

func (Option[T]) MapOrInt added in v1.1.0

func (o Option[T]) MapOrInt(def int, mapper func(T) int) int

MapOrInt maps the `Option`'s value to an integer if it is `Some` and returns the result, otherwise returns a default integer.

Parameters:

  • def: The default integer to return if the `Option` is `None`.
  • mapper: A function to apply to the `Option`'s value to produce an integer.

func (Option[T]) MapOrString added in v1.1.0

func (o Option[T]) MapOrString(def string, mapper func(T) string) string

MapOrString maps the `Option`'s value to a string if it is `Some` and returns the result, otherwise returns a default string.

Parameters:

  • def: The default string to return if the `Option` is `None`.
  • mapper: A function to apply to the `Option`'s value to produce a string.

func (Option[T]) MapToAny added in v1.2.0

func (o Option[T]) MapToAny(mapper func(T) any) Option[any]

MapToAny maps the contained value of an `Option[T]` to an any if it is `Some`, and returns a new `Option[any]` with the result.

If the original `Option` is `None`, this method returns `None[any]`.

Parameters:

  • mapper: The function to apply to the `Option`'s value. It takes a value of type `T` and returns an `any`.

Returns:

  • A new `Option[any]` containing the mapped value, or `None[any]` if the original `Option` was `None`.

func (Option[T]) MapToBool added in v1.1.0

func (o Option[T]) MapToBool(mapper func(T) bool) Option[bool]

MapToBool maps the contained value of an `Option[T]` to a boolean if it is `Some`, and returns a new `Option[bool]` with the result.

If the original `Option` is `None`, this method returns `None[bool]`.

Parameters:

  • mapper: The function to apply to the `Option`'s value. It takes a value of type `T` and returns a `bool`.

Returns:

  • A new `Option[bool]` containing the mapped value, or `None[bool]` if the original `Option` was `None`.

func (Option[T]) MapToInt added in v1.1.0

func (o Option[T]) MapToInt(mapper func(T) int) Option[int]

MapToInt maps the contained value of an `Option[T]` to an integer if it is `Some`, and returns a new `Option[int]` with the result.

If the original `Option` is `None`, this method returns `None[int]`.

Parameters:

  • mapper: The function to apply to the `Option`'s value. It takes a value of type `T` and returns an `int`.

Returns:

  • A new `Option[int]` containing the mapped value, or `None[int]` if the original `Option` was `None`.

func (Option[T]) MapToString added in v1.1.0

func (o Option[T]) MapToString(mapper func(T) string) Option[string]

MapToString maps the contained value of an `Option[T]` to a string if it is `Some`, and returns a new `Option[string]` with the result.

If the original `Option` is `None`, this method returns `None[string]`.

Parameters:

  • mapper: The function to apply to the `Option`'s value. It takes a value of type `T` and returns a `string`.

Returns:

  • A new `Option[string]` containing the mapped value, or `None[string]` if the original `Option` was `None`.

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

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

MarshalJSON implements the `json.Marshaler` interface for `Option`.

If the `Option` is `None`, it marshals to the JSON value `null`. If the `Option` is `Some`, it marshals the wrapped value to its JSON representation.

func (Option[T]) OkAndResult added in v1.1.0

func (o Option[T]) OkAndResult(apply func(T) (T, error)) Option[T]

OkAndResult applies a function that returns a value and an error to the `Option`'s contained value.

If the `Option` is `Some` and the applied function returns a `nil` error, this method returns a `Some` `Option` with the function's returned value. In all other cases (if the `Option` is `None` or the function returns a non-nil error), it returns a `None` `Option`. This is useful for chaining operations that might fail.

Parameters:

  • apply: A function that takes the `Option`'s value and returns a new value and an error.

func (Option[T]) OkOr added in v1.1.0

func (o Option[T]) OkOr(err error) (*T, error)

OkOr converts an `Option` into a Go-style `(value, error)` tuple.

If the `Option` is `Some`, it returns a pointer to the value and a `nil` error. If the `Option` is `None`, it returns a `nil` pointer and the provided `err`. This is useful for integrating `Option` with functions that return an error.

Parameters:

  • err: The error to return if the `Option` is `None`.

func (Option[T]) OkOrElse added in v1.1.0

func (o Option[T]) OkOrElse(err func() error) (*T, error)

OkOrElse converts an `Option` into a `(value, error)` tuple.

If the `Option` is `Some`, it returns a pointer to the value and a `nil` error. If the `Option` is `None`, it returns a `nil` pointer and the error returned by the `err` supplier function. This is useful when creating the error is an expensive operation, as the supplier function is only called when needed.

Parameters:

  • err: A function that returns the error to be used if the `Option` is `None`.

func (Option[T]) Or added in v1.1.0

func (o Option[T]) Or(other Option[T]) Option[T]

Or returns the `Option` if it is `Some`, otherwise it returns the `other` `Option`.

This method acts as a logical "OR" operation. If the current `Option` is `Some`, its value is preferred. If it's `None`, it falls back to the `other` `Option`.

Parameters:

  • other: The alternative `Option` to return if the current one is `None`.

func (Option[T]) OrElse added in v1.1.0

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

OrElse returns the `Option` itself if it is `Some`. If the `Option` is `None`, it calls the `supplier` function and returns the resulting `Option`.

This is useful for providing a fallback `Option` to use if the original is empty. The `supplier` function is only called if the `Option` is `None`, which can be beneficial for performance if creating the fallback is an expensive operation.

Parameters:

  • supplier: A function that provides the fallback `Option` to return if the original `Option` is `None`.

func (*Option[T]) Replace added in v1.1.0

func (o *Option[T]) Replace(value T) Option[T]

Replace replaces the contained value with a new value and returns the old `Option`.

If the `Option` is `Some`, its value is updated to `value`. The original `Some` `Option` with the old value is returned. If the `Option` is `None`, it remains `None`, and a new `Some` `Option` with the new value is returned.

Parameters:

  • value: The new value to place into the `Option`.

func (Option[T]) String added in v1.1.0

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

String implements the `fmt.Stringer` interface for `Option`.

It returns a string representation of the `Option`. For `Some` `Option`s, the format is "Some(value)". For a `None` `Option`, the format is "None".

func (*Option[T]) Take added in v1.1.0

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

Take takes the value out of the `Option`, leaving a `None` in its place.

If the `Option` is `Some`, this method returns the original `Some` `Option` and sets the receiver to `None`. If the `Option` is `None`, it remains `None`.

func (*Option[T]) TakeIf added in v1.1.0

func (o *Option[T]) TakeIf(predicate func(T) bool) Option[T]

TakeIf takes the value out of the `Option` and leaves a `None` if the contained value satisfies the given `predicate`.

If the `Option` is `Some` and the `predicate` returns `true`, it behaves identically to `Take`, returning the original `Some` `Option` and setting the receiver to `None`. Otherwise, it returns `None` and does not modify the original `Option`.

Parameters:

  • predicate: A function that tests the `Option`'s value.

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

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

UnmarshalJSON implements the `json.Unmarshaler` interface for `Option`.

If the JSON data is `null`, it unmarshals into a `None` `Option`. Otherwise, it unmarshals the data into the `Option`'s value, creating a `Some` `Option` with the unmarshaled content.

func (Option[T]) Unwrap added in v1.1.0

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

Unwrap returns the contained value of a `Some` `Option`.

Panics if the `Option` is `None`. This method should only be used when you are certain the `Option` contains a value.

func (Option[T]) UnwrapOr added in v1.1.0

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

UnwrapOr returns the contained value if the `Option` is `Some`, otherwise returns the provided default value `other`.

This is a safe alternative to `Unwrap` when you have a default value to use.

Parameters:

  • other: The default value to return if the `Option` is `None`.

func (Option[T]) UnwrapOrDefault added in v1.1.0

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

UnwrapOrDefault returns the contained value if the `Option` is `Some`. If the `Option` is `None`, it returns a default value.

The default value is determined by the following:

  1. If the type `T` implements the `Default` interface, `Default()` is called to get the default value.
  2. Otherwise, the Go language's zero value for type `T` is returned.

func (Option[T]) UnwrapOrElse added in v1.1.0

func (o Option[T]) UnwrapOrElse(supplier func() T) T

UnwrapOrElse returns the contained value if the `Option` is `Some`, otherwise it calls the provided `supplier` function to get a default value.

This is useful for providing a default value that is expensive to compute, as the supplier function is only called when needed.

Parameters:

  • supplier: A function that returns the default value if the `Option` is `None`.

func (Option[T]) UnwrapUnchecked added in v1.1.0

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

UnwrapUnchecked returns a pointer to the contained value without checking if the `Option` is `Some`.

The caller is responsible for ensuring the `Option` is `Some` before calling this method. Calling this on a `None` `Option` will result in a nil pointer.

func (Option[T]) Xor added in v1.1.0

func (o Option[T]) Xor(other Option[T]) Option[T]

Xor returns the `Option` that is `Some`, if exactly one of them is `Some`.

This performs a logical "XOR" (exclusive OR) operation. It returns a `Some` `Option` only if either the current `Option` is `Some` and the `other` is `None`, or vice versa. If both are `Some` or both are `None`, it returns `None`.

Parameters:

  • other: The other `Option` to perform the XOR operation with.

Directories

Path Synopsis
examples
marshal command
simple command

Jump to

Keyboard shortcuts

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