utils

package
v0.25.5 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2020 License: MIT Imports: 21 Imported by: 3

Documentation

Index

Constants

View Source
const Version = "v0.25.5"

Version version the project

Variables

View Source
var ErrMaxSleepCount = errors.New("max sleep count")

ErrMaxSleepCount ...

View Source
var Stderr = stderr()

Stderr ...

View Source
var Stdout = stdout()

Stdout ...

Functions

func All

func All(actions ...func()) func()

All run all actions concurrently, returns the wait function for all actions.

func C

func C(str interface{}, color string) string

C color terminal string

func ClearScreen

func ClearScreen() error

ClearScreen ...

func DefaultBackoff added in v0.21.0

func DefaultBackoff(interval time.Duration) time.Duration

DefaultBackoff algorithm: A(n) = A(n-1) * random[1.9, 2.1)

func Dump

func Dump(val interface{})

Dump ...

func E

func E(args ...interface{}) []interface{}

E if the last arg is error, panic it

func E1

func E1(arg interface{}, err error) interface{}

E1 if the second arg is error panic it, or return the first arg

func Err

func Err(v ...interface{})

Err log to stderr with timestamp and stack trace

func ErrArg

func ErrArg(args ...interface{}) error

ErrArg get the last arg as error

func Log

func Log(v ...interface{})

Log log to stdout with timestamp

func MustToJSON added in v0.16.3

func MustToJSON(data interface{}) string

MustToJSON encode data to json string

func MustToJSONBytes added in v0.16.3

func MustToJSONBytes(data interface{}) []byte

MustToJSONBytes encode data to json bytes

func Noop

func Noop(_ ...interface{})

Noop swallow all args and do nothing

func Pause added in v0.20.5

func Pause()

Pause the goroutine forever

func RandBytes

func RandBytes(len int) []byte

RandBytes generate random bytes with specified byte length

func RandString

func RandString(len int) string

RandString generate random string with specified string length

func Retry added in v0.21.0

func Retry(ctx context.Context, s Sleeper, fn func() (stop bool, err error)) error

Retry fn and sleeper until fn returns true or s returns error

func S

func S(tpl string, params ...interface{}) string

S Template render, the params is key-value pairs

func Sdump

func Sdump(val interface{}) string

Sdump ...

func Sleep added in v0.20.4

func Sleep(seconds float64)

Sleep the goroutine for specified seconds, such as 2.3 seconds

func Try

func Try(fn func()) (err interface{})

Try try fn with recover, return the panic as value

Types

type ErrInjector added in v0.21.1

type ErrInjector struct {
	// contains filtered or unexported fields
}

ErrInjector let you easily mock error for testing

func (*ErrInjector) CountInject added in v0.21.1

func (e *ErrInjector) CountInject(times int, err error)

CountInject inject err after E is called with specified times

func (*ErrInjector) E added in v0.21.1

func (e *ErrInjector) E(err error) error

E inject error

func (*ErrInjector) Inject added in v0.21.1

func (e *ErrInjector) Inject(fn func(error) error)

Inject the fn and enable the enjection, call it with nil to disable injection

type JSONResult added in v0.17.0

type JSONResult = *gjson.Result

JSONResult shortcut for gjson.Result

func JSON

func JSON(data interface{}) JSONResult

JSON parse json for easily access the value from json path

type Nil

type Nil struct{}

Nil used to create empty channel

type Sleeper added in v0.21.0

type Sleeper func(ctx context.Context) error

Sleeper sleeps for sometime, returns the reason to wake, if ctx is done release resource

func BackoffSleeper added in v0.21.0

func BackoffSleeper(init, maxInterval time.Duration, algorithm func(time.Duration) time.Duration) Sleeper

BackoffSleeper returns a sleeper that sleeps in a backoff manner every time get called. If algorithm is nil, DefaultBackoff will be used. Set interval and maxInterval to the same value to make it a constant interval sleeper. If maxInterval is not greater than 0, it will wake immediately.

func CountSleeper added in v0.21.0

func CountSleeper(max int) Sleeper

CountSleeper wake when counts to max and return

func MergeSleepers added in v0.21.0

func MergeSleepers(list ...Sleeper) Sleeper

MergeSleepers into one, wakes when first sleeper wakes. Such as you want to poll to check if wifi is connected, when you poll you also want to do do the check whenever wifi driver is enabled, then you can merge BackoffSleeper and ChannelSleeper to achieve it.

Jump to

Keyboard shortcuts

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