GithubHelp home page GithubHelp logo

go-option's Introduction

Option Type for Go

The Option package provides an Option type that represents encapsulation of an optional value; a value that may either contain a value (Some) or not contain a value (None), similar to Option in Rust.

Features

  • Some: Create an Option that contains a value.
  • None: Create an Option that represents no value.
  • Ok: Return the value if it exists.
  • Cause: Return the cause of None if it is due to an error.
  • Process: Transform the Option value with a function that might fail.
  • Map: Transform the Option value with a function that cannot fail.
  • Flatten: Nested Option values into a single Option.
  • Wrap: Wraps a value and an error into an Option.
  • WrapFn: Wraps a function that returns a value and an error into a function that returns an Option.

Installation

To use the Option package in your Go project, place the package in your project directory or a suitable location in your GOPATH. Import it using:

go get -u github.com/AY7295/go-option

Usage Examples

Here are some basic examples of how to use the Option type:

Creating Some and None

opt1 := option.Some(10)
opt2 := option.None[int]()

Processing an Option

opt := option.Some(5)
result := option.Process(opt, func(v int) (int, error) {
    if v == 0 {
        return 0, errors.New("zero value")
    }
    return 2 * v, nil
})

Mapping over an Option

opt := option.Some(10)
mapped := option.Map(opt, func(v int) int {
    return v + 1
})

Flattening an Option

doubleWrapped := option.Some(option.Some(20))
flattened := option.Flatten(doubleWrapped)

Testing

To run the tests for the Option package, navigate to the package directory and execute:

go test

This will run all the tests defined in option_test.go.

go-option's People

Contributors

ay7295 avatar

Stargazers

 avatar ComfyFluffy avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.