goutil

package module
v0.0.0-...-30c9632 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2025 License: GPL-3.0 Imports: 11 Imported by: 1

README

goutil

常用的go语言工具集,搜集了一些常用方法,部分方法尚未仔细验证,请谨慎使用。

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool(v interface{}) bool

Bool 将任意值转换成bool类型 注意:1. 当无法或者不支持转换时,此接口将返回0值;

  1. 此接口应当只在明确数据内容只是需要类型转换时使用,不应当用于对未知值进行转换。

func Cond

func Cond(cond bool, trueVal interface{}, falseVal interface{}) interface{}

Cond 条件表达式,用于实现三元运算符的功能,if cond == true then trueVal else falseVal

func CopyStruct

func CopyStruct(src, dst interface{})

CopyStruct 复制结构体

func EmptyCond

func EmptyCond(v interface{}, elseVal interface{}) interface{}

EmptyCond 判断值v,如果为空(零值或者nil),则返回elseVal,否则返回v本身

func Float32

func Float32(v interface{}) float32

func Float64

func Float64(v interface{}) float64

Float64 将任意值转换成float64类型 注意:1. 当无法或者不支持转换时,此接口将返回0值;

  1. 此接口应当只在明确数据内容只是需要类型转换时使用,不应当用于对未知值进行转换。

func Int

func Int(v interface{}) int

func Int32

func Int32(v interface{}) int32

func Int64

func Int64(v interface{}) int64

Int64 将任意值转换成int64类型 注意:1. 当无法或者不支持转换时,此接口将返回0值;

  1. 此接口应当只在明确数据内容只是需要类型转换时使用,不应当用于对未知值进行转换。

func IsEmpty

func IsEmpty(v interface{}) bool

IsEmpty 判断是否为空

func IsList

func IsList(i interface{}) bool

IsList 判断给定的对象是否是数组或者切片

func IsMap

func IsMap(v interface{}) bool

IsMap 判断给定的值是否是一个map

func IsNil

func IsNil(i interface{}) bool

IsNil 判断给定的值是否为nil

func NilCond

func NilCond(v interface{}, elseVal interface{}) interface{}

NilCond 判断值v,如果等于nil,则返回elseVal,否则返回v本身

func RandNumericString

func RandNumericString(len int) string

RandNumericString 生成一个只包含数字的随机字符串

func RandString

func RandString(len int) string

RandString 获取随机字符串

func String

func String(v interface{}) string

String 将任意值转换成string类型 注意:1. 当无法或者不支持转换时,此接口将返回0值;

  1. 此接口应当只在明确数据内容只是需要类型转换时使用,不应当用于对未知值进行转换。

func SubString

func SubString(str string, start, end int) string

SubString 字符串截取

func Uint

func Uint(v interface{}) uint

func Uint32

func Uint32(v interface{}) uint32

func Uint64

func Uint64(v interface{}) uint64

Uint64 将任意值转换成uint64类型 注意:1. 当无法或者不支持转换时,此接口将返回0值;

  1. 此接口应当只在明确数据内容只是需要类型转换时使用,不应当用于对未知值进行转换。

Types

type KVPair

type KVPair struct {
	K, V string
}

KVPair 参数键值对

type KVPairs

type KVPairs []KVPair

KVPairs 参数键值对列表

func Map2KVPairs

func Map2KVPairs(params map[string]string) KVPairs

Map2KVPairs 将map转换为键值对列表

func (KVPairs) Add

func (p KVPairs) Add(k, v string)

Add 添加键值对

func (KVPairs) EscapedString

func (p KVPairs) EscapedString() string

EscapedString 将所有元素按照“参数=urlencode(参数值)”的模式用“&”字符拼接成字符串

func (KVPairs) FilterEmptyValueAndKeys

func (p KVPairs) FilterEmptyValueAndKeys(keys ...string) KVPairs

FilterEmptyValueAndKeys 去掉列表中值为空以及指定字段的值

func (KVPairs) Len

func (p KVPairs) Len() int

func (KVPairs) Less

func (p KVPairs) Less(i, j int) bool

Less 实现排序接口

func (KVPairs) QuotedAndEscapedString

func (p KVPairs) QuotedAndEscapedString() string

