gutil

package
v1.16.9 Latest Latest
Warning

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

Go to latest
Published: May 30, 2022 License: MIT Imports: 9 Imported by: 49

Documentation

Overview

Package gutil provides utility functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ComparatorByte

func ComparatorByte(a, b interface{}) int

ComparatorByte provides a basic comparison on byte.

func ComparatorFloat32

func ComparatorFloat32(a, b interface{}) int

ComparatorFloat32 provides a basic comparison on float32.

func ComparatorFloat64

func ComparatorFloat64(a, b interface{}) int

ComparatorFloat64 provides a basic comparison on float64.

func ComparatorInt

func ComparatorInt(a, b interface{}) int

ComparatorInt provides a basic comparison on int.

func ComparatorInt16

func ComparatorInt16(a, b interface{}) int

ComparatorInt16 provides a basic comparison on int16.

func ComparatorInt32

func ComparatorInt32(a, b interface{}) int

ComparatorInt32 provides a basic comparison on int32.

func ComparatorInt64

func ComparatorInt64(a, b interface{}) int

ComparatorInt64 provides a basic comparison on int64.

func ComparatorInt8

func ComparatorInt8(a, b interface{}) int

ComparatorInt8 provides a basic comparison on int8.

func ComparatorRune

func ComparatorRune(a, b interface{}) int

ComparatorRune provides a basic comparison on rune.

func ComparatorString

func ComparatorString(a, b interface{}) int

ComparatorString provides a fast comparison on strings.

func ComparatorTime

func ComparatorTime(a, b interface{}) int

ComparatorTime provides a basic comparison on time.Time.

func ComparatorUint

func ComparatorUint(a, b interface{}) int

ComparatorUint provides a basic comparison on uint.

func ComparatorUint16

func ComparatorUint16(a, b interface{}) int

ComparatorUint16 provides a basic comparison on uint16.

func ComparatorUint32

func ComparatorUint32(a, b interface{}) int

ComparatorUint32 provides a basic comparison on uint32.

func ComparatorUint64

func ComparatorUint64(a, b interface{}) int

ComparatorUint64 provides a basic comparison on uint64.

func ComparatorUint8

func ComparatorUint8(a, b interface{}) int

ComparatorUint8 provides a basic comparison on uint8.

func Dump

func Dump(i ...interface{})

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

func Export

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

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

func IsEmpty

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 v1.13.2

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 v1.14.3

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

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

func ListItemValues added in v1.13.2

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 v1.13.2

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 v1.12.2

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

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

func MapContainsPossibleKey added in v1.12.2

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 added in v1.10.0

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

MapCopy does a shallow copy from map `data` to `copy` for most commonly used map type map[string]interface{}.

func MapDelete added in v1.12.2

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

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

func MapMerge added in v1.12.2

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

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

func MapMergeCopy added in v1.12.2

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 v1.13.4

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

MapOmitEmpty deletes all empty values from given map.

func MapPossibleItemByKey added in v1.10.0

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

MapPossibleItemByKey tries to find the possible key-value pair for given key ignoring cases and symbols.

Note that this function might be of low performance.

func MapToSlice added in v1.15.0

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 v1.13.2

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

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

func SliceDelete added in v1.13.2

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 v1.15.0

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 v1.16.3

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 v1.15.0

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

func Throw(exception interface{})

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

func Try added in v1.14.1

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

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 v1.14.3

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 is a function that compare a and b, and returns the result as int.

Should return a number:

negative , if a < b
zero     , if a == b
positive , if a > b

Jump to

Keyboard shortcuts

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