optional

package module
v0.0.0-...-308a839 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2022 License: MIT Imports: 0 Imported by: 0

README

optional GoDoc Go Report Card Coverage

Usage

// Comparable - go primitives
optional.Comparable(0).Empty()      // true
optional.Comparable("").Else("str") // str
optional.Comparable("").Present()   // false
optional.Comparable(0.0).IfEmpty(func(f float64) {
    fmt.Println("float '0.0' is empty")
})
optional.Comparable(1).IfPresent(func(i int) {
    fmt.Println("int 1 is present")
})
optional.Comparable(1).If(func(i int) bool {
    return i == 1
}).Get() // 1

// Slice
optional.Slice([]int{}).Empty()        // true
optional.Slice([]int{}).Else([]int{1}) // int{1}
optional.Slice([]int{}).Present()      // false
optional.Slice([]int{}).Else([]int{1}) // int{1}
optional.Slice([]string{"str"}).IfPresent(func(t []string) {
    fmt.Println(t) // str
})
optional.Slice([]int{1, 2, 3}).If(func(t []int) bool {
    return len(t) == 2
}).Else([]int{1, 2}) // int{1,2}

// Map
optional.Map(map[int]string{}).Empty()              // true
optional.Map(map[int]int{}).Else(map[int]int{1: 1}) // [int]int{1:1}

// Error
var e error
optional.Error(e).IfEmpty(func(a any) {
    fmt.Println("e is nil")
})

// Interface
var i interface{}
optional.Interface(i).IfEmpty(func(a any) {
    fmt.Println("i is nil")
})

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Comparable

func Comparable[T comparable](v T) *c[T]

func Error

func Error(v any) *i

func Interface

func Interface(v any) *i

func Map

func Map[K comparable, V any](v map[K]V) *m[K, V]

func Slice

func Slice[T any](v []T) *s[T]

Types

This section is empty.

Jump to

Keyboard shortcuts

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