stdutil

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2023 License: Apache-2.0 Imports: 14 Imported by: 0

README

Std Utils

  • some features

Install

go get github.com/zhangyiming748/pretty/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

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

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

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

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

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

CutFuncName get pkg path and short func name

func DiscardE

func DiscardE(_ error)

DiscardE discard error

func FuncName

func FuncName(fn any) string

FuncName get full func name, contains pkg path.

eg:

// OUTPUT: github.com/zhangyiming748/pretty/stdutil.PanicIf
stdutil.FuncName(stdutil.PkgName)

func GetCallStacks

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

func GetCallerInfo(skip int) string

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

returns:

github.com/zhangyiming748/pretty/stdutil_test.someFunc2(),stack_test.go:26

func GetCallersInfo

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

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

func GoVersion() string

GoVersion get go runtime version. eg: "1.18.2"

func IsContains

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

func IsEmpty(v any) bool

IsEmpty value check

func IsEqual

func IsEqual(src, dst any) bool

IsEqual determines if two objects are considered equal.

TIP: cannot compare function type

func IsFunc

func IsFunc(val any) bool

IsFunc value

func IsNil

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

func PkgName(fullFcName string) string

PkgName get current package name

Usage:

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

func SignalHandler

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

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

func ValueIsEmpty(v reflect.Value) bool

ValueIsEmpty reflect value check.

Deprecated: please use reflects.IsEmpty()

func ValueLen deprecated

func ValueLen(v reflect.Value) int

ValueLen get reflect value length

Deprecated: please use reflects.Len()

func WaitCloseSignals

func WaitCloseSignals(closer io.Closer) error

WaitCloseSignals for some huang program.

Types

type FullFcName

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

FullFcName struct.

func (*FullFcName) FuncName

func (ffn *FullFcName) FuncName() string

FuncName get short func name. eg: PanicIf

func (*FullFcName) Parse

func (ffn *FullFcName) Parse()

Parse the full func name.

func (*FullFcName) PkgName

func (ffn *FullFcName) PkgName() string

PkgName string get. eg: stdutil

func (*FullFcName) PkgPath

func (ffn *FullFcName) PkgPath() string

PkgPath string get. eg: github.com/zhangyiming748/pretty/stdutil

func (*FullFcName) String

func (ffn *FullFcName) String() string

String get full func name string.

type SignalError

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

func (e SignalError) Error() string

Error implements the error interface.

type Value

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