opt

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2023 License: Unlicense Imports: 0 Imported by: 0

README

go-opt

go-opt is an implementation of optional values in go.
Optional values can have a number of use cases such as:

  • Providing optional parameters to functions
  • Representing a look-up value that could be present or absent in particular context.

An optional value implements the following interface

package opt

type Option[T any] interface {

	// Get returns value of type T if present or panic if absent
	Get() T

	// GetOrElse returns value of type T if present else return the value passed
	// as a parameter.
	GetOrElse(T) T

	// GetOrZero returns value of type T if present else returns the zero value of T.
	GetOrZero() T

	// Empty returns true if value is absent else returns false.
	Empty() bool
}

This module provides two implementations of the Option[T] interface

  • Some[T]
  • None[T]

Installation

go get github.com/iUwej/go-opt

Usage

  1. To create an option with a value present, use the Some function.
package main

import "github.com/iUwej/go-opt"

intOpt := opt.Some(5) // creates an int option
strOpt := opt.Some("some string")
custOpt := opt.Some(CustomType())
  1. To create an option with a value absent, use the None function.
package main

import "github.com/iUwej/go-opt"

intOpt := opt.None[int]()
strOpt := opt.None[string]()
custOpt := opt.None[CustomType]()

Example

package main

import "github.com/iUwej/go-opt"



type User struct {
	Id     int64
	Name   string
	Emails []string
}

func FindUser(id string)opt.Option[User]  {
	user := User{
		Id : id,
		Name:   "admin",
		Emails: []string{"admin1@admin", "admin2@admin"},
	}
	if true{
		return opt.Some(user)
    } else{
		return opt.None[User]()
    }
	
}

func main() {
    userOpt := FindUser("some_id")
	// check if user present
	if userOpt.Empty(){
		//do something if userOpt is empty
    }
	// get the user if present or provide a default
	user := userOpt.GetOrElse(User{id:"back_up",Name: "Some Name"})
	
	//map the optional user into an optional name
	nameOpt := opt.Map(func(t User) string {
        return t.Name
	},userOpt)
}

Documentation

Overview

Package opt provides the specification and implementation of golang's optional values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option[T any] interface {

	// Get returns value of type T if present or panic if absent
	Get() T

	// GetOrElse returns value of type T if present else return the value passed
	// as a parameter.
	GetOrElse(T) T

	// GetOrZero returns value of type T if present else returns the zero value of T.
	GetOrZero() T

	// Empty returns true if value is absent else returns false.
	Empty() bool
}

Option defines the specification for a golang optional value.

func Map

func Map[T, K any](mapper func(t T) K, opt Option[T]) Option[K]

Map convert an Option of type T to an Option of type K.

func None

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

None returns the absent value for some type T.

func Some

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

Some returns an Option with value of type T present.

Jump to

Keyboard shortcuts

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