utils

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2024 License: Apache-2.0 Imports: 9 Imported by: 28

README

go-utils

Go utils for internal use.

Documentation

Index

Constants

This section is empty.

Variables

Functions

func AddLogWriter added in v0.0.2024072645

func AddLogWriter(writer io.Writer)

func Assert

func Assert(b bool, a ...any)

func Assertf added in v0.0.2024073012

func Assertf(b bool, format string, a ...any)

Assertf panics with the given message if the condition is false.

func Assign added in v0.0.2024080445

func Assign[T any](dst *T, src T) T

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, fns ...func(error))

func Debugger added in v0.0.2024021544

func Debugger()

Debugger waits for a debugger to connect if the environment variable $WAIT_DEBUGGER is set or the first argument is "--wait-debugger"

func E added in v0.0.2024022328

func E(args ...any) error

E returns error.

func Elvis added in v0.0.2024072420

func Elvis[T comparable](t T, f T) T

Elvis returns the first value if it is not empty, otherwise the second value. // Elvis operator - Wikipedia https://en.wikipedia.org/wiki/Elvis_operator

func ElvisF added in v0.0.2024072421

func ElvisF[T comparable](
	t func() T,
	f func() T,
) (ret T)

ElvisF returns the result of the first function if it is not empty, otherwise the result of the second function.

func Err

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

Err returns a zero value and the given error.

func ErrorAs added in v0.0.2024080520

func ErrorAs[T error](err error) (t T)

ErrorAs returns the error as the type T if possible, otherwise returns nil.

func Expect added in v0.0.2024073012

func Expect(err error, expectedErrors ...error)

Expect expects the error to be nil or one of the errors passed as arguments.

func Ignore

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

Ignore ignores an error 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 LogPrint added in v0.0.2024072645

func LogPrint(v ...interface{})

func LogPrintf added in v0.0.2024072645

func LogPrintf(format string, v ...interface{})

func LogPrintln added in v0.0.2024072645

func LogPrintln(v ...interface{})

func Nil added in v0.0.2024072641

func Nil[T any]() (t T)

Nil returns a zero value.

func Ok

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

Ok returns the given value and nil.

func P added in v0.0.2024022348

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

P returns a pointer to a copied value.

func PMap added in v0.0.2024022348

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

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

func PR added in v0.0.2024022348

func PR[T any](value T, err error) *valueResult[*T]

PR returns a pointer + error result context.

func Ptr

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

func PtrMap

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

func R added in v0.0.2024021526

func R[T any](value T, err error) *valueResult[T]

R returns a value + error result context.

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 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. // Ternary conditional operator - Wikipedia https://en.wikipedia.org/wiki/Ternary_conditional_operator

func TernaryF

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

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

func TernaryF2 added in v0.0.2024072004

func TernaryF2[T any, U any](
	cond bool,
	t func() (T, U),
	f func() (T, U),
) (ret1 T, ret2 U)

TernaryF2 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 Throw added in v0.0.2024080445

func Throw(err error)

func ThrowIf added in v0.0.2024080537

func ThrowIf(err error)

func V added in v0.0.2024022328

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

V returns the value. If err is not nil, it panics.

func V0 added in v0.0.2024022328

func V0(args ...any)

V0 returns no value. If err is not nil, it panics.

func V2 added in v0.0.2024070104

func V2[T any, U any](value1 T, value2 U, err error) (T, U)

V2 returns two values. If err is not nil, it panics.

func V3 added in v0.0.2024070250

func V3[T any, U any, V any](value1 T, value2 U, value3 V, err error) (T, U, V)

V3 returns three values. If err is not nil, it panics.

func WithStack added in v0.0.2024073012

func WithStack(err error) error

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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