QuotedAndEscapedString 自定义值的处理方式

func (KVPairs) QuotedString

func (p KVPairs) QuotedString() string

QuotedString 将所有元素按照“参数="参数值"”的模式用“&”字符拼接成字符串

func (KVPairs) Reverse

func (p KVPairs) Reverse()

Reverse 倒序

func (KVPairs) Sort

func (p KVPairs) Sort()

Sort 排序

func (KVPairs) String

func (p KVPairs) String() string

String 将所有元素按照“参数=参数值”的模式用“&”字符拼接成字符串

func (KVPairs) Swap

func (p KVPairs) Swap(i, j int)

func (KVPairs) ToMap

func (p KVPairs) ToMap() map[string]string

ToMap 将键值对转换为Map

func (KVPairs) ToXML

func (p KVPairs) ToXML() string

ToXML 将键值对转换成xml

type M

type M map[string]interface{}

M 常用的map类型

func MVal

func MVal(v interface{}) M

MVal 将给定V转换成一个M对象,如果不支持转换,则直接返回空的M值

func SMVal

func SMVal(v interface{}) []M

SMVal 将v转换为一个M列表

func (M) Contains

func (m M) Contains(k string) bool

Contains 检查是否包含指定key

func (M) DefaultGet

func (m M) DefaultGet(k string, dv interface{}) interface{}

DefaultGet 获取配置项k的值,如果不存在则返回默认值

func (M) DefaultGetBool

func (m M) DefaultGetBool(k string, b bool) bool

func (M) DefaultGetFloat64

func (m M) DefaultGetFloat64(k string, f float64) float64

func (M) DefaultGetInt

func (m M) DefaultGetInt(k string, i int) int

func (M) DefaultGetInt64

func (m M) DefaultGetInt64(k string, i int64) int64

func (M) DefaultGetString

func (m M) DefaultGetString(k, s string) string

DefaultGetString 获取指定K的值,如果K不存在,则返回默认值

func (M) DefaultGetUint

func (m M) DefaultGetUint(k string, i uint) uint

func (M) DefaultGetUint64

func (m M) DefaultGetUint64(k string, i uint64) uint64

func (M) Del

func (m M) Del(k string)

Del 删除指定key

func (M) ForceMerge

func (m M) ForceMerge(n M)

ForceMerge 强制合并数据

func (M) Get

func (m M) Get(k string) (interface{}, bool)

Get 获取配置项k的值

func (M) GetBool

func (m M) GetBool(k string) bool

func (M) GetByPath

func (m M) GetByPath(path string) (interface{}, bool)

GetByPath 根据path获取值

func (M) GetFloat32

func (m M) GetFloat32(k string) float32

func (M) GetFloat64

func (m M) GetFloat64(k string) float64

func (M) GetInt

func (m M) GetInt(k string) int

func (M) GetInt64

func (m M) GetInt64(k string) int64

func (M) GetJson

func (m M) GetJson(k string) string

func (M) GetString

func (m M) GetString(k string) string

func (M) GetUint

func (m M) GetUint(k string) uint

func (M) GetUint64

func (m M) GetUint64(k string) uint64

func (M) IsArray

func (m M) IsArray(k string) bool

IsArray 判断指定的值是否是切片或者数组

func (M) IsEmpty

func (m M) IsEmpty(k string) bool

IsEmpty 判断是否为空

func (M) Json

func (m M) Json() string

Json 获取json格式数据

func (M) Keys

func (m M) Keys() []string

Keys 获取map的键列表

func (M) MVal

func (m M) MVal(k string) M

MVal 获取指定k的值

func (M) MValByPath

func (m M) MValByPath(path string) (M, bool)

MValByPath 根据路径获取一个M值

func (M) Map

func (m M) Map() map[string]interface{}

Map 获取原始数据

func (M) Merge

func (m M) Merge(n M)

Merge 合并数据

func (M) Pairs

func (m M) Pairs() KVPairs

Pairs 将M转换为KVPairs

func (M) SMVal

func (m M) SMVal(k string) []M

SMVal 获取一个M切片

func (M) SMValByPath

func (m M) SMValByPath(path string) ([]M, bool)

SMValByPath 根据路径获取一个SM值

