dutil

package
v0.1.9 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2021 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package gutil provides utility functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComparatorByte added in v0.0.9

func ComparatorByte(a, b interface{}) int

ComparatorByte provides a basic comparison on byte.

func ComparatorFloat32 added in v0.0.9

func ComparatorFloat32(a, b interface{}) int

ComparatorFloat32 provides a basic comparison on float32.

func ComparatorFloat64 added in v0.0.9

func ComparatorFloat64(a, b interface{}) int

ComparatorFloat64 provides a basic comparison on float64.

func ComparatorInt added in v0.0.9

func ComparatorInt(a, b interface{}) int

ComparatorInt provides a basic comparison on int.

func ComparatorInt16 added in v0.0.9

func ComparatorInt16(a, b interface{}) int

ComparatorInt16 provides a basic comparison on int16.

func ComparatorInt32 added in v0.0.9

func ComparatorInt32(a, b interface{}) int

ComparatorInt32 provides a basic comparison on int32.

func ComparatorInt64 added in v0.0.9

func ComparatorInt64(a, b interface{}) int

ComparatorInt64 provides a basic comparison on int64.

func ComparatorInt8 added in v0.0.9

func ComparatorInt8(a, b interface{}) int

ComparatorInt8 provides a basic comparison on int8.

func ComparatorRune added in v0.0.9

func ComparatorRune(a, b interface{}) int

ComparatorRune provides a basic comparison on rune.

func ComparatorString

func ComparatorString(a, b interface{}) int

func ComparatorTime added in v0.0.9

func ComparatorTime(a, b interface{}) int

ComparatorTime provides a basic comparison on time.Time.

func ComparatorUint added in v0.0.9

func ComparatorUint(a, b interface{}) int

ComparatorUint provides a basic comparison on uint.

func ComparatorUint16 added in v0.0.9

func ComparatorUint16(a, b interface{}) int

ComparatorUint16 provides a basic comparison on uint16.

func ComparatorUint32 added in v0.0.9

func ComparatorUint32(a, b interface{}) int

ComparatorUint32 provides a basic comparison on uint32.

func ComparatorUint64 added in v0.0.9

func ComparatorUint64(a, b interface{}) int

ComparatorUint64 provides a basic comparison on uint64.

func ComparatorUint8 added in v0.0.9

func ComparatorUint8(a, b interface{}) int

ComparatorUint8 provides a basic comparison on uint8.

func Dump added in v0.0.9

func Dump(i ...interface{})

Dump prints variables <i...> to stdout with more manually readable.

func Export added in v0.0.9

func Export(i ...interface{}) string

Export returns variables <i...> as a string with more manually readable.

func IsEmpty added in v0.0.9

func IsEmpty(value interface{}) bool

IsEmpty checks given `value` empty or not. It returns false if `value` is: integer(0), bool(false), slice/map(len=0), nil; or else returns true.

func ItemValue added in v0.0.9

func ItemValue(item interface{}, key interface{}) (value interface{}, found bool)

