option

package
v1.6.2 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2022 License: MIT Imports: 0 Imported by: 4

README

Option

option is optional value

How to use

create
someInt := option.Some(100)
noneInt := option.None[int]()

someString := option.Some("Hello, World!")
noneString := option.None[string]()
unwrap
someInt := option.Some(100)
noneInt := option.None[int]()

if someInt.Ok() {
    fmt.Println(someInt.Unwrap())
}

if !noneInt.Ok() {
    log.Fatal(errors.New("noneInt is empty"))
}

Default Parameter

func Add(a *option.Option[int], b *option.Option[int]) int {
	return a.UnwrapOr(100) + b.UnwrapOr(100)
}

func main() {
	fmt.Println(Add(option.Some(1), option.Some(2)))
	fmt.Println(Add(nil, option.Some(2)))
	fmt.Println(Add(nil, nil))
}
3
102
200

Documentation

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
}

func None

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

func Some

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

func (*Option[T]) AndThen

func (o *Option[T]) AndThen(fn func(T) (T, error)) *Option[T]

func (*Option[T]) Map

func (o *Option[T]) Map(fn func(T) (T, error))

func (*Option[T]) MapOr

func (o *Option[T]) MapOr(fn func(T) (T, error), defaultValue T)

func (*Option[T]) Ok

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

func (*Option[T]) Unwrap

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

func (*Option[T]) UnwrapOr

func (o *Option[T]) UnwrapOr(defaultValue T) T

Jump to

Keyboard shortcuts

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