func (M) SVal

func (m M) SVal(k string) S

SVal 获取指定k的Slice值

func (M) SValByPath

func (m M) SValByPath(path string) (S, bool)

SValByPath 根据路径获取一个S值

func (M) Size

func (m M) Size() int

Size 获取参数数量

func (M) UrlQuery

func (m M) UrlQuery() string

UrlQuery 将map转换为query string

func (M) UrlValues

func (m M) UrlValues() url.Values

UrlValues 转换为url.Values

type S

type S []interface{}

S 常用的slice类型

func SVal

func SVal(v interface{}) S

SVal 将v转换为一个Slice值,如果不支持转换,则返回空,使用者需要预先确认值类型

func (S) Contains

func (s S) Contains(v interface{}) bool

Contains 包含检查 注意:1. 此方法因为在比较过程中进行了类型转换,因此存在风险;

  1. 建议在明确数据内容的场景下使用此方法.

func (S) Extend

func (s S) Extend(f func(interface{}) string) *XS

Extend 对slice进行增强

func (S) Filter

func (s S) Filter()

Filter 过滤,过滤掉slice中的空值对象

func (S) Float64

func (s S) Float64() []float64

func (S) Int

func (s S) Int() []int

func (S) Int64

func (s S) Int64() []int64

Int64 类型转换,将所有元素转换成int64类型,并返回转换后的slice

func (S) Interface

func (s S) Interface() []interface{}

Interface 类型转换, to interface

func (S) Json

func (s S) Json() string

Json 将S转换为json字符串

func (*S) Load

func (s *S) Load(v []byte) error

Load 从json中加载数据

func (S) M

func (s S) M() []M

M 类型转换,将slice转换为map

func (S) Size

func (s S) Size() int

func (S) String

func (s S) String() []string

func (S) Uint

func (s S) Uint() []uint

func (S) Uint64

func (s S) Uint64() []uint64

type Tree

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

Tree 定义一个树结构

func NewTree

func NewTree() *Tree

NewTree 创建一个树实例

func (*Tree) Add

func (tree *Tree) Add(items ...TreeItem)

Add 添加Tree Items

func (*Tree) Tree

func (tree *Tree) Tree() []TreeItem

Tree 返回树

type TreeItem

type TreeItem interface {
	Key() string
	ParentKey() string
	AddChild(ti TreeItem)
	GetChild(k string) (TreeItem, bool)
}

TreeItem 定义构成树的元素接口

type XS

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

XS 扩展增强的slice对象

func NewXS

func NewXS(f func(interface{}) string) *XS

NewXS 创建一个slice增强对象

func XSVal

func XSVal(v interface{}, f func(interface{}) string) *XS

XSVal 将v转换为一个增强的Slice对象,如果不支持转换,则返回空,使用者需要预先确认值类型

func (*XS) Add

func (xs *XS) Add(args ...interface{})

Add 添加(1个或多个)元素到列表

func (*XS) Contains

func (xs *XS) Contains(v interface{}) bool

Contains 检查是否包含某个元素

func (*XS) Delete

func (xs *XS) Delete(arg interface{})

Delete 删除指定值的元素

func (*XS) Intersect

func (xs *XS) Intersect(arrs ...S) *XS

Intersect 计算给定切片与当前列表的交集

func (*XS) SAdd

func (xs *XS) SAdd(args ...interface{})

SAdd 添加(1个或多个)元素到列表, 如果元素已经存在,则忽略该元素

func (*XS) Size

func (xs *XS) Size() int

Size 获取元素列表个数

func (*XS) Slice

func (xs *XS) Slice() []interface{}

Slice 获取切片(为了防止修改对象本身,需单独复制一个切片出来)

func (*XS) Union

func (xs *XS) Union(arrs ...S) *XS

Union 计算给定切片与当前列表的并集

func (*XS) Unique

func (xs *XS) Unique() []interface{}

Unique 对列表元素去重

func (*XS) UniqueFilter

func (xs *XS) UniqueFilter(f func(string) bool) []interface{}

UniqueFilter 对列表元素去重,并根据filter方法进行过滤 filter参数是一个字符串,此值是根据创建XS对象时指定的keyFunc得到的

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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