basefn

package
v0.6.18 Latest Latest
Warning

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

Go to latest
Published: Dec 9, 2024 License: MIT Imports: 2 Imported by: 5

Documentation

Overview

Package basefn provide some no-dependents util functions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallOn

func CallOn(cond bool, fn ErrFunc) error

CallOn call func on condition is true

func CallOrElse

func CallOrElse(cond bool, okFn, elseFn ErrFunc) error

CallOrElse call okFunc() on condition is true, else call elseFn()

func ErrOnFail

func ErrOnFail(cond bool, err error) error

ErrOnFail return input error on cond is false, otherwise return nil

func FirstOr

func FirstOr[T any](sl []T, elseVal T) T

FirstOr get first elem or elseVal

func Must

func Must[T any](v T, err error) T

Must return like (v, error). will panic on error, otherwise return v.

Usage:

// old
v, err := fn()
if err != nil {
	panic(err)
}

// new
v := goutil.Must(fn())

func MustIgnore added in v0.6.16

func MustIgnore(_ any, err error)

MustIgnore for return like (v, error). Ignore return v and will panic on error.

Useful for io, file operation func: (n int, err error)

Usage:

// old
_, err := fn()
if err != nil {
	panic(err)
}

// new
basefn.MustIgnore(fn())

func MustOK

func MustOK(err error)

MustOK if error is not empty, will panic

func OrError

func OrError(cond bool, err error) error

OrError return input error on cond is false, otherwise return nil

func OrReturn

func OrReturn[T any](cond bool, okFn, elseFn func() T) T

OrReturn call okFunc() on condition is true, else call elseFn()

func OrValue

func OrValue[T any](cond bool, okVal, elVal T) T

OrValue get

func PanicErr added in v0.6.12

func PanicErr(err error)

PanicErr panics if error is not empty

func PanicIf added in v0.6.12

func PanicIf(cond bool, fmtAndArgs ...any)

PanicIf if cond = true, panics with error message

func Panicf

func Panicf(format string, v ...any)

Panicf format panic message use fmt.Sprintf

Types

type ErrFunc

type ErrFunc func() error

ErrFunc type

Jump to

Keyboard shortcuts

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