goutils

package module
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Feb 11, 2021 License: MIT Imports: 12 Imported by: 9

README

goutils

Common utilities for golang

GoDoc Go Report Card

Documentation

Index

Constants

View Source
const EmptyStr = ""

EmptyStr is the empty string const

View Source
const RegIPv4 = "(([2][5][0-5]|[2][0-4][0-9]|[1][0-9]{2}|[1-9][0-9]|[0-9])[.]){3}([2][5][0-5]|[2][0-4][0-9]|[1][0-9]{2}|[1-9][0-9]|[0-9])"

RegIPv4 is the IPv4 Regexp

Variables

This section is empty.

Functions

func Between

func Between(s, from, to string) string

Between return the middle part of a string from string "from" to string "to"

func Contains

func Contains(obj interface{}, v ...interface{}) bool

Contains tells an object is in a list or not

func ContainsInt added in v0.6.0

func ContainsInt(obj int, v ...int) bool

ContainsInt tells an integer is in a list or not

func ContainsString added in v0.6.0

func ContainsString(obj string, v ...string) bool

ContainsString tells a string is in a list or not

func CopyMapSS added in v0.6.0

func CopyMapSS(m map[string]string) map[string]string

CopyMapSS makes a shallow copy of a map.

func DeepContains

func DeepContains(obj interface{}, v ...interface{}) bool

DeepContains tells an object is in a list or not, but uses reflect.DeepEqual to determine the equality

func DefaultIfNil added in v0.9.0

func DefaultIfNil(obj, dv interface{}) interface{}

DefaultIfNil return dv if obj is nil

func Download

func Download(url string, dest string) (err error)

Download is used to conveniently download a file to the certain path

func FileSize added in v0.3.0

func FileSize(s uint64) string

FileSize translate bytes number into human-readable size

func Filter added in v0.7.0

func Filter(slice, fn interface{}) interface{}

Filter a slice with fn

func FilterBlankString added in v0.2.0

func FilterBlankString(vs []string) []string

FilterBlankString removes blank strings in a string slice

func FilterEmptyString added in v0.2.0

func FilterEmptyString(vs []string) []string

FilterEmptyString removes empty strings in a string slice

func FilterInPlace added in v0.7.0

func FilterInPlace(slicePtr, fn interface{})

FilterInPlace a slice with fn in-place

func FilterInt added in v0.2.0

func FilterInt(vs []int, f func(int) bool) []int

FilterInt filters an int slice with the function returns false

func FilterString added in v0.2.0

func FilterString(vs []string, f func(string) bool) []string

FilterString filters a string slice with the function returns false

func HumanReadableDuration added in v0.3.0

func HumanReadableDuration(diffSeconds int64) string

HumanReadableDuration translates seconds into human-readable text

func Indent added in v0.5.0

func Indent(s, prefix string) string

Indent inserts prefix at the beginning of each non-empty line of s. The end-of-line marker is NL.

func IndentBytes added in v0.5.0

func IndentBytes(b, prefix []byte) []byte

IndentBytes inserts prefix at the beginning of each non-empty line of b. The end-of-line marker is NL.

func Index

func Index(s, substr string) int

Index return the location of a string in another long string, if it doesn't exist, returns -1 this function supports CJK characters

func IsBlank

func IsBlank(s string) bool

IsBlank tells a string is blank or not, a string with all white blanks would be considered as true

func IsDir

func IsDir(path string) bool

IsDir tells a path is a directory or not

func IsEmpty

func IsEmpty(s string) bool

IsEmpty tells a string is empty or not

func IsExist

func IsExist(path string) bool

IsExist is used to determine whether a path exists or not

func IsFile

func IsFile(path string) bool

IsFile tells a path is a file or not

func IsIPv4 added in v0.2.0

func IsIPv4(s string) bool

IsIPv4 tells a string matches IPv4 form or not

func IsNil added in v0.9.0

func IsNil(object interface{}) bool

IsNil checks if a specified object is nil or not, without Failing.

func IsNotBlank

func IsNotBlank(s string) bool

IsNotBlank tells a string is not blank or is

func IsNotEmpty

func IsNotEmpty(s string) bool

IsNotEmpty tells a string is not empty or is

func IsValueNil added in v0.9.0

func IsValueNil(value reflect.Value) bool

IsValueNil returns if reflect.Value is nil. panics when value is invalid.

func LeftPad

func LeftPad(s string, padStr string, length int) string

LeftPad pad a string with specified character to the left side

func Len

func Len(s string) int

Len returns the length of a string using rune, it's useful when getting the length of a string including CJK characters

func ListAllEnvs added in v0.6.0

func ListAllEnvs() map[string]string

ListAllEnvs list all envs in map type

func MakeRange added in v0.7.0

