Documentation ¶
Index ¶
- func Pointer[T any](o Option[T]) *T
- type Option
- func (o Option[T]) Equal(other Option[T]) bool
- func (o Option[T]) GoString() string
- func (o *Option[T]) IsNone() bool
- func (o *Option[T]) IsPresent() bool
- func (o Option[T]) MarshalJSON() ([]byte, error)
- func (o *Option[T]) Pointer() *T
- func (o *Option[T]) Scan(src any) error
- func (o Option[T]) String() string
- func (o *Option[T]) UnmarshalJSON(bytes []byte) error
- func (o *Option[T]) Unpack() (T, bool)
- func (o *Option[T]) Unwrap() T
- func (o *Option[T]) UnwrapOr(defaultValue T) T
- func (o *Option[T]) UnwrapOrZero() T
- func (o Option[T]) Value() (driver.Value, error)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Option ¶
type Option[T any] struct { // contains filtered or unexported fields }
Option[T] represents an optional value of type T.
Options that have no value are called None. The zero value of Option[T] is None.
func FromPointer ¶
FromPointer creates Option[T] from a pointer. If the pointer is nil, None is returned. Otherwise, a new Option[T] with the pointed value is returned.
func Map ¶
Map returns a new option by applying the given function to the value of the option. If the option is None, None is returned.
Example ¶
package main import ( "fmt" "github.com/cybozu-go/options" ) func main() { getLength := func(s string) int { return len(s) } some := options.New("hello") fmt.Printf("some: %#v\n", options.Map(some, getLength)) none := options.None[string]() fmt.Printf("none: %#v\n", options.Map(none, getLength)) }
Output: some: options.New(5) none: options.None[int]()
func Pack ¶ added in v0.3.0
Pack creates Option[T] from (T, bool). If the bool is true, a new Option[T] with the given value is returned. Otherwise, None is returned.
Example ¶
package main import ( "fmt" "github.com/cybozu-go/options" ) func main() { some := options.Pack(42, true) fmt.Println(some.GoString()) none := options.Pack[int](0, false) fmt.Println(none.GoString()) }
Output: options.New(42) options.None[int]()
func (Option[T]) Equal ¶
Equal returns true if the two options are equal. Equality of the wrapped values is determined by reflect.DeepEqual.
Usually you don't need to call this method since you can use == operator. This method is provided to make Option[T] comparable by go-cmp.
func (Option[T]) GoString ¶
GoString returns the Go representation of the option.
Example ¶
package main import ( "fmt" "github.com/cybozu-go/options" ) func main() { some := options.New(true) fmt.Printf("some: %#v\n", some) none := options.None[bool]() fmt.Printf("none: %#v\n", none) }
Output: some: options.New(true) none: options.None[bool]()
func (Option[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*Option[T]) Pointer ¶
func (o *Option[T]) Pointer() *T
Pointer returns a pointer to the wrapped value of the option. If the option is None, nil is returned.
func (*Option[T]) Scan ¶
Scan implements the SQL driver.Scanner interface. See http://jmoiron.net/blog/built-in-interfaces
func (Option[T]) String ¶
String returns the string representation of the wrapped value. If the option is None, an empty string is returned.
Example ¶
package main import ( "fmt" "github.com/cybozu-go/options" ) func main() { some := options.New(true) fmt.Println("some:", some.String()) none := options.None[bool]() fmt.Println("none:", none.String()) }
Output: some: true none:
func (*Option[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface.
func (*Option[T]) Unpack ¶ added in v0.3.0
Unpack returns (value, present). If the option is None, value is always zero. This function is useful when you want to check the presence of the value and get it at once.
Example ¶
package main import ( "fmt" "github.com/cybozu-go/options" ) func main() { some := options.New("hello") if s, ok := some.Unpack(); ok { fmt.Println(s) } none := options.None[string]() if s, ok := none.Unpack(); ok { fmt.Println(s) } }
Output: hello
func (*Option[T]) Unwrap ¶
func (o *Option[T]) Unwrap() T
Unwrap returns the value of the option. If the option is None, Unwrap panics. You should check the option with Option.IsPresent before calling this method.
Example ¶
package main import ( "fmt" "github.com/cybozu-go/options" ) func main() { opt := options.New(42) fmt.Println(opt.Unwrap()) }
Output: 42
func (*Option[T]) UnwrapOr ¶
func (o *Option[T]) UnwrapOr(defaultValue T) T
UnwrapOr returns the value of the option. If the option is None, the given default value is returned.
Example ¶
package main import ( "fmt" "github.com/cybozu-go/options" ) func main() { some := options.New(42) fmt.Println(some.UnwrapOr(-1)) none := options.None[int]() fmt.Println(none.UnwrapOr(-1)) }
Output: 42 -1
func (*Option[T]) UnwrapOrZero ¶
func (o *Option[T]) UnwrapOrZero() T
UnwrapOrZero returns the value of the option. If the option is None, the zero value of T is returned.
Example ¶
package main import ( "fmt" "github.com/cybozu-go/options" ) func main() { some := options.New(42) fmt.Println(some.UnwrapOrZero()) none := options.None[int]() fmt.Println(none.UnwrapOrZero()) }
Output: 42 0
func (Option[T]) Value ¶
Value implements the SQL driver.Valuer interface. See http://jmoiron.net/blog/built-in-interfaces