gopts

package module
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2024 License: BSD-3-Clause Imports: 5 Imported by: 2

README

Gopts

Gopts is a small package to handle options in Go heavily inspired by Rust's Option enum.

basically, it's a wrapper around

if x != nil {
    return *x
}

Import

go get github.com/karim-w/gopts

Usage

package main

import (
    "log"
    "github.com/karim-w/gopts"
)

func getOption() gopts.Option[int] {
    // Create an option with a value
    return gopts.Some(5)
}

func getNothing() gopts.Option[int] {
    // Create an option with no value
    return gopts.None[int]()
}

func main() {
    // an option
    val := getOption()

    // check if there is a value
    if val.IsSome() {
        // pull the value
        num := val.Unwrap()

        log.Println("Value is", num)
    }

    // an option with no value
    val = getNothing()

    // check if there is nothing
    if val.IsNone() {
        log.Println("No value")
    }


    defer func() {
        if r := recover(); r != nil {
            log.Println("Panic:", r)
        }
    }()


    // when pulling a value from an option with no value
    // it will panic
    val.Unwrap()

}

License

BSD 3-Clause License

Author

karim-w

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option added in v1.0.1

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

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

func None

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

None returns an Option[T] with no value. Example:

opt := None()
fmt.Println(opt.IsNone()) // true

func Some

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

Some returns an Option[T] with a value. Example:

opt := Some(42)
fmt.Println(opt.IsSome()) // true

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

func (o Option[T]) Get() (res T, ok bool)

Get returns the value of the Options[T] and a boolean indicating if the value is present. Example:

opt := Some(42)
val, ok := opt.Get()
fmt.Println(val, ok) // 42, true

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

func (o Option[T]) GetOrElse(defaultValue T) (res T)

GetOrElse returns the value of the Options[T]. If the Options[T] has no value, it returns the default value. Example:

opt := Some(42)
fmt.Println(opt.GetOrElse(0)) // 42

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

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

IsNone returns true if the Options[T] has no value. Example:

opt := None()
fmt.Println(opt.IsNone()) // true

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

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

IsSome returns true if the Options[T] has a value. Example:

opt := Some(42)
fmt.Println(opt.IsSome()) // true

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

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

MarshalJSON implements the json.Marshaler interface.

func (*Option[T]) Scan added in v1.1.2

func (o *Option[T]) Scan(value interface{}) error

Scan implements the sql.Scanner interface for Option

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

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

UnmarshalJSON implements the json.Unmarshaler interface.

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

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

Unwrap returns the value of the Options[T]. If the Options[T] has no value, it panics. Example:

opt := Some(42)
fmt.Println(opt.Unwrap()) // 42

func (Option[T]) Value added in v1.1.2

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

Value implements the driver.Valuer interface for Option[T]

Jump to

Keyboard shortcuts

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