option

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2024 License: Apache-2.0 Imports: 6 Imported by: 0

README

option

github goref

Option type for golang

Install

go get -u github.com/cospectrum/option

Requires Go version 1.22.0 or greater.

Usage

import (
	"fmt"

	"github.com/cospectrum/option"
)

func main()
	divide := func(numerator, denominator float64) option.Option[float64] {
		if denominator == 0.0 {
			return option.None[float64]()
		}
		return option.Some(numerator / denominator)
	}

	// The return value of the function is an option
	result := divide(2.0, 3.0)

	// Pattern match to retrieve the value
	result.Match(
		func(val float64) {
			fmt.Printf("Result: %v\n", val)
		},
		func() {
			fmt.Println("Cannot divide by 0")
		},
	)
}
JSON
type U struct {
	Num option.Option[int] `json:"num"`
}

var u U
json.Unmarshal([]byte(`{"num": null}`), &u) // => U{Num: option.None()}
json.Unmarshal([]byte(`{}`), &u) // => U{Num: option.None()}
json.Unmarshal([]byte(`{"num": 0}`), &u) // => U{Num: option.Some(0)}
json.Unmarshal([]byte(`{"num": 3}`), &u) // => U{Num: option.Some(3)}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Match

func Match[T, U any](opt Option[T], some func(T) U, none func() U) U

If option has a value, returns result of the first function, else returns result of the second function.

Types

type Option

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

Type Option represents an optional value: every Option is either `Some` and contains a value, or `None`, and does not.

func AsPtr added in v0.0.3

func AsPtr[T any](opt *Option[T]) Option[*T]

Converts from *Option<T> to Option<*T> without copy.

func Map added in v0.0.3

func Map[T, U any](opt Option[T], f func(T) U) Option[U]

Maps an Option[T] to Option[U] by applying a function to a contained value (if Some) or returns None (if None).

func None

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

Creates Option[T] without a value

func Some

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

Creates Option[T] with the specified value

func (Option[T]) Clone

func (opt Option[T]) Clone() Option[T]

Returns a copy of the option.

func (Option[T]) Expect

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

Returns the contained value. Panics if there is no value with a custom panic message.

func (Option[T]) IsNone

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

Returns true if the option has no value.

func (Option[T]) IsSome

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

Returns true if the option has a value.

func (Option[T]) IsSomeAnd

func (opt Option[T]) IsSomeAnd(f func(T) bool) bool

Returns true if the option has a value and the value matches a predicate.

func (Option[T]) MarshalJSON

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

func (Option[T]) Match added in v0.0.2

func (opt Option[T]) Match(some func(T), none func())

If option has a value, calls the first function, else calls the second function.

func (*Option[T]) Scan

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

func (Option[T]) String

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

func (*Option[T]) Take

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

Takes the value out of the option, leaving a None in its place.

func (*Option[T]) UnmarshalJSON

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

func (Option[T]) Unwrap

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

Returns the contained value. Panics if there is no value.

func (Option[T]) UnwrapOr

func (opt Option[T]) UnwrapOr(defaultVal T) T

Returns the contained value or a provided default.

func (Option[T]) UnwrapOrDefault

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

Returns the contained value or a default.

func (Option[T]) UnwrapOrElse

func (opt Option[T]) UnwrapOrElse(f func() T) T

Returns the contained value or computes it from a function.

func (Option[T]) Value

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

Jump to

Keyboard shortcuts

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