util

package
v0.1.140 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2021 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanPath

func CleanPath(p string) string

CleanPath is the URL version of path.Clean, it returns a canonical URL path for p, eliminating . and .. elements.

The following rules are applied iteratively until no further processing can be done:

  1. Replace multiple slashes with a single slash.
  2. Eliminate each . path name element (the current directory).
  3. Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
  4. Eliminate .. elements that begin a rooted path: that is, replace "/.." by "/" at the beginning of a path.

If the result of this process is an empty string, "/" is returned

func Exist added in v0.1.130

func Exist(path string) (bool, error)

Exist

func ExpandEnv

func ExpandEnv(data []byte) []byte

ExpandEnv expand env variables, format: ${env}, ${env||default}

func FormatLanguage

func FormatLanguage(lang string) string

FormatLanguage format lang to ll-CC format

func FormatVersion

func FormatVersion(ver string, minDepth int) string

FormatVersion format version to have minimum depth, eg. FormatVersion("v10...2....2.1-alpha", 5) == "v10.2.2.1.0-alpha"

func GenUniqueId

func GenUniqueId() string

GenUniqueId generate a 24 bytes unique id

func HashSha1Crc32

func HashSha1Crc32(s string) uint32

func IsAllDigit

func IsAllDigit(s string) bool

func IsAllLetter

func IsAllLetter(s string) bool

func IsAllLower

func IsAllLower(s string) bool

func IsAllUpper

func IsAllUpper(s string) bool

func MapClear

func MapClear(m map[string]interface{})

MapClear clear map to empty

func MapGet

func MapGet(m map[string]interface{}, key string, dftSplit ...string) interface{}

MapGet get value by dot separated key, empty key for m itself

func MapMerge

func MapMerge(a map[string]interface{}, m ...map[string]interface{})

MapMerge merge map recursively

func MapSet

func MapSet(m map[string]interface{}, key string, val interface{}, dftSplit ...string)

MapSet set value by dot separated key, empty key for root, nil val for clear

func MapToSliceInt added in v0.1.94

func MapToSliceInt(sI interface{}) []int

强制转换key为字符串数字map为[]int {"1":1} => [1]

func MapToSliceString added in v0.1.94

func MapToSliceString(sI interface{}) []string

强制转换key为字符串数字的map为[]string {"1":"ss"} => ["ss"]

func Md5Bytes

func Md5Bytes(v interface{}) []byte

Md5Bytes get 16 bytes of md5

func Md5String

func Md5String(v interface{}) string

Md5String get 32 bytes of md5 hex string

func PanicTrace

func PanicTrace(maxDepth int, multiLine, debug bool) string

PanicTrace get panic trace

func ParamsToMapSlice added in v0.1.94

func ParamsToMapSlice(m map[string]interface{}) map[string]interface{}

转换map为map/slice {"aaa[0][bbb]":"sss"} => {"aaa":[{"bbb":"sss"}]} 主要是转换map 中key为数字的map 转换为slice

func ST2Map added in v0.1.97

func ST2Map(s interface{}, stayZero bool, dftTag ...string) map[string]interface{}

ST2Map Struct to map stayZero 是否保留零值 dftTag 获取标签为字段名,如果未传标签或者没有标签 获取字段名字,并首字母小写当作字段名

func STMergeField

func STMergeField(s1, s2 interface{})

STMergeField merge the same or compatible field of s2 into s1, zero and unexported field will be skipped and embedded struct field will be treated as a single field.

func STMergeSame

func STMergeSame(s1, s2 interface{})

STMergeSame merge none zero field of s2 into s1, s1 and s2 must have the same struct type. unexported field will be skipped and embedded struct field will be treated as a single field.

func SliceFilterInt added in v0.1.95

func SliceFilterInt(a []int, callbacks ...SliceFilterIntFunc) []int

SliceFilterInt 依次将 slice 中的每个值传递到 callback 函数。 如果 callback 函数返回 true,则 slice 的当前值会被包含在返回的结果slice中。 如果callback 未传 就会过滤零值

func SliceFilterString added in v0.1.95

func SliceFilterString(a []string, callbacks ...SliceFilterStringFunc) []string

SliceFilterString 依次将 slice 中的每个值传递到 callback 函数。 如果 callback 函数返回 true,则 slice 的当前值会被包含在返回的结果slice中。 如果callback 未传 就会过滤零值

func SliceSearch

func SliceSearch(n int, f func(int) bool) int

SliceSearch search in a slice has the length of n, return the first position where f(i) is true, -1 is returned if nothing found.

func SliceSearchInt

func SliceSearchInt(a []int, x int) int

SliceSearchInt search x in an int slice, return the first position of x, -1 is returned if nothing found.

func SliceSearchString

func SliceSearchString(a []string, x string) int

SliceSearchString search x in a string slice, return the first position of x, -1 is returned if nothing found.

func SliceUniqueInt

func SliceUniqueInt(a []int) []int

SliceUniqueInt retrieve unique items item from int slice.

func SliceUniqueString

func SliceUniqueString(a []string) []string

SliceUniqueString retrieve unique string items from string slice.

func ToBool

func ToBool(v interface{}) bool

func ToFloat

func ToFloat(v interface{}) float64

func ToInt

func ToInt(v interface{}) int

func ToInt64 added in v0.1.92

func ToInt64(v interface{}) int64

func ToString

func ToString(v interface{}) string

func VersionCompare

func VersionCompare(ver1, ver2 string) int

VersionCompare compare versions like version_compare of php, special version strings these are handled in the following order, (any string not found) < dev < alpha = a < beta = b < rc < #(empty) < ##(digit) < pl = p, result: -1(ver1 < ver2), 0(ver1 == ver2), 1(ver1 > ver2)

Types

type HashFnv64a added in v0.1.3

type HashFnv64a struct{}

func NewHashFnv64a added in v0.1.3

func NewHashFnv64a() *HashFnv64a

newHash returns a new 64-bit FNV-1a hash which makes no memory allocations. See https://en.wikipedia.org/wiki/Fowler–Noll–Vo_hash_function

func (*HashFnv64a) Value added in v0.1.3

func (h *HashFnv64a) Value(key string) uint64

Value gets the string and returns its uint64 hash value.

type HashFunc

type HashFunc func(string) uint32

type HashRing

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

func NewHashRing

func NewHashRing(args ...interface{}) *HashRing

new hash ring, optional args: node string: node item key, multi node accepted, see AddNode(). spots int: num of virtual spots for each node, default 32. hashFunc HashFunc: function to calculate hash value, default HashSha1Crc32. eg. h := NewHashRing("127.0.0.1:6379", "127.0.0.1:6379", "127.0.0.1:6380")

func (*HashRing) AddNode

func (h *HashRing) AddNode(node string, w ...int)

add node to hash ring with default 1 weight, if node add multiple times, it gets a proportional amount of weight.

func (*HashRing) DelNode

func (h *HashRing) DelNode(node string)

remove node from hash ring

func (*HashRing) GetNode

func (h *HashRing) GetNode(key string) string

get node from hash ring by the hash value of key

type SliceFilterIntFunc added in v0.1.95

type SliceFilterIntFunc func(s int) bool

type SliceFilterStringFunc added in v0.1.95

type SliceFilterStringFunc func(s string) bool

Jump to

Keyboard shortcuts

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