// Package basefn provide some no-dependents util functions
package basefn import ( ) // Panicf format panic message use fmt.Sprintf func ( string, ...any) { panic(fmt.Sprintf(, ...)) } // PanicIf if cond = true, panics with an error message func ( bool, ...any) { if { panic(errors.New(comfunc.FormatWithArgs())) } } // PanicErr panics if error is not empty func ( error) { if != nil { panic() } } // MustOK if error is not empty, will panic func ( error) { if != nil { panic() } } // 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 [ any]( , error) { if != nil { panic() } return } // 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 ( any, error) { PanicErr() } // ErrOnFail return input error on cond is false, otherwise return nil func ( bool, error) error { return OrError(, ) } // OrError return input error on cond is false, otherwise return nil func ( bool, error) error { if ! { return } return nil } // FirstOr get first elem or elseVal func [ any]( [], ) { if len() > 0 { return [0] } return } // OrValue get. like: if cond { okVal } else { elVal } func [ any]( bool, , ) { if { return } return } // OrReturn call okFunc() on condition is true, else call elseFn() // // like expr: if cond { okFunc() } else { elseFn() } func [ any]( bool, , func() ) { if { return () } return () } // ErrFunc type type ErrFunc func() error // CallOn call func on condition is true func ( bool, ErrFunc) error { if { return () } return nil } // CallOrElse call okFunc() on condition is true, else call elseFn() func ( bool, , ErrFunc) error { if { return () } return () }