utils

package
v0.60.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2020 License: MIT Imports: 22 Imported by: 136

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All added in v0.52.0

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

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

func DefaultBackoff added in v0.52.0

func DefaultBackoff(interval time.Duration) time.Duration

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

func Dump added in v0.54.0

func Dump(list ...interface{})

Dump values to logger

func E

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

E if the last arg is error, panic it

func Exec added in v0.52.0

func Exec(name string, args ...string)

Exec command

func FileExists added in v0.52.0

func FileExists(path string) bool

FileExists checks if file exists, only for file, not for dir

func IsSyncMapEmpty added in v0.59.0

func IsSyncMapEmpty(s *sync.Map) bool

IsSyncMapEmpty helper

func Mkdir added in v0.52.0

func Mkdir(path string) error

Mkdir makes dir recursively

func MustReadBytes added in v0.52.0

func MustReadBytes(r io.Reader) []byte

MustReadBytes from reader

func MustReadJSON added in v0.52.0

func MustReadJSON(r io.Reader) gjson.Result

MustReadJSON from reader

func MustReadString added in v0.52.0

func MustReadString(r io.Reader) string

MustReadString from reader

func MustToJSON added in v0.52.0

func MustToJSON(data interface{}) string

MustToJSON encode data to json string

func MustToJSONBytes added in v0.52.0

func MustToJSONBytes(data interface{}) []byte

MustToJSONBytes encode data to json bytes

func OutputFile added in v0.52.0

func OutputFile(p string, data interface{}) error

OutputFile auto creates file if not exists, it will try to detect the data type and auto output binary, string or json

func Pause added in v0.52.0

func Pause()

Pause the goroutine forever

func RandString added in v0.52.0

func RandString(len int) string

RandString generate random string with specified string length

func ReadJSON added in v0.52.0

func ReadJSON(r io.Reader) (gjson.Result, error)

ReadJSON from reader

func ReadJSONPathAsString added in v0.54.0

func ReadJSONPathAsString(r io.Reader, path string) (string, error)

ReadJSONPathAsString from reader

func ReadString added in v0.52.0

func ReadString(p string) (string, error)

ReadString reads file as string

func Retry added in v0.52.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 added in v0.52.0

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

S Template render, the params is key-value pairs

func SDump added in v0.52.0

func SDump(v interface{}) string

SDump a value

func Serve added in v0.52.0

func Serve(host string) (string, *http.ServeMux, func())

Serve a port, if host is empty a random port will be used.

func SetCmdStdPipe added in v0.54.0

func SetCmdStdPipe(cmd *exec.Cmd)

SetCmdStdPipe command

func Sleep added in v0.52.0

func Sleep(seconds float64)

Sleep the goroutine for specified seconds, such as 2.3 seconds

func SyncMapToMap added in v0.59.0

func SyncMapToMap(s *sync.Map) map[string]interface{}

SyncMapToMap convertor

Types

type Nil added in v0.52.0

type Nil struct{}

Nil type

type Sleeper added in v0.52.0

type Sleeper func(context.Context) error

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

func BackoffSleeper added in v0.52.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.52.0

func CountSleeper(max int) Sleeper

CountSleeper wake when counts to max and return

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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