Documentation
¶
Index ¶
- Constants
- Variables
- func A2B(val interface{}) []byte
- func AddFileIfNotExisted(name, data string) string
- func AnyJoin(sep string, args ...interface{}) string
- func AnyJoinNon0(sep string, args ...interface{}) string
- func AnyJoinNon0ToUpper(sep string, args ...interface{}) string
- func AorB[T General](a, b T) T
- func ArrA2B(fields ...interface{}) [][]byte
- func ArrB2A(args ...[]byte) []interface{}
- func ArrB2S(args ...[]byte) []string
- func ArrS2B(fields ...string) [][]byte
- func B2A(v []byte, args ...interface{}) interface{}
- func BindOpts(opt *WArgs, opts ...WArgFunc)
- func Bytes2GB(n uint64) float64
- func Bytes2MB(n uint64) float64
- func Caller(skip int) (string, int)
- func CallerStack() []byte
- func CatchPanicAsErr(fn func()) (err error)
- func CeilInt(a, b int) int
- func CharToNum(s string, opts ...NumOptFunc) (v interface{}, e error)
- func ConcatSlice[T General](a []T, b []T) []T
- func DecodeUrl(baseurl string) url.Values
- func DecodeUrlToMap(baseurl string) (got map[string]string)
- func DecodeUrlToMapIface(baseurl string) (got map[string]interface{})
- func DefaultIfNil(value interface{}, defaultValue interface{}) interface{}
- func DiffRate(a, b int) float64
- func DummyErrorLog(msg ...interface{})deprecated
- func DummyLog(msg ...interface{})
- func DumpCallerStack()
- func DumpCallerWithKey(args ...string)
- func Duration(msg string, start time.Time)
- func ElapsedSeconds(start time.Time, args ...int) float64
- func EncodeUrl(baseurl string, kv map[string]interface{}, pathArgs ...string) *url.URL
- func EnsureByRetry(fn func() error, args ...int) (tried int, err error)
- func F64KMFromStr(str string, opts ...NumOptFunc) (i float64, err error)
- func FileGetGz(filename string) ([]byte, error)
- func FirstIface(args ...interface{}) interface{}
- func FirstNonNil(values ...interface{}) interface{}
- func FirstNonZero[T General](args ...T) T
- func FirstOrDefaultArgs[T General](dft T, args ...T) (val T)
- func GbkToUtf8(s []byte) ([]byte, error)
- func GetAllTags(structIn interface{}, tag string) (tags []string)
- func GetAttr(o interface{}, key string) reflect.Value
- func GetCharNumOr(s string, opts ...NumOptFunc) (v interface{})
- func GetHostPublicIp(url string) string
- func GetIntByIndex(arr []int, index int) int
- func GetKind(o interface{}) reflect.Kind
- func GetLocalIpAddr() (ip, port string)
- func GetPublicIp(args ...string) (ip string)
- func GetRangeByStep(index, step, total int) (start, end int)
- func GetStrByIndex(arr []string, index int) string
- func GetStrBySplit(raw string, sep string, offset int) (string, bool)
- func GetStrBySplitAtIndex(raw interface{}, sep string, index int) string
- func GetUrlParam(baseurl string, key string) (bool, string)
- func GetenvBool(key string) (bool, error)
- func GetenvBoolWithDft(key string, dft bool) bool
- func GetenvInt(key string) (int, error)
- func GetenvIntWithDft(key string, dft int) int
- func GetenvStr(key string) (string, error)
- func GzipBytes(raw []byte) (*bytes.Buffer, error)
- func HostUUID() string
- func IfThen(condition bool, a interface{}) interface{}
- func IfThenElse(condition bool, a interface{}, b interface{}) interface{}
- func IfThenElseStr(condition bool, a, b string) string
- func IfaceAorB(a, b interface{}) interface{}
- func InitializeXOpts(opts ...XOptFunc)
- func Insert[T General](arr []T, index int, value T) []T
- func IntKMFromStr(str string, opts ...NumOptFunc) (int, bool)
- func IsDefaultVal(x interface{}) bool
- func IsGzip(filename string) bool
- func IsJSON(str string) bool
- func IsZeroVal(x interface{}) bool
- func KillProcess(name string) error
- func LogFatalIfErr(err error, msg string)
- func LogIfErr(err error, msg string) bool
- func Map2StructureByDefault(dat interface{}, dstStruct interface{})
- func Map2StructureLoosely(dat interface{}, config *mapstructure.DecoderConfig)
- func Max[T Number](args ...T) (max T)
- func Min[T Number](args ...T) (min T)
- func MkdirIfNotExist(dirPathName string) (b bool, dirAbsPath string)
- func MkdirIfNotExistFromFile(filePath string) error
- func MustCharToNum(s string, opts ...NumOptFunc) (v interface{})
- func MustF64KMFromStr(str string, opts ...NumOptFunc) float64
- func MustFileAppendString(filename, data string)
- func MustFileSetString(filename, data string)
- func MustGetKeys(obj interface{}) []string
- func MustGetStrBySplit(raw string, sep string, offset int) string
- func MustGzipBytes(raw []byte) []byte
- func MustGzipBytesToStr(raw []byte) string
- func MustIntKMFromStr(str string, opts ...NumOptFunc) int
- func MustMarshal(data interface{}) []byte
- func MustReadFile(filename string) (raw []byte)
- func MustStrToGzip(raw string) []byte
- func MustStrToGzipStr(raw string) string
- func MustString(obj interface{}) string
- func MustStringify(data interface{}) string
- func MustStruct2Yaml(dat interface{}) (out string)
- func MustStructToMap(data interface{}) map[string]interface{}
- func MustStructify(data string, op interface{})
- func MustUnGzip(filename string) string
- func MustUnGzipWithSameName(filename string) string
- func MustUnGzipWithoutGz(filename string) string
- func MustWriteFile(name, data string)
- func MustYaml2Struct(raw []byte, st interface{})
- func NewChaos(key, iv string) *chaos
- func NewChromeExtension(line, savePath string) (string, string, string)
- func NewSlice(start, end, step int) []int
- func NewSliceByCount(start, count, step int) []int
- func NewStringSlice(raw string, fixStep int, randomStep ...bool) []string
- func PanicIfErr(args ...interface{})
- func PanicIfErrWithPause(args ...interface{})
- func Pause(args ...string)
- func PauseToDebug(msg ...string) bool
- func PlainTablelize(s *csv.Reader)
- func PrintArrayAsTable(header []string, rows [][]string)
- func PrintStrAsTable(header string, rows []string, sep string)
- func QuietGetKeys(obj interface{}) []string
- func RandFloatX1k(min, max float64) int
- func RandSleep(min, max float64, msg ...string) int
- func ReadFile(filename string) (raw []byte, err error)
- func RealTimeInfo() map[string]interface{}
- func RecoverAndDumpOnly()
- func RecoverWithCb(fn func())
- func RefineIndex(i, length int) int
- func RefineString(name string) string
- func RefineWinFileName(name string) string
- func Round(num float64) int
- func RunCommandInBash(command string) (string, string, error)
- func SetAttr(o interface{}, key string, val interface{})
- func SliceToTable(header *[]string, data *[][]string)
- func SplitSlice[T any](lines []T, step int) (got [][]T)
- func StrToArrWithNonEmpty(raw string, sep string) (arr []string)
- func StrToGzip(raw string) (*bytes.Buffer, error)
- func Stringify(data interface{}) (string, error)
- func Structify(data string, value interface{}) error
- func Tablelize(s *csv.Reader)
- func ToFixed(num float64, precision ...int) float64
- func ToFixedStr(num float64, precision ...int) string
- func Track(msg string) (string, time.Time)
- func TruncateString(s string, max int) string
- func TruncateText(s string, max int) string
- func TruncateWord(s string, max int) string
- func Utf8ToGbk(s []byte) ([]byte, error)
- func Walk(root string, opts ...WArgFunc) []string
- func WriteFile(name string, data string) error
- func Yaml2Config(raw []byte, cfs ...*config.Config) (cf *config.Config, err error)
- type Any
- type ErrTry
- type Floats
- type General
- type Ints
- type NumOptFunc
- type NumOpts
- type Number
- type PsMetric
- type UInts
- type WArgFunc
- type WArgs
- type XOptFunc
- type XOpts
Constants ¶
const MaxInt = int(MaxUint >> 1)
const MaxUint = ^uint(0)
const MinInt = -MaxInt - 1
const MinUint = 0
const ShellToUse = "bash"
Variables ¶
var ( Green = xpretty.Green Red = xpretty.Red )
var ErrEnvVarEmpty = errors.New("getenv: environment variable empty")
var ErrorNoNumbers = errors.New("no number found")
Functions ¶
func AddFileIfNotExisted ¶
func AnyJoin ¶
AnyJoin: join all args with sep, even value in args is zero if you don't want zero values, use `AnyJoinNon0` instead
func AnyJoinNon0 ¶
AnyJoinNon0: all zero valid will be skipped but " " is allowed
func ArrA2B ¶
func ArrA2B(fields ...interface{}) [][]byte
ArrA2B
convert interface array to byte array
func ArrB2A ¶
func ArrB2A(args ...[]byte) []interface{}
ArrB2A
convert byte array to interface array
func Caller ¶
Caller wraps runtime.Caller and returns file and line number information
Returns: filename, linenum
func CallerStack ¶
func CallerStack() []byte
func CatchPanicAsErr ¶
func CatchPanicAsErr(fn func()) (err error)
CatchPanicAsErr
try fn with recover, return the panic as error
func CharToNum ¶
func CharToNum(s string, opts ...NumOptFunc) (v interface{}, e error)
CharToNum: substract `number+Chars` from source str returns int by default
func ConcatSlice ¶
func ConcatSlice[T General](a []T, b []T) []T
func DecodeUrlToMap ¶
func DecodeUrlToMapIface ¶
func DefaultIfNil ¶
func DefaultIfNil(value interface{}, defaultValue interface{}) interface{}
DefaultIfNil checks if the value is nil, if true returns the default value otherwise the original
func DummyErrorLog
deprecated
func DummyErrorLog(msg ...interface{})
DummyErrorLog will print a dummy log with red bg
Deprecated: use xpretty.DummyErrorLog instead
func DummyLog ¶
func DummyLog(msg ...interface{})
DummyLog will print a dummy log with green bg Deprecated: use xpretty.DummyLog instead
func DumpCallerStack ¶
func DumpCallerStack()
func DumpCallerWithKey ¶
func DumpCallerWithKey(args ...string)
DumpCallerStack
print the caller tree
func EnsureByRetry ¶
EnsureByRetry
Params:
- fn
- args: tries, delay, showLogOrNot
wrapper of retry.Do with default - total try 3 times
- overwritten by args[0]
- WARN: if args[0] == 0, the "fn" will be skipped
- if args[0] < 0, will use default 3
- retry delay of 0 millisecond
- overwritten by args[1]
- on retry not print logs
- overwritten by args[2]
func F64KMFromStr ¶
func F64KMFromStr(str string, opts ...NumOptFunc) (i float64, err error)
func FirstIface ¶
func FirstIface(args ...interface{}) interface{}
func FirstNonNil ¶
func FirstNonNil(values ...interface{}) interface{}
FirstNonNil returns the first non nil parameter
func FirstNonZero ¶
func FirstNonZero[T General](args ...T) T
FirstNonZero returns the first non zero values, and if all are zero vlaues, return the first value in args
func FirstOrDefaultArgs ¶
func FirstOrDefaultArgs[T General](dft T, args ...T) (val T)
FirstOrDefaultArgs
return the first args value, if args not empty else return default value
func GetAllTags ¶
func GetCharNumOr ¶
func GetCharNumOr(s string, opts ...NumOptFunc) (v interface{})
GetCharNumOr returns parsed number or keep original
func GetHostPublicIp ¶
func GetPublicIp ¶
func GetRangeByStep ¶
GetRangeByStep return range<index*step, (index+1)*step>
func GetStrBySplit ¶
GetStrBySplit: split raw str with separator and join from offset
example: raw = "a,b,c,d,e" v, b := GetStrBySplit(raw, ",", 1) // v = "bcde", b = true v, b := GetStrBySplit(raw, "_", 1) // v = "a,b,c,d,e", b = false v, b := GetStrBySplit(raw, ",", -1) // v = "e", b = false
@return string @return bool
func GetStrBySplitAtIndex ¶
GetStrBySplitAtIndex split raw to slice and then return element at index
- if sep not in raw, returns raw
- if index < 0, reset index to len() + index
- if index > total length, returns the last one
- else returns element at index
func GetenvBool ¶
func GetenvBoolWithDft ¶
func GetenvIntWithDft ¶
func IfThen ¶
func IfThen(condition bool, a interface{}) interface{}
IfThen evaluates a condition, if true returns the parameters otherwise nil
func IfThenElse ¶
func IfThenElse(condition bool, a interface{}, b interface{}) interface{}
IfThenElse evaluates a condition, if true returns the first parameter otherwise the second
func IfThenElseStr ¶
IfThenElse evaluates a condition, if true returns the first parameter otherwise the second
func InitializeXOpts ¶
func InitializeXOpts(opts ...XOptFunc)
InitializeXOpts setups
- debug: used in `PauseToDebug`
- recover: used in `RecoverAndDumpOnly`
- dummyLog: used in `DummyLog`
- pauseInPanic: use in `PanicIfErr`
func Insert ¶
Insert insert value to arr at index
- if index >= len(arr), append to arr
- else insert at index
func IntKMFromStr ¶
func IntKMFromStr(str string, opts ...NumOptFunc) (int, bool)
func KillProcess ¶
func LogFatalIfErr ¶
LogFatalIfErr logs error with level fatal if `err!=nil`, else do nothing
func LogIfErr ¶
LogIfErr logs error with level error, and return true if err!=nil, else false
@return bool
func Map2StructureByDefault ¶
func Map2StructureByDefault(dat interface{}, dstStruct interface{})
Map2StructureByDefault
just a wrapper of commonly usage to mapstructure.DecoderConfig
func Map2StructureLoosely ¶
func Map2StructureLoosely(dat interface{}, config *mapstructure.DecoderConfig)
Map2StructureLoosely wrapper of mapstructure.NewDecoder
this will convert map[interface{}]interface{} to struct WARN: remember to pass struct in config.Result e.g.
WeaklyTypedInput: true, Result: &st, TagName: "structs", // not required, but you can always customize it
func MkdirIfNotExist ¶
func MkdirIfNotExistFromFile ¶
func MustCharToNum ¶
func MustCharToNum(s string, opts ...NumOptFunc) (v interface{})
func MustF64KMFromStr ¶
func MustF64KMFromStr(str string, opts ...NumOptFunc) float64
func MustFileAppendString ¶
func MustFileAppendString(filename, data string)
func MustFileSetString ¶
func MustFileSetString(filename, data string)
func MustGetKeys ¶
func MustGetKeys(obj interface{}) []string
func MustGetStrBySplit ¶
MustGetStrBySplit get str or ""
func MustGzipBytes ¶
func MustGzipBytesToStr ¶
func MustIntKMFromStr ¶
func MustIntKMFromStr(str string, opts ...NumOptFunc) int
func MustMarshal ¶
func MustMarshal(data interface{}) []byte
func MustReadFile ¶
func MustStrToGzip ¶
func MustStrToGzipStr ¶
func MustString ¶
func MustString(obj interface{}) string
func MustStringify ¶
func MustStringify(data interface{}) string
func MustStruct2Yaml ¶
func MustStruct2Yaml(dat interface{}) (out string)
func MustStructToMap ¶
func MustStructToMap(data interface{}) map[string]interface{}
func MustStructify ¶
func MustStructify(data string, op interface{})
func MustUnGzip ¶
func MustUnGzipWithSameName ¶
func MustUnGzipWithoutGz ¶
func MustWriteFile ¶
func MustWriteFile(name, data string)
func MustYaml2Struct ¶
func MustYaml2Struct(raw []byte, st interface{})
func NewChaos ¶
func NewChaos(key, iv string) *chaos
NewChaos creates cipher with mode CBC, padding PKCS7
@return *chaos
func NewChromeExtension ¶
NewChromeExtension will create two files from line, and save to savePath
- background.js
- manifest.json
line format is: "host:port:username:password:<OTHER>"
func NewSliceByCount ¶
func NewStringSlice ¶
NewStringSlice
raw: the raw string to be convert to slice fixStep: how many chars in each slice args: just add any one to enable random step mode
return:
str slice with each has (~)maxLen chars
func PanicIfErrWithPause ¶
func PanicIfErrWithPause(args ...interface{})
func PauseToDebug ¶
PauseToDebug block the normal workflow, and used for debug purpose only
this is triggered by ctrl.debug = true
func PlainTablelize ¶
func PrintArrayAsTable ¶
func PrintStrAsTable ¶
func QuietGetKeys ¶
func QuietGetKeys(obj interface{}) []string
func RandFloatX1k ¶
RandFloatX1k: return a value between (min, max) * 1000
func ReadFile ¶
ReadFile read content from file, support both plain file and gzip file
@param filename @return raw @return err
func RealTimeInfo ¶
func RealTimeInfo() map[string]interface{}
func RecoverAndDumpOnly ¶
func RecoverAndDumpOnly()
func RecoverWithCb ¶
func RecoverWithCb(fn func())
func RefineIndex ¶
RefineIndex
- 0 <= i < length return i
- i < 0 return length+i
- i >= length return length-1
func RefineString ¶
RefineString replaces chars those not allowed in Windows `\/:*?"<>|`
func RefineWinFileName ¶
func SetAttr ¶
func SetAttr(o interface{}, key string, val interface{})
SetAttr will set o.key to val same as GetAttr, o should be a pointer
func SliceToTable ¶
func SplitSlice ¶
func StrToArrWithNonEmpty ¶
func ToFixedStr ¶
func TruncateText ¶
func TruncateWord ¶
func Yaml2Config ¶
Types ¶
type NumOptFunc ¶
type NumOptFunc func(o *NumOpts)
func Chars ¶
func Chars(s string) NumOptFunc
func Dft ¶
func Dft(i interface{}) NumOptFunc
type PsMetric ¶
type PsMetric struct { // host info Uptime uint64 `json:"uptime,omitempty"` Hostname string `json:"hostname,omitempty"` OS string `json:"os,omitempty"` HostId string `json:"host_id"` // mem Total float64 `json:"total,omitempty"` UsedPercent float64 `json:"used_percent,omitempty"` // cpu Cores int32 `json:"cores,omitempty"` ModelName string `json:"model_name,omitempty"` // load Load1 float64 `json:"load1,omitempty"` Load5 float64 `json:"load5,omitempty"` Load15 float64 `json:"load15,omitempty"` }
func NewPsMetric ¶
func NewPsMetric() *PsMetric
func (*PsMetric) Mem ¶
func (p *PsMetric) Mem() *mem.VirtualMemoryStat
type XOptFunc ¶
type XOptFunc func(o *XOpts)