Skip to content

go-andiamo/gopt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Gopt

GoDoc Latest Version codecov Go Report Card

A very light Optional implementation in Golang

Installation

To install Gopt, use go get:

go get github.com/go-andiamo/gopt

To update Gopt to the latest version, run:

go get -u github.com/go-andiamo/gopt

Examples

package main

import (
    . "github.com/go-andiamo/gopt"
)

func main() {
    optFlt := Of[float64](1.23)
    println(optFlt.IsPresent())
    println(optFlt.OrElse(-1))

    opt2 := Empty[float64]()
    println(opt2.IsPresent())
    println(opt2.OrElse(-1))

    opt2.OrElseSet(10)
    println(opt2.IsPresent())
    println(opt2.OrElse(-1))
}

Methods

Method and description Returns
Get()
returns the value and an error if the value is not present
(T, error)
AsEmpty()
returns a new empty optional of the same type
Optional[T]
IsPresent()
returns true if the value is present, otherwise false
bool
IfPresent(f func(v T))
if the value is present, calls the supplied function with the value, otherwise does nothing
returns the original optional
Optional[T]
IfPresentOtherwise(f func(v T), other func())
if the value is present, calls the supplied function with the value, otherwise calls the other function
returns the original optional
Optional[T]
OrElse(other T)
returns the value if present, otherwise returns other
T
OrElseGet(f func() T)
returns the value if present, otherwise returns the result of calling the supplied function
T
OrElseSet(v T)
if the value is not present it is set to the supplied value
Optional[T]
OrElseError(err error)
returns the supplied error if the value is not present, otherwise returns nil
error
OrElsePanic(v any)
if the value is not present, panics with the supplied value, otherwise does nothing
nothing
DoWith(f func(v T))
if the value is present, calls the supplied function with the value
returns the original optional
Optional[T]
Filter(f func(v T) bool)
if the value is present and calling the supplied filter function returns true, returns a new optional describing the value
Otherwise returns an empty optional
Optional[T]
Map(f func(v T) any)
if the value is present and the result of calling the supplied mapping function returns non-nil, returns an optional describing that returned value
Otherwise returns an empty optional
Optional[any]
MarshalJSON()
implements JSON marshal
if the value is present, returns the marshalled data for the value
Otherwise, returns the marshalled data for null
([]byte, error)
UnmarshalJSON(data []byte)
implements JSON unmarshal
if the supplied data is null representation, sets the present to false
Otherwise, unmarshal the data as the value and sets the optional to present (unless the result of unmarshalling the value returns an error - in which case the present is set to false)
error
Scan(value interface{})
implements sql.Scan
error

Constructors

Constructor function and description
Of[T any](value T) Optional[T]
Creates a new optional with the supplied value
OfNillable[T any](value T) Optional[T]
Creates a new optional with the supplied value
If the supplied value is nil, an empty (not present) optional is returned
OfNillableString(value string) Optional[string]
Creates a new string optional with the supplied value
If the supplied value is an empty string, an empty (not-present) optional is returned
Empty[T any]() Optional[T]
Creates a new empty (not-present) optional of the specified type