Option container for golang

Option

Option provides an Option container which can be used to force some additional presence assertions on values.

Example

Having:

type User struct {
    Name string
    Age  int
}
defaultUser := User{Name: "A", Age: 1}
aUser := User{Name: "B", Age: 2}

Unpack Some[User]:

// option.Option[User]
maybeUser := option.O(aUser)
// option is a user, use value, ignore default
user := maybeUser.Default(defaultUser)
fmt.Println(user == aUser) // => true

Call a function if Option is Some:

// option.Option[User]
maybeUser := option.O(aUser)
maybeUser.Some(func(u User) {
	fmt.Println("name: ", u.Name)
}) // name: B

Unpack None[User]:

maybeUser := option.O[User]() // option.None[User]
// No user, use default
user := maybeUser.Default(defaultUser)
fmt.Println(user == defaultUser) // => true

Unpack None[User] (verbose):

user := maybeUser.Switchv(
    func(u User) User { return u },
    func() User { return defaultUser })
fmt.Println(user == defaultUser) // => true

Switch on Option:

maybeUser := option.O(aUser)
maybeUser.Switch(
    func(u User) {
        fmt.Printf("Got a user %s, aged %d\n", u.Name, u.Age)
    },
    func() {
        fmt.Println("Got nothing")
    })

Iterate

opts := option.Slice[User](
    User{Name: "Douglas", Age: 42},
    User{Name: "Neil", Age: 61},
)

opts.EachPtr(func(s *User) {
    s.Age*=2
})
opts.Each(func(s User) {
    fmt.Println(s)
})
// => {Douglas 84}
// => {Neil 122}

ultimateWriter := option.FoldIdxl(opts, func(i int, res User, next User) User {
    if i != len(opts)-1 {
        res.Name += next.Name + " "
    }
    res.Age += next.Age
    return res
}, User{})
fmt.Println(ultimateWriter) // {Douglas Neil 206}
Similar Resources

An os/exec like interface for running a command in a container, and being able to easily interact with stdin, stdout, and other adjustments

dockerexec An "os/exec" like interface for running a command in a container, and being able to easily interact with stdin, stdout, and other adjustmen

Jul 14, 2022

Argparse for golang. Just because `flag` sucks

Golang argparse Let's be honest -- Go's standard command line arguments parser flag terribly sucks. It cannot come anywhere close to the Python's argp

Dec 28, 2022

Golang library with POSIX-compliant command-line UI (CLI) and Hierarchical-configuration. Better substitute for stdlib flag.

Golang library with POSIX-compliant command-line UI (CLI) and Hierarchical-configuration. Better substitute for stdlib flag.

cmdr cmdr is a POSIX-compliant, command-line UI (CLI) library in Golang. It is a getopt-like parser of command-line options, be compatible with the ge

Oct 28, 2022

CONTRIBUTIONS ONLY: A Go (golang) command line and flag parser

CONTRIBUTIONS ONLY What does this mean? I do not have time to fix issues myself. The only way fixes or new features will be added is by people submitt

Dec 29, 2022

A CLI tool implemented by Golang to manage `CloudComb` resource

CloudComb CLI tool: comb Get Started comb is a CLI tool for manage resources in CloudComb base on cloudcomb-go-sdk. Support Mac, Linux and Windows. We

Jan 4, 2021

Automatically generate Go (golang) struct definitions from example JSON

gojson gojson generates go struct definitions from json or yaml documents. Example $ curl -s https://api.github.com/repos/chimeracoder/gojson | gojson

Jan 1, 2023

Command Line Alias Manager and Plugin System - Written in Golang

Command Line Alias Manager and Plugin System - Written in Golang

aly - Command Line Alias Manager and Packager Aly offers the simplest way to manage, share, and obtain command line aliases! Warning: This project is

Jun 16, 2022

💻 PTerm | Pretty Terminal Printer A golang module to print pretty text

💻 PTerm | Pretty Terminal Printer A golang module to print pretty text

✨ PTerm is a modern go module to beautify console output. Featuring charts, progressbars, tables, trees, and many more 🚀 It's completely configurable and 100% cross-platform compatible.

Jan 1, 2023

A collection of terminal-based widgets for richer Golang CLI apps.

A collection of terminal-based widgets for richer Golang CLI apps.

Flinch A collection of terminal-based widgets for richer Golang CLI apps. Ships with a library to build your own widgets/TUIs too. Warning: This modul

Jan 7, 2023
Related tags
Option: a busy optional parameter for golang

Option option is a busy optional parameter init opt := option.New() set opt.Set("parameter name", 3.141592) apply a := 0.0 opt.Apply("parameter name",

Dec 28, 2021
Flag is a simple but powerful command line option parsing library for Go support infinite level subcommand

Flag Flag is a simple but powerful commandline flag parsing library for Go. Documentation Documentation can be found at Godoc Supported features bool

Sep 26, 2022
go command line option parser

go-flags: a go library for parsing command line arguments This library provides similar functionality to the builtin flag library of go, but provides

Jan 4, 2023
A very simple library for interactively selecting an option on a terminal
A very simple library for interactively selecting an option on a terminal

go-choice A very simple library for interactively selecting an option on a terminal Usage package main import ( "fmt" "github.com/TwiN/go-ch

Dec 30, 2021
top in container - Running the original top command in a container
top in container - Running the original top command in a container

Running the original top command in a container will not get information of the container, many metrics like uptime, users, load average, tasks, cpu, memory, are about the host in fact. topic(top in container) will retrieve those metrics from container instead, and shows the status of the container, not the host.

Dec 2, 2022
Top-like interface for container metrics
Top-like interface for container metrics

Top-like interface for container metrics ctop provides a concise and condensed overview of real-time metrics for multiple containers: as well as a sin

Jan 9, 2023
Go library and CLIs for working with container registries
Go library and CLIs for working with container registries

Go library and CLIs for working with container registries

Jun 1, 2021
Go library and CLIs for working with container registries
Go library and CLIs for working with container registries

Go library and CLIs for working with container registries

Dec 27, 2022
CLI tool and library for generating a Software Bill of Materials from container images and filesystems
CLI tool and library for generating a Software Bill of Materials from container images and filesystems

A CLI tool and Go library for generating a Software Bill of Materials (SBOM) from container images and filesystems. Exceptional for vulnerability dete

Jan 6, 2023
wy : a set of command-line tools to test your container-based platform

wy wy (Abbreviation of Would You) is a set of command-line tools to test your container-based platform. ToC: Commands Deployment Monitoring Contributi

Apr 30, 2022