func MakeRange(min, max int) []int

MakeRange returns a slice starts from min and ends with max

func Map added in v0.2.0

func Map(vs []string, f func(string) string) []string

Map maps a string slice with a function

func MergeMapSS added in v0.6.0

func MergeMapSS(base map[string]string, overrides ...map[string]string) map[string]string

MergeMapSS merges multiple maps into one

func PanicError added in v0.7.0

func PanicError(err error)

PanicError panic if err is not nil

func Reduce added in v0.7.0

func Reduce(slice, pairFunc, zero interface{}) interface{}

Reduce a alice with

func RightPad

func RightPad(s string, padStr string, length int) string

RightPad pad a string with specified character to the right side

func Round added in v0.8.0

func Round(val float64, precision int) float64

Round round half up For example:

Round(0.363636, 3)  // 0.364
Round(0.363636, 2)  // 0.36
Round(0.363636, 1)  // 0.4
Round(32, 1)        // 30

func Ternary added in v0.9.0

func Ternary(condition bool, trueExpr, falseExpr interface{}) interface{}

Ternary the common (condition ? trueExpr : falseExpr) expression in C

func ToBool added in v0.4.0

func ToBool(s string) bool

ToBool returns the boolean value represented by the string, default: false

func ToBoolDv added in v0.4.0

func ToBoolDv(s string, dv bool) bool

ToBoolDv returns the boolean value represented by the string with a default bool value

func ToInt

func ToInt(s string) int

ToInt safely parse a string to a integer with a default value 0 when parse fails

func ToIntDv

func ToIntDv(s string, dv int) int

ToIntDv safely parse a string to a integer with a specified default value

func Transform added in v0.7.0

func Transform(slice, fn interface{}) interface{}

Transform map a slice with fn

func TransformInPlace added in v0.7.0

func TransformInPlace(slice, fn interface{}) interface{}

TransformInPlace map a slice with fn in-place

func Trim

func Trim(str string) string

Trim cuts the blanks of a string, the beginning and the end

func TrimLeft

func TrimLeft(str string) string

TrimLeft cuts the left side blanks of a string

func TrimRight

func TrimRight(str string) string

TrimRight cuts the right side blanks of a string

func UUID

func UUID() string

UUID returns a random generated UUID string

func WildcardMatch added in v0.3.0

func WildcardMatch(pattern, name string) (matched bool)

WildcardMatch - finds whether the text matches/satisfies the pattern string. supports '*' and '?' wildcards in the pattern string. unlike path.Match(), considers a path as a flat name space while matching the pattern. The difference is illustrated in the example here https://play.golang.org/p/Ega9qgD4Qz .

func WildcardMatchSimple added in v0.3.0

func WildcardMatchSimple(pattern, name string) bool

WildcardMatchSimple - finds whether the text matches/satisfies the pattern string. supports only '*' wildcard in the pattern. considers a file system path as a flat name space.

func ZeroFill

func ZeroFill(s string, length int) string

ZeroFill pad a string(usually a number string) with "0" to the left

Types

type SysStatus added in v0.3.0

type SysStatus struct {
	Uptime       string
	NumGoroutine int

	// General statistics.
	MemAllocated string // bytes allocated and still in use
	MemTotal     string // bytes allocated (even if freed)
	MemSys       string // bytes obtained from system (sum of XxxSys below)
	Lookups      uint64 // number of pointer lookups
	MemMallocs   uint64 // number of mallocs
	MemFrees     uint64 // number of frees

	// Main allocation heap statistics.
	HeapAlloc    string // bytes allocated and still in use
	HeapSys      string // bytes obtained from system
	HeapIdle     string // bytes in idle spans
	HeapInuse    string // bytes in non-idle span
	HeapReleased string // bytes released to the OS
	HeapObjects  uint64 // total number of allocated objects

	// Low-level fixed-size structure allocator statistics.
	//	Inuse is bytes used now.
	//	Sys is bytes obtained from system.
	StackInuse  string // bootstrap stacks
	StackSys    string
	MSpanInuse  string // mspan structures
	MSpanSys    string
	MCacheInuse string // mcache structures
	MCacheSys   string
	BuckHashSys string // profiling bucket hash table
	GCSys       string // GC metadata
	OtherSys    string // other system allocations

	// Garbage collector statistics.
	NextGC       string // next run in HeapAlloc time (bytes)
	LastGC       string // last run in absolute time (ns)
	PauseTotalNs string
	PauseNs      string // circular buffer of recent GC pause times, most recent at [(NumGC+255)%256]
	NumGC        uint32
}

SysStatus describes system runtime environment

func GetSysStatus added in v0.3.0

func GetSysStatus() *SysStatus

GetSysStatus returns runtime information

Jump to

Keyboard shortcuts

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