package
module
Version:
v0.0.0-...-308a839
Opens a new window with list of versions in this module.
Published: Jul 27, 2022
License: MIT
Opens a new window with license information.
Imports: 0
Opens a new window with list of imports.
Imported by: 0
Opens a new window with list of known importers.
README
¶
optional
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
¶
func Slice[T any](v []T) *s[T]
Source Files
¶
Click to show internal directories.
Click to hide internal directories.