ItemValue retrieves and returns its value of which name/attribute specified by `key`. The parameter `item` can be type of map/*map/struct/*struct.

func Keys added in v0.0.9

func Keys(mapOrStruct interface{}) (keysOrAttrs []string)

Keys retrieves and returns the keys from given map or struct.

func ListItemValues added in v0.0.9

func ListItemValues(list interface{}, key interface{}, subKey ...interface{}) (values []interface{})

ListItemValues retrieves and returns the elements of all item struct/map with key `key`. Note that the parameter `list` should be type of slice which contains elements of map or struct, or else it returns an empty slice.

The parameter `list` supports types like: []map[string]interface{} []map[string]sub-map []struct []struct:sub-struct Note that the sub-map/sub-struct makes sense only if the optional parameter `subKey` is given.

func ListItemValuesUnique added in v0.0.9

func ListItemValuesUnique(list interface{}, key string, subKey ...interface{}) []interface{}

ListItemValuesUnique retrieves and returns the unique elements of all struct/map with key `key`. Note that the parameter `list` should be type of slice which contains elements of map or struct, or else it returns an empty slice.

func MapContains added in v0.0.9

func MapContains(data map[string]interface{}, key string) (ok bool)

MapContains checks whether map `data` contains `key`.

func MapContainsPossibleKey added in v0.0.9

func MapContainsPossibleKey(data map[string]interface{}, key string) bool

MapContainsPossibleKey checks if the given `key` is contained in given map `data`. It checks the key ignoring cases and symbols.

Note that this function might be of low performance.

func MapCopy

func MapCopy(data map[string]interface{}) (copy map[string]interface{})

MapCopy 复制map数据到新的map对象

func MapDelete added in v0.0.9

func MapDelete(data map[string]interface{}, keys ...string)

MapDelete deletes all `keys` from map `data`.

func MapMerge added in v0.0.9

func MapMerge(dst map[string]interface{}, src ...map[string]interface{})

MapMerge merges all map from `src` to map `dst`.

func MapMergeCopy added in v0.0.9

func MapMergeCopy(src ...map[string]interface{}) (copy map[string]interface{})

MapMergeCopy creates and returns a new map which merges all map from `src`.

func MapOmitEmpty added in v0.0.9

func MapOmitEmpty(data map[string]interface{})

MapOmitEmpty deletes all empty values from given map.

func MapPossibleItemByKey

func MapPossibleItemByKey(data map[string]interface{}, key string) (foundKey string, foundValue interface{})

MapPossibleItemByKey 忽略map中key值中的符号,只对比字符和数字,如果能匹配,则返回。注意这个方法的性能会比较低

func MapToSlice added in v0.0.9

func MapToSlice(data interface{}) []interface{}

MapToSlice converts map to slice of which all keys and values are its items. Eg: {"K1": "v1", "K2": "v2"} => ["K1", "v1", "K2", "v2"]

func SliceCopy added in v0.0.9

func SliceCopy(data []interface{}) []interface{}

SliceCopy does a shallow copy of slice `data` for most commonly used slice type []interface{}.

func SliceDelete added in v0.0.9

func SliceDelete(data []interface{}, index int) (newSlice []interface{})

SliceDelete deletes an element at `index` and returns the new slice. It does nothing if the given `index` is invalid.

func SliceToMap added in v0.0.9

func SliceToMap(slice interface{}) map[string]interface{}

SliceToMap converts slice type variable `slice` to `map[string]interface{}`. Note that if the length of `slice` is not an even number, it returns nil. Eg: ["K1", "v1", "K2", "v2"] => {"K1": "v1", "K2": "v2"} ["K1", "v1", "K2"] => nil

func SliceToMapWithColumnAsKey added in v0.1.9

func SliceToMapWithColumnAsKey(slice interface{}, key interface{}) map[interface{}]interface{}

SliceToMapWithColumnAsKey converts slice type variable `slice` to `map[interface{}]interface{}` The value of specified column use as the key for returned map. Eg: SliceToMapWithColumnAsKey([{"K1": "v1", "K2": 1}, {"K1": "v2", "K2": 2}], "K1") => {"v1": {"K1": "v1", "K2": 1}, "v2": {"K1": "v2", "K2": 2}} SliceToMapWithColumnAsKey([{"K1": "v1", "K2": 1}, {"K1": "v2", "K2": 2}], "K2") => {1: {"K1": "v1", "K2": 1}, 2: {"K1": "v2", "K2": 2}}

func StructToSlice added in v0.0.9

func StructToSlice(data interface{}) []interface{}

StructToSlice converts struct to slice of which all keys and values are its items. Eg: {"K1": "v1", "K2": "v2"} => ["K1", "v1", "K2", "v2"]

func Throw added in v0.0.9

func Throw(exception interface{})

Throw throws out an exception, which can be caught be TryCatch or recover.

func Try added in v0.0.9

func Try(try func()) (err error)

Try implements try... logistics using internal panic...recover. It returns error if any exception occurs, or else it returns nil.

func TryCatch added in v0.0.9

func TryCatch(try func(), catch ...func(exception error))

TryCatch implements try...catch... logistics using internal panic...recover. It automatically calls function `catch` if any exception occurs ans passes the exception as an error.

func Values added in v0.0.9

func Values(mapOrStruct interface{}) (values []interface{})

Values retrieves and returns the values from given map or struct.

Types

type Comparator

type Comparator func(a, b interface{}) int

Comparator 对比方法 返回的数字表示的含义:

负数 , if a < b
0     , if a == b
正数 , if a > b

Jump to

Keyboard shortcuts

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