utils

package module
v0.0.2024022347 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2024 License: Apache-2.0 Imports: 8 Imported by: 32

README

go-utils

Go utils for internal use

Documentation

Index

Constants

This section is empty.

Variables

View Source
var WaitForDebugger = Debugger

Functions

func Assert

func Assert(b bool, messages ...string)

func Bind

func Bind[T any](err error, fn func() (T, error)) (T, error)

Bind returns the result of the given function that can fail if err is nil, otherwise the error.

func Bind0

func Bind0(err error, fn func() error) error

Bind0 is an alias of Then.

func Bind1

func Bind1[T any](err error, fn func() (T, error)) (T, error)

Bind1 is an alias of Bind.

func Bind2

func Bind2[T any, U any](err error, fn func() (T, U, error)) (T, U, error)

func Catch added in v0.0.2024021245

func Catch(errRef *error)

func Contains

func Contains[T comparable](items []T, target T) bool

Contains checks if “target” is in “items”.

func Debugger added in v0.0.2024021544

func Debugger()

Debugger waits for a debugger to connect if the environment variable $WAIT or $DEBUG is set

func E added in v0.0.2024022328

func E(rest ...any) error

func Ensure

func Ensure[T any](value T, err error) T

Ensure checks if the value is available. If err is not nil, it panics.

func Ensure0

func Ensure0[T any](first T, rest ...any)

Ensure0 checks that err is nil. If err is not nil, it panics.

func Ensure1

func Ensure1[T any](value T, err error) T

func Ensure2

func Ensure2[T any, U any](value T, value2 U, err error) (T, U)

func Err

func Err[T any](err error) (T, error)

Err returns a zero value and the given error.

func Ignore

func Ignore[T any](T, ...any)

Ignore ignores errors explicitly.

func Let

func Let[T any](err error, fn func() T) T

Let returns the result of the given function if err is nil.

func Let0

func Let0(err error, fn func())

Let0 calls the given function if err is nil and returns nothing.

func Let1

func Let1[T any](err error, fn func() T) T

Let1 is an alias of Let.

func Let2

func Let2[T any, U any](err error, fn func() (T, U)) (T, U)

func Map

func Map[T any, U any](items []T, fn func(T) U) []U

Map applies “fn” to each item in “items” and returns the result.

func Ok

func Ok[T any](value T) (T, error)

Ok returns the given value and nil.

func Ptr

func Ptr[T any](v T) *T

Ptr returns a pointer to a copied value.

func PtrMap

func PtrMap[T any, U any](pt *T, fn func(*T) *U) *U

PtrMap returns a pointer to the result of the given function.

func R2 added in v0.0.2024021524

func R2[T any, U any](value T, value2 U, err error) (T, U)

func RecoverPanicObject

func RecoverPanicObject[T comparable](errRef *error, target T, fn func() error)

func RecoverPanicType

func RecoverPanicType[T any](errRef *error, fn func(T) error)

func Result added in v0.0.2024022347

func Result[T any](ptr *T, err error) *ptrResult[T]

Result returns a pointer + error result context to ignore specific errors.

func Ternary

func Ternary[T any](
	cond bool,
	t T,
	f T,
) T

Ternary returns the first value if cond is true, otherwise the second value.

func TernaryF

func TernaryF[T any](
	cond bool,
	t func() T,
	f func() T,
) T

Ternary returns the result of the first function if cond is true, otherwise the result of the second function.

func Then

func Then(err error, fn func() error) error

Then calls the given function if err is nil.

func V added in v0.0.2024022328

func V[T any](value T, err error) T

func V0 added in v0.0.2024022328

func V0[T any](first T, rest ...any)

func ValueResult added in v0.0.2024022347

func ValueResult[T any](value T, err error) *ptrResult[T]

ValueResult returns a value + error result context to ignore specific errors.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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