o

package
v2.80.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

o provides an Option type to represent values that may or may not be present.

This code was copies from https://github.com/BooleanCat/go-functional@ae5a155c0e997d1c5de53ea8b49109aca9c53d9f and we've added the Map function and associated tests. It was pulled into the project because I believe if we're using Option, it should be a core domain type rather than a dependency.

Index

Examples

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
}

Option represents an optional value. The Some variant contains a value and the None variant represents the absence of a value.

func Map

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

Map applies a function to the contained value of (if Some), or returns None.

Use this function very sparingly as it can lead to very unidiomatic and surprising Go code. However, there are times when used judiciiously, it is significantly more ergonomic than unwrapping the Option.

Example
package main

import (
	"fmt"

	o "github.com/cli/cli/v2/pkg/option"
)

func main() {
	fmt.Println(o.Map(o.Some(2), double))
	fmt.Println(o.Map(o.None[int](), double))

}

func double(i int) int {
	return i * 2
}
Output:

Some(4)
None

func None

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

None instantiates an Option with no value.

func Some

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

Some instantiates an Option with a value.

func SomeIfNonZero added in v2.71.0

func SomeIfNonZero[T comparable](value T) Option[T]

func (Option[T]) Expect

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

Expect returns the underlying value for a Some variant, or panics with the provided message for a None variant.

Example
package main

import (
	"fmt"

	o "github.com/cli/cli/v2/pkg/option"
)

func main() {
	fmt.Println(o.Some(4).Expect("oops"))

}
Output:

4

func (Option[T]) IsNone

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

IsNone returns true if the Option is a None variant.

Example
package main

import (
	"fmt"

	o "github.com/cli/cli/v2/pkg/option"
)

func main() {
	fmt.Println(o.Some(4).IsNone())
	fmt.Println(o.None[int]().IsNone())

}
Output:

false
true

func (Option[T]) IsSome

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

IsSome returns true if the Option is a Some variant.

Example
package main

import (
	"fmt"

	o "github.com/cli/cli/v2/pkg/option"
)

func main() {
	fmt.Println(o.Some(4).IsSome())
	fmt.Println(o.None[int]().IsSome())

}
Output:

true
false

func (Option[T]) String

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

String implements the fmt.Stringer interface.

func (Option[T]) Unwrap

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

Unwrap returns the underlying value of a Some variant, or panics if called on a None variant.

Example
package main

import (
	"fmt"

	o "github.com/cli/cli/v2/pkg/option"
)

func main() {
	fmt.Println(o.Some(4).Unwrap())
}
Output:

4

func (Option[T]) UnwrapOr

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

UnwrapOr returns the underlying value of a Some variant, or the provided value on a None variant.

Example
package main

import (
	"fmt"

	o "github.com/cli/cli/v2/pkg/option"
)

func main() {
	fmt.Println(o.Some(4).UnwrapOr(3))
	fmt.Println(o.None[int]().UnwrapOr(3))
}
Output:

4
3

func (Option[T]) UnwrapOrElse

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

UnwrapOrElse returns the underlying value of a Some variant, or the result of calling the provided function on a None variant.

Example
package main

import (
	"fmt"

	o "github.com/cli/cli/v2/pkg/option"
)

func main() {
	fmt.Println(o.Some(4).UnwrapOrElse(func() int {
		return 3
	}))

	fmt.Println(o.None[int]().UnwrapOrElse(func() int {
		return 3
	}))

}
Output:

4
3

func (Option[T]) UnwrapOrZero

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

UnwrapOrZero returns the underlying value of a Some variant, or the zero value on a None variant.

Example
package main

import (
	"fmt"

	o "github.com/cli/cli/v2/pkg/option"
)

func main() {
	fmt.Println(o.Some(4).UnwrapOrZero())
	fmt.Println(o.None[int]().UnwrapOrZero())

	// Output
	// 4
	// 0
}

func (Option[T]) Value

func (o Option[T]) Value() (T, bool)

Value returns the underlying value and true for a Some variant, or the zero value and false for a None variant.

Example
package main

import (
	"fmt"

	o "github.com/cli/cli/v2/pkg/option"
)

func main() {
	value, ok := o.Some(4).Value()
	fmt.Println(value)
	fmt.Println(ok)

}
Output:

4
true

Jump to

Keyboard shortcuts

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