stdutil

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2022 License: MIT Imports: 14 Imported by: 0

README

Std Utils

  • some features

Install

go get github.com/gookit/goutil/stdutils

Go docs

Usage

Testings

go test -v ./stdutils/...

Test limit by regexp:

go test -v -run ^TestSetByKeys ./stdutils/...

Documentation

Overview

Package stdutil provide some standard util functions for go.

Index

Constants

This section is empty.

Variables

View Source
var (
	DefStackLen = 10000
	MaxStackLen = 100000
)

some commonly consts

Functions

func BaseTypeVal added in v0.5.2

func BaseTypeVal(val any) (value any, err error)

BaseTypeVal convert custom type or intX,uintX,floatX to generic base type.

intX/unitX 	=> int64
floatX      => float64
string 	    => string

returns int64,string,float or error

func BaseTypeVal2 added in v0.5.2

func BaseTypeVal2(v reflect.Value) (value any, err error)

BaseTypeVal2 convert custom type or intX,uintX,floatX to generic base type.

intX/unitX 	=> int64
floatX      => float64
string 	    => string

returns int64,string,float or error

func CheckContains added in v0.5.7

func CheckContains(data, elem any) (valid, found bool)

CheckContains try loop over the data check if the data includes the element.

TIP: only support types: string, map, array, slice

map         - check key exists
string 	    - check sub-string exists
array,slice - check sub-element exists

return (false, false) if impossible. return (true, false) if element was not found. return (true, true) if element was found.

func Contains added in v0.5.7

func Contains(data, elem any) bool

Contains try loop over the data check if the data includes the element. alias of the IsContains

TIP: only support types: string, map, array, slice

map         - check key exists
string 	    - check sub-string exists
array,slice - check sub-element exists

func CutFuncName added in v0.5.0

func CutFuncName(fullFcName string) (pkgPath, shortFnName string)

CutFuncName get pkg path and short func name

func DiscardE added in v0.5.6

func DiscardE(_ error)

DiscardE discard error

func FuncName added in v0.4.2

func FuncName(fn any) string

FuncName get full func name, contains pkg path.

eg:

// OUTPUT: github.com/gookit/goutil/stdutil.PanicIf
stdutil.FuncName(stdutil.PkgName)

func GetCallStacks added in v0.4.2

func GetCallStacks(all bool) []byte

GetCallStacks stacks is a wrapper for runtime. If all is true, Stack that attempts to recover the data for all goroutines.

from glog package

func GetCallerInfo added in v0.5.0

func GetCallerInfo(skip int) string

GetCallerInfo get caller func name and with base filename and line.

returns:

github.com/gookit/goutil/stdutil_test.someFunc2(),stack_test.go:26

func GetCallersInfo added in v0.4.2

func GetCallersInfo(skip, max int) []string

GetCallersInfo returns an array of strings containing the func name, file and line number of each stack frame leading. NOTICE: max should > skip

func Go added in v0.4.2

func Go(f func() error) error

Go is a basic promise implementation: it wraps calls a function in a goroutine and returns a channel which will later return the function's return value.

func GoVersion added in v0.5.5

func GoVersion() string

GoVersion get go runtime version. eg: "1.18.2"

func IsContains added in v0.5.7

func IsContains(data, elem any) bool

IsContains try loop over the data check if the data includes the element.

TIP: only support types: string, map, array, slice

map         - check key exists
string 	    - check sub-string exists
array,slice - check sub-element exists

func IsEmpty added in v0.5.6

func IsEmpty(v any) bool

IsEmpty value check

func IsEqual added in v0.5.7

func IsEqual(src, dst any) bool

IsEqual determines if two objects are considered equal.

TIP: cannot compare function type

func IsFunc added in v0.5.6

func IsFunc(val any) bool

IsFunc value

func IsNil added in v0.5.6

func IsNil(v any) bool

IsNil value check

func MustString

func MustString(v any) string

MustString convert value(basic type) to string, will panic on convert a complex type.

func PanicIf

func PanicIf(err error)

PanicIf if error is not empty

func PanicIfErr

func PanicIfErr(err error)

PanicIfErr if error is not empty

func Panicf

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

Panicf format panic message use fmt.Sprintf

func PkgName added in v0.4.2

func PkgName(fullFcName string) string

PkgName get current package name

Usage:

fullFcName := stdutil.FuncName(fn)
pgkName := stdutil.PkgName(fullFcName)

func SignalHandler added in v0.6.0

func SignalHandler(ctx context.Context, signals ...os.Signal) (execute func() error, interrupt func(error))

SignalHandler returns an actor, i.e. an execute and interrupt func, that terminates with SignalError when the process receives one of the provided signals, or the parent context is canceled.

from https://github.com/oklog/run/blob/master/actors.go

func SimpleCallersInfo added in v0.5.0

func SimpleCallersInfo(skip, num int) []string

SimpleCallersInfo returns an array of strings containing the func name, file and line number of each stack frame leading.

func ToString

func ToString(v any) string

ToString always convert value to string, will ignore error

func TryString

func TryString(v any) (string, error)

TryString try to convert a value to string

func ValueIsEmpty deprecated added in v0.5.2

func ValueIsEmpty(v reflect.Value) bool

ValueIsEmpty reflect value check.

Deprecated: please use reflects.IsEmpty()

func ValueLen deprecated added in v0.5.2

func ValueLen(v reflect.Value) int

ValueLen get reflect value length

Deprecated: please use reflects.Len()

func WaitCloseSignals added in v0.4.2

func WaitCloseSignals(closer io.Closer) error

WaitCloseSignals for some huang program.

Types

type FullFcName added in v0.5.0

type FullFcName struct {
	// FullName eg: github.com/gookit/goutil/stdutil.PanicIf
	FullName string
	// contains filtered or unexported fields
}

FullFcName struct.

func (*FullFcName) FuncName added in v0.5.0

func (ffn *FullFcName) FuncName() string

FuncName get short func name. eg: PanicIf

func (*FullFcName) Parse added in v0.5.0

func (ffn *FullFcName) Parse()

Parse the full func name.

func (*FullFcName) PkgName added in v0.5.0

func (ffn *FullFcName) PkgName() string

PkgName string get. eg: stdutil

func (*FullFcName) PkgPath added in v0.5.0

func (ffn *FullFcName) PkgPath() string

PkgPath string get. eg: github.com/gookit/goutil/stdutil

func (*FullFcName) String added in v0.5.0

func (ffn *FullFcName) String() string

String get full func name string.

type SignalError added in v0.6.0

type SignalError struct {
	Signal os.Signal
}

SignalError is returned by the signal handler's execute function when it terminates due to a received signal.

func (SignalError) Error added in v0.6.0

func (e SignalError) Error() string

Error implements the error interface.

type Value added in v0.5.0

type Value = structs.Value

Value data store, is alias of structs.Value

Jump to

Keyboard shortcuts

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