Documentation ¶
Index ¶
- func Chain[T any](fns ...func(T) T) func(T) T
- func ChainWithErr[T any](fns ...func(T) (T, error)) func(T) (T, error)
- func Default[T comparable](values ...T) T
- func DefaultFunc[T comparable](callbacks ...func() T) T
- func DefaultWithFunc[T comparable](value T, callbacks ...func() T) T
- func ErrorIf(condition bool, format string, a ...any) error
- func If[T any](condition bool, trueVal T, falseVal T) T
- func IfFunc[T any](condition bool, trueFunc func() T, falseFunc func() T) T
- func IsEmpty[T comparable](value T) bool
- func IsType[T any](value any) bool
- func IsZero[T comparable](value T) bool
- func Once(fn func()) func()
- func Optional[T any](value *T) *T
- func PanicIf(condition bool, format string, a ...any)
- func Pipe[T any](fns ...func(T) T) func(T) T
- func PipeWithErr[T any](fns ...func(T) (T, error)) func(T) (T, error)
- func Ptr[T any](value T) *T
- func Repeat(fn func() error, times int) error
- func Retry(fn func() error, attempts int, sleeps ...time.Duration) (err error)
- func ReturnIf[T any](condition bool, callback func() T, defaults ...T) T
- func Scan(src any, dest any) error
- func Tap[T any](value T, callbacks ...func(T)) T
- func Timeout(fn func() error, timeout time.Duration) error
- func Transform[T, R any](value T, callback func(T) R) R
- func Unless[T any](condition bool, falseVal T, trueVal T) T
- func Until(fn func() bool, sleeps ...time.Duration)
- func UntilTimeout(fn func() bool, timeout time.Duration, sleeps ...time.Duration) error
- func Val[T any](value *T) T
- func When(condition bool, callback func())
- func With[T any](value T, callbacks ...func(T) T) T
- type Proxy
- func (p *Proxy[T]) Tap(callbacks ...func(T)) *Proxy[T]
- func (p *Proxy[T]) Transform(callback func(T) T) *Proxy[T]
- func (p *Proxy[T]) Unless(condition bool, callbacks ...func(T) T) *Proxy[T]
- func (p *Proxy[T]) Value() T
- func (p *Proxy[T]) When(condition bool, callbacks ...func(T) T) *Proxy[T]
- func (p *Proxy[T]) With(callbacks ...func(T) T) *Proxy[T]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Chain ¶
func Chain[T any](fns ...func(T) T) func(T) T
Chain is a reverse Pipe
Chain(m1, m2, m3)(value) => m1(m2(m3(value)))
func ChainWithErr ¶
ChainWithErr is a reverse PipeWithErr
ChainWithErr(m1, m2, m3)(value) => m1(m2(m3(value)))
func Default ¶ added in v2.12.0
func Default[T comparable](values ...T) T
Default returns the first non-zero value. If all values are zero, return the zero value.
Default("", "foo") // "foo" Default("bar", "foo") // "bar" Default("", "", "foo") // "foo"
func DefaultFunc ¶ added in v2.15.1
func DefaultFunc[T comparable](callbacks ...func() T) T
func DefaultWithFunc ¶ added in v2.12.0
func DefaultWithFunc[T comparable](value T, callbacks ...func() T) T
DefaultWithFunc returns defaultValue if value is zero, otherwise value.
DefaultWithFunc("", func() string { return "foo" }) // "foo" DefaultWithFunc("bar", func() string { return "foo" }) // "bar" DefaultWithFunc("", func() string { return "" }, func() string { return "foo" }) // "foo"
func ErrorIf ¶
ErrorIf returns an error if the condition is true.
ErrorIf(true, "error") => error ErrorIf(false, "error") => nil ErrorIf(true, "error %s", "with value") => error with value
func If ¶ added in v2.12.0
If returns trueVal if condition is true, otherwise falseVal.
If(true, "foo", "bar") // "foo" If(false, "foo", "bar") // "bar"
Warning: that the trueVal and falseVal in this method will also be executed during runtime, and there may be panics during use. Please be cautious when using it. You can use IfFunc or Optional to avoid this situation.
Example:
var nilVal *foo If(nilVal != nil, nilVal.Name, "") // panic: runtime error: invalid memory address or nil pointer dereference If(nilVal != nil, Optional(nilVal).Name, "") // ""
func IfFunc ¶ added in v2.12.0
IfFunc returns trueFunc() if condition is true, otherwise falseFunc().
IfFunc(true, func() string { return "foo" }, func() string { return "bar" }) // "foo"
func IsEmpty ¶ added in v2.12.0
func IsEmpty[T comparable](value T) bool
IsEmpty returns true if the value is zero.
IsEmpty(0) // true IsEmpty("") // true
func IsType ¶ added in v2.12.0
IsType returns true if the value is the type.
IsType[int](1) // true IsType[int]("foo") // false
func IsZero ¶ added in v2.12.0
func IsZero[T comparable](value T) bool
IsZero returns true if the value is zero.
IsZero(0) // true IsZero("") // true IsZero("foo") // false
func Once ¶ added in v2.14.2
func Once(fn func()) func()
Once returns a function that calls the given function only once.
once := Once(func() { fmt.Println("hello") }) once() // prints hello once() // does nothing
func Optional ¶ added in v2.12.0
func Optional[T any](value *T) *T
Optional returns the value if it is not nil, otherwise the zero value.
Optional(&foo{Name: "bar"}) // &foo{Name: "bar"} Optional[foo](nil) // &foo{} Optional[int](nil) // *int(0)
func PanicIf ¶
PanicIf panics if the condition is true.
PanicIf(true, "error") => panic("error") PanicIf(false, "error") => nil PanicIf(true, "error %s", "with value") => panic("error with value")
func Pipe ¶
func Pipe[T any](fns ...func(T) T) func(T) T
Pipe is a function that takes a value and returns a value
Pipe(m1, m2, m3)(value) => m3(m2(m1(value)))
func PipeWithErr ¶
PipeWithErr is a function that takes a value and returns a value and an error
PipeWithErr(m1, m2, m3)(value) => m3(m2(m1(value)))
func Ptr ¶ added in v2.12.0
func Ptr[T any](value T) *T
Ptr returns a pointer to the value.
Ptr("foo") // *string("foo") Ptr(1) // *int(1)
func Repeat ¶ added in v2.11.0
Repeat runs the given function `times` times or until an error is returned.
Repeat(func() error { fmt.Println("hello"); return nil }, 3) => prints hello 3 times and returns nil Repeat(func() error { return fmt.Errorf("error") }, 3) => returns error
func Retry ¶
Retry retries the given function until it returns nil or the attempts are exhausted. `sleeps` is the time to sleep between each attempt. If `sleeps` is not provided, it will not sleep.
Retry(func() error { return nil }, 3) => nil Retry(func() error { return nil }, 3, time.Second) => nil Retry(func() error { return fmt.Errorf("error") }, 3) => error
func ReturnIf ¶ added in v2.15.1
ReturnIf returns the result of the callback if the condition is true. Otherwise, return the default value or zero value.
ReturnIf(true, func() string { return "foo" }, "bar") // "foo" ReturnIf(false, func() string { return "foo" }, "bar") // "bar"
func Scan ¶
Scan sets the value of dest to the value of src.
var foo string Scan("bar", &foo) // foo == "bar" var bar struct {A string} Scan(struct{A string}{"foo"}, &bar) // bar == struct{A string}{"foo"}
func Tap ¶ added in v2.12.0
func Tap[T any](value T, callbacks ...func(T)) T
Tap calls the given callback with the given value then returns the value.
Tap("foo", func(s string) { fmt.Println(s) // "foo" and os.Stdout will print "foo" }, func(s string) { // more callbacks }...)
func Timeout ¶
Timeout runs the given function with a timeout. If the function does not return before the timeout, it returns an error.
Timeout(func() error { return nil }, time.Second) => nil Timeout(func() error { time.Sleep(2 * time.Second); return nil }, time.Second) => error
func Transform ¶ added in v2.12.0
func Transform[T, R any](value T, callback func(T) R) R
Transform calls the given callback with the given value then return the result.
Transform(1, strconv.Itoa) // "1" Transform("foo", func(s string) *foo { return &foo{Name: s} }) // &foo{Name: "foo"}
func Unless ¶ added in v2.12.0
Unless returns falseVal if condition is true, otherwise trueVal.
Unless(true, "foo", "bar") // "bar" Unless(false, "foo", "bar") // "foo"
func Until ¶
Until retries the given function until it returns true. `sleeps` is the time to sleep between each attempt. If `sleeps` is not provided, it will not sleep.
Until(func() bool { return true }) Until(func() bool { return true }, time.Second)
func UntilTimeout ¶ added in v2.13.0
UntilTimeout retries the given function until it returns true or the timeout is reached. `sleeps` is the time to sleep between each attempt. If `sleeps` is not provided, it will not sleep. The timeout includes the time to sleep.
UntilTimeout(func() bool { return true }, time.Second) UntilTimeout(func() bool { return true }, time.Second, time.Millisecond)
func Val ¶ added in v2.12.0
func Val[T any](value *T) T
Val returns the value of the pointer. If the pointer is nil, return the zero value.
Val((*string)(nil)) // "" Val(Ptr("foo")) // "foo"