collection

package module
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Aug 23, 2022 License: Apache-2.0 Imports: 7 Imported by: 45

README

collection

Collection包目标是用于替换golang原生的Slice,使用场景是在大量不追求极致性能,追求业务开发效能的场景。

Collection的使用手册线上地址:http://collection.funaio.cn/

你也可以通过类库直接在本地启动本地文档:(需要本机安装npm)

npm install 
npm run docs:dev

// 访问地址: http://localhost:2333/
版本 说明
v1.4.2 增加KeyByStrField方法,增加交集和并集函数 Union,Intersect
v1.4.0 增加三种新类型 uint32, uint, uint64, 增加GroupBy 和 Split 方法
v1.3.0 增加文档说明
1.2.0 增加对象指针数组,增加测试覆盖率, 增加ToInterfaces方法
1.1.2 增加一些空数组的判断,解决一些issue
1.1.1 对collection包进行了json解析和反解析的支持,对mix类型支持了SetField和RemoveFields的类型设置
1.1.0 增加了对int32的支持,增加了延迟加载,增加了Copy函数,增加了compare从ICollection传递到IMix,使用快排加速了Sort方法
1.0.1 第一次发布

go get github.com/jianfengye/collection@v1.4.0

Collection包目前支持的元素类型:int32, int, int64, uint32, uint, uint64, float32, float64, string, object, objectPoint

第一步:使用下列几个方法进行初始化Collection:

NewIntCollection(objs []int) *IntCollection

NewInt64Collection(objs []int64) *Int64Collection

NewInt32Collection(objs []int32) *Int32Collection

NewUIntCollection(objs []uint) *UIntCollection

NewUInt64Collection(objs []uint64) *UInt64Collection

NewUInt32Collection(objs []uint32) *UInt32Collection

NewFloat64Collection(objs []float64) *Float64Collection

NewFloat32Collection(objs []float32) *Float32Collection

NewStrCollection(objs []string) *StrCollection

NewObjCollection(objs interface{}) *ObjCollection

NewObjPointCollection(objs interface{}) *ObjPointCollection

第二步:你可以很方便使用ICollection的所有函数:

// ICollection 表示数组结构,有几种类型
type ICollection interface {
	// Err ICollection错误信息,链式调用的时候需要检查下这个error是否存在,每次调用之后都检查一下
	Err() error
	// SetErr 设置ICollection的错误信息
	SetErr(error) ICollection

	/*
		下面的方法对所有Collection都生效
	*/
	// NewEmpty 复制一份当前相同类型的ICollection结构,但是数据是空的
	NewEmpty(err ...error) ICollection
	// IsEmpty 判断是否是空数组
	IsEmpty() bool
	// IsNotEmpty 判断是否是空数组
	IsNotEmpty() bool
	// Append 放入一个元素到数组中,对所有Collection生效, 仅当item和Collection结构不一致的时候返回错误
	Append(item interface{}) ICollection
	// Remove 删除一个元素, 需要自类实现
	Remove(index int) ICollection
	// Insert 增加一个元素。
	Insert(index int, item interface{}) ICollection
	// Search 查找数据中是否包含,-1不包含,>=0 返回数组中元素下标,对所有Collection生效
	Search(item interface{}) int
	// Unique 过滤数组中重复的元素,仅对基础Collection生效
	Unique() ICollection
	// Filter 按照某个方法进行过滤, 保留符合的
	Filter(func(item interface{}, key int) bool) ICollection
	// Reject 按照某个方法进行过滤,去掉符合的
	Reject(func(item interface{}, key int) bool) ICollection
	// First 获取满足条件的第一个, 如果没有填写过滤条件,就获取所有的第一个
	First(...func(item interface{}, key int) bool) IMix
	// Last 获取满足条件的最后一个,如果没有填写过滤条件,就获取所有的最后一个
	Last(...func(item interface{}, key int) bool) IMix
	// Slice 获取数组片段,对所有Collection生效
	Slice(...int) ICollection
	// Index 获取某个下标,对所有Collection生效
	Index(i int) IMix
	// SetIndex 设置数组的下标为某个值
	SetIndex(i int, val interface{}) ICollection
	// Copy 复制当前数组
	Copy() ICollection
	// Count 获取数组长度,对所有Collection生效
	Count() int
	// Merge 将两个数组进行合并,参数的数据挂在当前数组中,返回当前数组,对所有Collection生效
	Merge(arr ICollection) ICollection

	// Each 每个元素都调用一次的方法
	Each(func(item interface{}, key int))
	// Map 每个元素都调用一次的方法, 并组成一个新的元素
	Map(func(item interface{}, key int) interface{}) ICollection
	// Reduce 合并一些元素,并组成一个新的元素
	Reduce(func(carry IMix, item IMix) IMix) IMix
	// Every 判断每个对象是否都满足, 如果collection是空,返回true
	Every(func(item interface{}, key int) bool) bool
	// ForPage 按照分页进行返回
	ForPage(page int, perPage int) ICollection
	// Nth 获取从索引offset开始为0,每n位值组成数组
	Nth(n int, offset int) ICollection
	// Pad 将数组填充到count个数,只能数值型生效
	Pad(count int, def interface{}) ICollection
	// Pop 从队列右侧弹出结构
	Pop() IMix
	// Push 推入元素
	Push(item interface{}) ICollection
	// Prepend 前面插入一个元素
	Prepend(item interface{}) ICollection
	// Random 随机获取一个元素
	Random() IMix
	// Reverse 倒置
	Reverse() ICollection
	// Shuffle 随机乱置
	Shuffle() ICollection
	// GroupBy 类scala groupby 设计, 根据某个函数分组
	GroupBy(func(interface{}, int) interface{}) map[interface{}]ICollection
	// Split 按照size个数进行分组
	Split(size int) []ICollection
	// DD 打印出当前数组结构
	DD()

	/*
		下面的方法对ObjCollection 和 ObjPointCollection 生效
	*/
	// Pluck 返回数组中对象的某个key组成的数组,仅对ObjectCollection生效, key为对象属性名称,必须为public的属性
	Pluck(key string) ICollection
	// SortBy 按照某个字段进行排序
	SortBy(key string) ICollection
	// SortByDesc 按照某个字段进行排序,倒序
	SortByDesc(key string) ICollection
	// KeyByStrField 根据某个字段为key,返回一个map, 要求key对应的field是string
	KeyByStrField(key string) (map[string]interface{}, error)

	/*
		下面的方法对基础Collection生效,但是ObjCollection一旦设置了Compare函数也生效
	*/
	// SetCompare 比较a和b,如果a>b, 返回1,如果a<b, 返回-1,如果a=b, 返回0
	// 设置比较函数,理论上所有Collection都能设置比较函数,但是强烈不建议基础Collection设置
	SetCompare(func(a interface{}, b interface{}) int) ICollection
	// GetCompare 获取比较函数
	GetCompare() func(a interface{}, b interface{}) int
	// Max 数组中最大的元素,仅对基础Collection生效, 可以传递一个比较函数
	Max() IMix
	// Min 数组中最小的元素,仅对基础Collection生效
	Min() IMix
	// Contains 判断是否包含某个元素,(并不进行定位),对基础Collection生效
	Contains(obj interface{}) bool
	// ContainsCount 判断包含某个元素的个数,返回0代表没有找到,返回正整数代表个数。必须设置compare函数
	ContainsCount(obj interface{}) int
	// Diff 比较两个数组,获取第一个数组不在第二个数组中的元素,组成新数组,仅对基础元素生效
	Diff(arr ICollection) ICollection
	// Sort 进行排序, 升序
	Sort() ICollection
	// SortDesc 进行排序,倒序
	SortDesc() ICollection
	// Join 进行拼接
	Join(split string, format ...func(item interface{}) string) string
	// Union 比较两个数组,获取两个数组并集,仅对基础元素生效
	Union (arr ICollection) ICollection
	// Intersect 比较两个数组,获取两个数组交集,仅对基础元素生效
	Intersect (arr ICollection) ICollection

	/*
		下面的方法对基础Collection生效
	*/
	// Avg 获取平均值
	Avg() IMix
	// Median 获取中位值
	Median() IMix
	// Mode 获取Mode值
	Mode() IMix
	// Sum 获取sum值
	Sum() IMix

	/*
		下面的方法对根据不同的对象,进行不同的调用转换
	*/
	// ToStrings 转化为golang原生的字符数组,仅对StrCollection生效
	ToStrings() ([]string, error)
	// ToInt64s 转化为golang原生的Int64数组,仅对Int64Collection生效
	ToInt64s() ([]int64, error)
	// ToInt32s 转化为golang原生的Int32数组,仅对Int32Collection生效
	ToInt32s() ([]int32, error)
	// ToInts 转化为golang原生的Int数组,仅对IntCollection生效
	ToInts() ([]int, error)
	// ToUInt64s 转化为golang原生的UInt64数组,仅对UInt64Collection生效
	ToUInt64s() ([]uint64, error)
	// ToUInt32s 转化为golang原生的UInt32数组,仅对UInt32Collection生效
	ToUInt32s() ([]uint32, error)
	// ToUInts 转化为golang原生的UInt数组,仅对UIntCollection生效
	ToUInts() ([]uint, error)
	// ToMixs 转化为obj数组
	ToMixs() ([]IMix, error)
	// ToFloat64s 转化为float64数组
	ToFloat64s() ([]float64, error)
	// ToFloat32s 转化为float32数组
	ToFloat32s() ([]float32, error)
	// ToInterfaces 转化为interface{} 数组
	ToInterfaces() ([]interface{}, error)
	// ToObjs 转化为objs{}数组
	ToObjs(interface{}) error

	// ToJson 转换为Json
	ToJson() ([]byte, error)
	// FromJson 从json数组转换
	FromJson([]byte) error
}

更多例子请参考: 使用手册

License

collection is licensed under Apache License.

Contributors

感谢下列作者的contributor. [Contribute].

author

有任何问题可直接github留言,或者联系作者:轩脉刃

xuanmairen

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AbsCollection

type AbsCollection struct {
	ICollection
	Parent ICollection //用于调用子类
	// contains filtered or unexported fields
}

这个是一个虚函数,能实现的都实现

func (*AbsCollection) Append

func (arr *AbsCollection) Append(item interface{}) ICollection

func (*AbsCollection) Avg

func (arr *AbsCollection) Avg() IMix

func (*AbsCollection) Contains

func (arr *AbsCollection) Contains(obj interface{}) bool

func (*AbsCollection) ContainsCount

func (arr *AbsCollection) ContainsCount(obj interface{}) int

func (*AbsCollection) Copy

func (arr *AbsCollection) Copy() ICollection

func (*AbsCollection) Count

func (arr *AbsCollection) Count() int

func (*AbsCollection) DD

func (arr *AbsCollection) DD()

func (*AbsCollection) Diff

func (arr *AbsCollection) Diff(arr2 ICollection) ICollection

func (*AbsCollection) Each

func (arr *AbsCollection) Each(f func(item interface{}, key int))

func (*AbsCollection) Err

func (arr *AbsCollection) Err() error

func (*AbsCollection) Every

func (arr *AbsCollection) Every(f func(item interface{}, key int) bool) bool

func (*AbsCollection) Filter

func (arr *AbsCollection) Filter(f func(obj interface{}, index int) bool) ICollection

func (*AbsCollection) First

func (arr *AbsCollection) First(f ...func(obj interface{}, index int) bool) IMix

func (*AbsCollection) ForPage

func (arr *AbsCollection) ForPage(page int, perPage int) ICollection

func (*AbsCollection) FromJson

func (arr *AbsCollection) FromJson(data []byte) error

func (*AbsCollection) GetCompare

func (arr *AbsCollection) GetCompare() func(a interface{}, b interface{}) int

func (*AbsCollection) GroupBy added in v1.4.0

func (arr *AbsCollection) GroupBy(f func(item interface{}, key int) interface{}) map[interface{}]ICollection

func (*AbsCollection) Index

func (arr *AbsCollection) Index(i int) IMix

func (*AbsCollection) Insert

func (arr *AbsCollection) Insert(index int, obj interface{}) ICollection

func (*AbsCollection) Intersect added in v1.4.1

func (arr *AbsCollection) Intersect(arr2 ICollection) ICollection

Intersect 两个集合的交集

func (*AbsCollection) IsEmpty

func (arr *AbsCollection) IsEmpty() bool

func (*AbsCollection) IsNotEmpty

func (arr *AbsCollection) IsNotEmpty() bool

func (*AbsCollection) Join

func (arr *AbsCollection) Join(split string, format ...func(item interface{}) string) string

func (*AbsCollection) KeyByStrField added in v1.4.2

func (arr *AbsCollection) KeyByStrField(key string) (map[string]interface{}, error)

func (*AbsCollection) Last

func (arr *AbsCollection) Last(fs ...func(item interface{}, key int) bool) IMix

func (*AbsCollection) Map

func (arr *AbsCollection) Map(f func(item interface{}, key int) interface{}) ICollection

func (*AbsCollection) MarshalJSON

func (arr *AbsCollection) MarshalJSON() ([]byte, error)

func (*AbsCollection) Max

func (arr *AbsCollection) Max() IMix

func (*AbsCollection) Median

func (arr *AbsCollection) Median() IMix

func (*AbsCollection) Merge

func (arr *AbsCollection) Merge(arr2 ICollection) ICollection

func (*AbsCollection) Min

func (arr *AbsCollection) Min() IMix

func (*AbsCollection) Mode

func (arr *AbsCollection) Mode() IMix

func (*AbsCollection) NewEmpty

func (arr *AbsCollection) NewEmpty(err ...error) ICollection

下面的几个函数必须要实现

func (*AbsCollection) Nth

func (arr *AbsCollection) Nth(n int, offset int) ICollection

func (*AbsCollection) Pad

func (arr *AbsCollection) Pad(count int, def interface{}) ICollection

func (*AbsCollection) Pluck

func (arr *AbsCollection) Pluck(key string) ICollection

func (*AbsCollection) Pop

func (arr *AbsCollection) Pop() IMix

func (*AbsCollection) Prepend

func (arr *AbsCollection) Prepend(item interface{}) ICollection

func (*AbsCollection) Push

func (arr *AbsCollection) Push(item interface{}) ICollection

func (*AbsCollection) Random

func (arr *AbsCollection) Random() IMix

func (*AbsCollection) Reduce

func (arr *AbsCollection) Reduce(f func(carry IMix, item IMix) IMix) IMix

func (*AbsCollection) Reject

func (arr *AbsCollection) Reject(f func(item interface{}, key int) bool) ICollection

func (*AbsCollection) Remove

func (arr *AbsCollection) Remove(index int) ICollection

func (*AbsCollection) Reverse

func (arr *AbsCollection) Reverse() ICollection

func (*AbsCollection) Search

func (arr *AbsCollection) Search(item interface{}) int

func (*AbsCollection) SetCompare

func (arr *AbsCollection) SetCompare(compare func(a interface{}, b interface{}) int) ICollection

func (*AbsCollection) SetErr

func (arr *AbsCollection) SetErr(err error) ICollection

func (*AbsCollection) SetIndex

func (arr *AbsCollection) SetIndex(i int, val interface{}) ICollection

func (*AbsCollection) Shuffle

func (arr *AbsCollection) Shuffle() ICollection

func (*AbsCollection) Slice

func (arr *AbsCollection) Slice(ps ...int) ICollection

func (*AbsCollection) Sort

func (arr *AbsCollection) Sort() ICollection

func (*AbsCollection) SortBy

func (arr *AbsCollection) SortBy(key string) ICollection

func (*AbsCollection) SortByDesc

func (arr *AbsCollection) SortByDesc(key string) ICollection

func (*AbsCollection) SortDesc

func (arr *AbsCollection) SortDesc() ICollection

func (*AbsCollection) Split added in v1.4.0

func (arr *AbsCollection) Split(size int) []ICollection

func (*AbsCollection) Sum

func (arr *AbsCollection) Sum() IMix

func (*AbsCollection) ToFloat32s

func (arr *AbsCollection) ToFloat32s() ([]float32, error)

func (*AbsCollection) ToFloat64s

func (arr *AbsCollection) ToFloat64s() ([]float64, error)

func (*AbsCollection) ToInt32s

func (arr *AbsCollection) ToInt32s() ([]int32, error)

func (*AbsCollection) ToInt64s

func (arr *AbsCollection) ToInt64s() ([]int64, error)

func (*AbsCollection) ToInterfaces

func (arr *AbsCollection) ToInterfaces() ([]interface{}, error)

func (*AbsCollection) ToInts

func (arr *AbsCollection) ToInts() ([]int, error)

func (*AbsCollection) ToJson

func (arr *AbsCollection) ToJson() ([]byte, error)

func (*AbsCollection) ToMixs

func (arr *AbsCollection) ToMixs() ([]IMix, error)

func (*AbsCollection) ToObjs

func (arr *AbsCollection) ToObjs(objs interface{}) error

func (*AbsCollection) ToStrings

func (arr *AbsCollection) ToStrings() ([]string, error)

func (*AbsCollection) ToUInt32s added in v1.4.0

func (arr *AbsCollection) ToUInt32s() ([]uint32, error)

func (*AbsCollection) ToUInt64s added in v1.4.0

func (arr *AbsCollection) ToUInt64s() ([]uint64, error)

func (*AbsCollection) ToUInts added in v1.4.0

func (arr *AbsCollection) ToUInts() ([]uint, error)

func (*AbsCollection) Union added in v1.4.1

func (arr *AbsCollection) Union(arr2 ICollection) ICollection

Union 两个集合的并集

func (*AbsCollection) Unique

func (arr *AbsCollection) Unique() ICollection

func (*AbsCollection) UnmarshalJSON

func (arr *AbsCollection) UnmarshalJSON(data []byte) error

type EleType

type EleType int
const (
	TYPE_UNKNWON EleType = iota
	Type_INT
	Type_INT64
	Type_INT32
	TYPE_STRING
	TYPE_FLOAT32
	TYPE_FLOAT64
	TYPE_OBJ
	TYPE_OBJ_POINT
)

type Float32Collection

type Float32Collection struct {
	AbsCollection
	// contains filtered or unexported fields
}

func NewFloat32Collection

func NewFloat32Collection(objs []float32) *Float32Collection

NewFloat32Collection create a new Float32Collection

func (*Float32Collection) Copy

func (arr *Float32Collection) Copy() ICollection

Copy copy collection

func (*Float32Collection) Count

func (arr *Float32Collection) Count() int

func (*Float32Collection) DD

func (arr *Float32Collection) DD()

func (*Float32Collection) FromJson

func (arr *Float32Collection) FromJson(data []byte) error

func (*Float32Collection) Index

func (arr *Float32Collection) Index(i int) IMix

func (*Float32Collection) Insert

func (arr *Float32Collection) Insert(index int, obj interface{}) ICollection

func (*Float32Collection) NewEmpty

func (arr *Float32Collection) NewEmpty(err ...error) ICollection

func (*Float32Collection) Remove

func (arr *Float32Collection) Remove(i int) ICollection

func (*Float32Collection) SetIndex

func (arr *Float32Collection) SetIndex(i int, val interface{}) ICollection

func (*Float32Collection) ToJson

func (arr *Float32Collection) ToJson() ([]byte, error)

type Float64Collection

type Float64Collection struct {
	AbsCollection
	// contains filtered or unexported fields
}

func NewFloat64Collection

func NewFloat64Collection(objs []float64) *Float64Collection

NewFloat64Collection create a new Float64Collection

func (*Float64Collection) Copy

func (arr *Float64Collection) Copy() ICollection

Copy copy collection

func (*Float64Collection) Count

func (arr *Float64Collection) Count() int

func (*Float64Collection) DD

func (arr *Float64Collection) DD()

func (*Float64Collection) FromJson

func (arr *Float64Collection) FromJson(data []byte) error

func (*Float64Collection) Index

func (arr *Float64Collection) Index(i int) IMix

func (*Float64Collection) Insert

func (arr *Float64Collection) Insert(index int, obj interface{}) ICollection

func (*Float64Collection) NewEmpty

func (arr *Float64Collection) NewEmpty(err ...error) ICollection

func (*Float64Collection) Remove

func (arr *Float64Collection) Remove(i int) ICollection

func (*Float64Collection) SetIndex

func (arr *Float64Collection) SetIndex(i int, val interface{}) ICollection

func (*Float64Collection) ToJson

func (arr *Float64Collection) ToJson() ([]byte, error)

type ICollection

type ICollection interface {
	// Err ICollection错误信息,链式调用的时候需要检查下这个error是否存在,每次调用之后都检查一下
	Err() error
	// SetErr 设置ICollection的错误信息
	SetErr(error) ICollection

	/*
		下面的方法对所有Collection都生效
	*/
	// NewEmpty 复制一份当前相同类型的ICollection结构,但是数据是空的
	NewEmpty(err ...error) ICollection
	// IsEmpty 判断是否是空数组
	IsEmpty() bool
	// IsNotEmpty 判断是否是空数组
	IsNotEmpty() bool
	// Append 放入一个元素到数组中,对所有Collection生效, 仅当item和Collection结构不一致的时候返回错误
	Append(item interface{}) ICollection
	// Remove 删除一个元素, 需要自类实现
	Remove(index int) ICollection
	// Insert 增加一个元素。
	Insert(index int, item interface{}) ICollection
	// Search 查找数据中是否包含,-1不包含,>=0 返回数组中元素下标,对所有Collection生效
	Search(item interface{}) int
	// Unique 过滤数组中重复的元素,仅对基础Collection生效
	Unique() ICollection
	// Filter 按照某个方法进行过滤, 保留符合的
	Filter(func(item interface{}, key int) bool) ICollection
	// Reject 按照某个方法进行过滤,去掉符合的
	Reject(func(item interface{}, key int) bool) ICollection
	// First 获取满足条件的第一个, 如果没有填写过滤条件,就获取所有的第一个
	First(...func(item interface{}, key int) bool) IMix
	// Last 获取满足条件的最后一个,如果没有填写过滤条件,就获取所有的最后一个
	Last(...func(item interface{}, key int) bool) IMix
	// Slice 获取数组片段,对所有Collection生效
	Slice(...int) ICollection
	// Index 获取某个下标,对所有Collection生效
	Index(i int) IMix
	// SetIndex 设置数组的下标为某个值
	SetIndex(i int, val interface{}) ICollection
	// Copy 复制当前数组
	Copy() ICollection
	// Count 获取数组长度,对所有Collection生效
	Count() int
	// Merge 将两个数组进行合并,参数的数据挂在当前数组中,返回当前数组,对所有Collection生效
	Merge(arr ICollection) ICollection

	// Each 每个元素都调用一次的方法
	Each(func(item interface{}, key int))
	// Map 每个元素都调用一次的方法, 并组成一个新的元素
	Map(func(item interface{}, key int) interface{}) ICollection
	// Reduce 合并一些元素,并组成一个新的元素
	Reduce(func(carry IMix, item IMix) IMix) IMix
	// Every 判断每个对象是否都满足, 如果collection是空,返回true
	Every(func(item interface{}, key int) bool) bool
	// ForPage 按照分页进行返回
	ForPage(page int, perPage int) ICollection
	// Nth 获取从索引offset开始为0,每n位值组成数组
	Nth(n int, offset int) ICollection
	// Pad 将数组填充到count个数,只能数值型生效
	Pad(count int, def interface{}) ICollection
	// Pop 从队列右侧弹出结构
	Pop() IMix
	// Push 推入元素
	Push(item interface{}) ICollection
	// Prepend 前面插入一个元素
	Prepend(item interface{}) ICollection
	// Random 随机获取一个元素
	Random() IMix
	// Reverse 倒置
	Reverse() ICollection
	// Shuffle 随机乱置
	Shuffle() ICollection
	// GroupBy 类scala groupby 设计, 根据某个函数分组
	GroupBy(func(interface{}, int) interface{}) map[interface{}]ICollection
	// Split 按照size个数进行分组
	Split(size int) []ICollection
	// DD 打印出当前数组结构
	DD()

	/*
		下面的方法对ObjCollection生效
	*/
	// Pluck 返回数组中对象的某个key组成的数组,仅对ObjectCollection生效, key为对象属性名称,必须为public的属性
	Pluck(key string) ICollection
	// SortBy 按照某个字段进行排序
	SortBy(key string) ICollection
	// SortByDesc 按照某个字段进行排序,倒序
	SortByDesc(key string) ICollection
	// KeyByStrField 根据某个字段为key,返回一个map,要求key对应的field是string
	KeyByStrField(key string) (map[string]interface{}, error)

	/*
		下面的方法对基础Collection生效,但是ObjCollection一旦设置了Compare函数也生效
	*/
	// SetCompare 比较a和b,如果a>b, 返回1,如果a<b, 返回-1,如果a=b, 返回0
	// 设置比较函数,理论上所有Collection都能设置比较函数,但是强烈不建议基础Collection设置
	SetCompare(func(a interface{}, b interface{}) int) ICollection
	// GetCompare 获取比较函数
	GetCompare() func(a interface{}, b interface{}) int
	// Max 数组中最大的元素,仅对基础Collection生效, 可以传递一个比较函数
	Max() IMix
	// Min 数组中最小的元素,仅对基础Collection生效
	Min() IMix
	// Contains 判断是否包含某个元素,(并不进行定位),对基础Collection生效
	Contains(obj interface{}) bool
	// ContainsCount 判断包含某个元素的个数,返回0代表没有找到,返回正整数代表个数。必须设置compare函数
	ContainsCount(obj interface{}) int
	// Diff 比较两个数组,获取第一个数组不在第二个数组中的元素,组成新数组
	Diff(arr ICollection) ICollection
	// Sort 进行排序, 升序
	Sort() ICollection
	// SortDesc 进行排序,倒序
	SortDesc() ICollection
	// Join 进行拼接
	Join(split string, format ...func(item interface{}) string) string
	// Union 两个集合的并集
	Union(arr ICollection) ICollection
	// Intersect 两个集合的交集
	Intersect(arr ICollection) ICollection

	/*
		下面的方法对基础Collection生效
	*/
	// Avg 获取平均值
	Avg() IMix
	// Median 获取中位值
	Median() IMix
	// Mode 获取Mode值
	Mode() IMix
	// Sum 获取sum值
	Sum() IMix

	/*
		下面的方法对根据不同的对象,进行不同的调用转换
	*/
	// ToStrings 转化为golang原生的字符数组,仅对StrCollection生效
	ToStrings() ([]string, error)
	// ToInt64s 转化为golang原生的Int64数组,仅对Int64Collection生效
	ToInt64s() ([]int64, error)
	// ToInt32s 转化为golang原生的Int32数组,仅对Int32Collection生效
	ToInt32s() ([]int32, error)
	// ToInts 转化为golang原生的Int数组,仅对IntCollection生效
	ToInts() ([]int, error)
	// ToUInt64s 转化为golang原生的UInt64数组,仅对UInt64Collection生效
	ToUInt64s() ([]uint64, error)
	// ToUInt32s 转化为golang原生的UInt32数组,仅对UInt32Collection生效
	ToUInt32s() ([]uint32, error)
	// ToUInts 转化为golang原生的UInt数组,仅对UIntCollection生效
	ToUInts() ([]uint, error)
	// ToMixs 转化为obj数组
	ToMixs() ([]IMix, error)
	// ToFloat64s 转化为float64数组
	ToFloat64s() ([]float64, error)
	// ToFloat32s 转化为float32数组
	ToFloat32s() ([]float32, error)
	// ToInterfaces 转化为interface{} 数组
	ToInterfaces() ([]interface{}, error)
	// ToObjs 转化为objs{}数组
	ToObjs(interface{}) error

	// ToJson 转换为Json
	ToJson() ([]byte, error)
	// FromJson 从json数组转换
	FromJson([]byte) error
}

ICollection 表示数组结构,有几种类型

func NewEmptyMixCollection

func NewEmptyMixCollection() ICollection

func NewMixCollection

func NewMixCollection(typ reflect.Type) ICollection

根据typ 创建一个新的Mix数组

type IMix

type IMix interface {
	Err() error
	SetErr(err error) IMix

	Compare(n IMix) (int, error) // 两个IMix结构是否相同
	Type() reflect.Type          // 获取类型
	SetCompare(func(a interface{}, b interface{}) int) IMix

	Add(mix IMix) (IMix, error) // 加法操作
	Sub(mix IMix) (IMix, error) // 减法操作
	Div(n int) (IMix, error)    // 除法操作
	Multi(n int) (IMix, error)  // 乘法操作

	ToString() (string, error)
	ToInt64() (int64, error)
	ToInt32() (int32, error)
	ToInt() (int, error)
	ToUInt64() (uint64, error)
	ToUInt32() (uint32, error)
	ToUInt() (uint, error)
	ToFloat64() (float64, error)
	ToFloat32() (float32, error)
	ToInterface() (interface{}, error) // 所有函数可用

	MustToString() string
	MustToInt64() int64
	MustToInt32() int32
	MustToInt() int
	MustToUInt64() uint64
	MustToUInt32() uint32
	MustToUInt() uint
	MustToFloat64() float64
	MustToFloat32() float32
	MustToInterface() interface{}

	Format() string // 打印成string
	DD()

	SetField(key string, val interface{}) IMix
	RemoveFields(...string) IMix
}

IMix是一个混合结构

type Int32Collection

type Int32Collection struct {
	AbsCollection
	// contains filtered or unexported fields
}

func NewInt32Collection

func NewInt32Collection(objs []int32) *Int32Collection

NewInt32Collection create a new Int32Collection

func (*Int32Collection) Copy

func (arr *Int32Collection) Copy() ICollection

Copy copy collection

func (*Int32Collection) Count

func (arr *Int32Collection) Count() int

func (*Int32Collection) DD

func (arr *Int32Collection) DD()

func (*Int32Collection) FromJson

func (arr *Int32Collection) FromJson(data []byte) error

func (*Int32Collection) Index

func (arr *Int32Collection) Index(i int) IMix

func (*Int32Collection) Insert

func (arr *Int32Collection) Insert(index int, obj interface{}) ICollection

func (*Int32Collection) NewEmpty

func (arr *Int32Collection) NewEmpty(err ...error) ICollection

func (*Int32Collection) Remove

func (arr *Int32Collection) Remove(i int) ICollection

func (*Int32Collection) SetIndex

func (arr *Int32Collection) SetIndex(i int, val interface{}) ICollection

func (*Int32Collection) ToJson

func (arr *Int32Collection) ToJson() ([]byte, error)

type Int64Collection

type Int64Collection struct {
	AbsCollection
	// contains filtered or unexported fields
}

func NewInt64Collection

func NewInt64Collection(objs []int64) *Int64Collection

NewInt64Collection create a new Int64Collection

func (*Int64Collection) Copy

func (arr *Int64Collection) Copy() ICollection

Copy copy collection

func (*Int64Collection) Count

func (arr *Int64Collection) Count() int

func (*Int64Collection) DD

func (arr *Int64Collection) DD()

func (*Int64Collection) FromJson

func (arr *Int64Collection) FromJson(data []byte) error

func (*Int64Collection) Index

func (arr *Int64Collection) Index(i int) IMix

func (*Int64Collection) Insert

func (arr *Int64Collection) Insert(index int, obj interface{}) ICollection

func (*Int64Collection) NewEmpty

func (arr *Int64Collection) NewEmpty(err ...error) ICollection

func (*Int64Collection) Remove

func (arr *Int64Collection) Remove(i int) ICollection

func (*Int64Collection) SetIndex

func (arr *Int64Collection) SetIndex(i int, val interface{}) ICollection

func (*Int64Collection) ToJson

func (arr *Int64Collection) ToJson() ([]byte, error)

type IntCollection

type IntCollection struct {
	AbsCollection
	// contains filtered or unexported fields
}

func NewIntCollection

func NewIntCollection(objs []int) *IntCollection

NewIntCollection create a new IntCollection

func (*IntCollection) Copy

func (arr *IntCollection) Copy() ICollection

Copy copy collection

func (*IntCollection) Count

func (arr *IntCollection) Count() int

func (*IntCollection) DD

func (arr *IntCollection) DD()

func (*IntCollection) FromJson

func (arr *IntCollection) FromJson(data []byte) error

func (*IntCollection) Index

func (arr *IntCollection) Index(i int) IMix

func (*IntCollection) Insert

func (arr *IntCollection) Insert(index int, obj interface{}) ICollection

func (*IntCollection) NewEmpty

func (arr *IntCollection) NewEmpty(err ...error) ICollection

func (*IntCollection) Remove

func (arr *IntCollection) Remove(i int) ICollection

func (*IntCollection) SetIndex

func (arr *IntCollection) SetIndex(i int, val interface{}) ICollection

func (*IntCollection) ToJson

func (arr *IntCollection) ToJson() ([]byte, error)

type Mix

type Mix struct {
	IMix
	// contains filtered or unexported fields
}

func NewEmptyMix

func NewEmptyMix() *Mix

func NewErrorMix

func NewErrorMix(err error) *Mix

func NewMix

func NewMix(real interface{}) *Mix

func (*Mix) Add

func (m *Mix) Add(n IMix) (IMix, error)

func (*Mix) Compare

func (m *Mix) Compare(n IMix) (ret int, err error)

Equal 判断两个Mix是否相等

func (*Mix) DD

func (m *Mix) DD()

func (*Mix) Div

func (m *Mix) Div(n int) (IMix, error)

func (*Mix) Err

func (m *Mix) Err() error

func (*Mix) Format

func (m *Mix) Format() string

func (*Mix) MarshalJSON

func (m *Mix) MarshalJSON() ([]byte, error)

func (*Mix) Multi

func (m *Mix) Multi(n int) (IMix, error)

func (*Mix) MustToFloat32

func (m *Mix) MustToFloat32() float32

func (*Mix) MustToFloat64

func (m *Mix) MustToFloat64() float64

func (*Mix) MustToInt

func (m *Mix) MustToInt() int

func (*Mix) MustToInt32

func (m *Mix) MustToInt32() int32

func (*Mix) MustToInt64

func (m *Mix) MustToInt64() int64

func (*Mix) MustToInterface

func (m *Mix) MustToInterface() interface{}

func (*Mix) MustToString

func (m *Mix) MustToString() string

func (*Mix) MustToUInt added in v1.4.0

func (m *Mix) MustToUInt() uint

func (*Mix) MustToUInt32 added in v1.4.0

func (m *Mix) MustToUInt32() uint32

func (*Mix) MustToUInt64 added in v1.4.0

func (m *Mix) MustToUInt64() uint64

func (*Mix) RemoveFields

func (m *Mix) RemoveFields(key ...string) IMix

func (*Mix) SetCompare

func (m *Mix) SetCompare(compare func(a interface{}, b interface{}) int) IMix

func (*Mix) SetErr

func (m *Mix) SetErr(err error) IMix

func (*Mix) SetField

func (m *Mix) SetField(key string, val interface{}) IMix

func (*Mix) Sub

func (m *Mix) Sub(n IMix) (IMix, error)

func (*Mix) ToFloat32

func (m *Mix) ToFloat32() (float32, error)

func (*Mix) ToFloat64

func (m *Mix) ToFloat64() (float64, error)

func (*Mix) ToInt

func (m *Mix) ToInt() (int, error)

func (*Mix) ToInt32

func (m *Mix) ToInt32() (int32, error)

func (*Mix) ToInt64

func (m *Mix) ToInt64() (int64, error)

func (*Mix) ToInterface

func (m *Mix) ToInterface() (interface{}, error)

func (*Mix) ToString

func (m *Mix) ToString() (string, error)

func (*Mix) ToUInt added in v1.4.0

func (m *Mix) ToUInt() (uint, error)

func (*Mix) ToUInt32 added in v1.4.0

func (m *Mix) ToUInt32() (uint32, error)

func (*Mix) ToUInt64 added in v1.4.0

func (m *Mix) ToUInt64() (uint64, error)

func (*Mix) Type

func (m *Mix) Type() reflect.Type

type ObjCollection

type ObjCollection struct {
	AbsCollection
	// contains filtered or unexported fields
}

ObjCollection 代表数组集合

func NewObjCollection

func NewObjCollection(objs interface{}) *ObjCollection

NewObjCollection 根据对象数组创建

func NewObjCollectionByType

func NewObjCollectionByType(typ reflect.Type) *ObjCollection

NewObjCollectionByType 根据类型创建一个空的数组, 传进来的Type是slice类型

func (*ObjCollection) Copy

func (arr *ObjCollection) Copy() ICollection

Copy 复制到新的数组

func (*ObjCollection) Count

func (arr *ObjCollection) Count() int

func (*ObjCollection) DD

func (arr *ObjCollection) DD()

func (*ObjCollection) FromJson

func (arr *ObjCollection) FromJson(data []byte) error

func (*ObjCollection) Index

func (arr *ObjCollection) Index(i int) IMix

func (*ObjCollection) Insert

func (arr *ObjCollection) Insert(index int, obj interface{}) ICollection

func (*ObjCollection) KeyByStrField added in v1.4.2

func (arr *ObjCollection) KeyByStrField(key string) (map[string]interface{}, error)

KeyByStrField 按照某个字段进行排序,倒序

func (*ObjCollection) NewEmpty

func (arr *ObjCollection) NewEmpty(err ...error) ICollection

func (*ObjCollection) Pluck

func (arr *ObjCollection) Pluck(key string) ICollection

Pluck 将对象的某个key作为Slice的value,作为slice返回

func (*ObjCollection) Remove

func (arr *ObjCollection) Remove(i int) ICollection

func (*ObjCollection) SetIndex

func (arr *ObjCollection) SetIndex(i int, val interface{}) ICollection

func (*ObjCollection) SortBy

func (arr *ObjCollection) SortBy(key string) ICollection

SortBy 按照某个字段进行排序

func (*ObjCollection) SortByDesc

func (arr *ObjCollection) SortByDesc(key string) ICollection

SortByDesc 按照某个字段进行排序,倒序

func (*ObjCollection) ToJson

func (arr *ObjCollection) ToJson() ([]byte, error)

func (*ObjCollection) ToObjs

func (arr *ObjCollection) ToObjs(objs interface{}) error

type ObjPointCollection

type ObjPointCollection struct {
	AbsCollection
	// contains filtered or unexported fields
}

ObjCollection 代表数组集合

func NewObjPointCollection

func NewObjPointCollection(objs interface{}) *ObjPointCollection

NewObjCollection 根据对象数组创建

func (*ObjPointCollection) Copy

func (arr *ObjPointCollection) Copy() ICollection

Copy 复制到新的数组

func (*ObjPointCollection) Count

func (arr *ObjPointCollection) Count() int

func (*ObjPointCollection) DD

func (arr *ObjPointCollection) DD()

func (*ObjPointCollection) FromJson

func (arr *ObjPointCollection) FromJson(data []byte) error

func (*ObjPointCollection) Index

func (arr *ObjPointCollection) Index(i int) IMix

func (*ObjPointCollection) Insert

func (arr *ObjPointCollection) Insert(index int, obj interface{}) ICollection

func (*ObjPointCollection) KeyByStrField added in v1.4.2

func (arr *ObjPointCollection) KeyByStrField(key string) (map[string]interface{}, error)

KeyByStrField 按照某个字段进行排序,倒序

func (*ObjPointCollection) NewEmpty

func (arr *ObjPointCollection) NewEmpty(err ...error) ICollection

func (*ObjPointCollection) Pluck

func (arr *ObjPointCollection) Pluck(key string) ICollection

将对象的某个key作为Slice的value,作为slice返回

func (*ObjPointCollection) Remove

func (arr *ObjPointCollection) Remove(i int) ICollection

func (*ObjPointCollection) SetIndex

func (arr *ObjPointCollection) SetIndex(i int, val interface{}) ICollection

func (*ObjPointCollection) SortBy

func (arr *ObjPointCollection) SortBy(key string) ICollection

按照某个字段进行排序

func (*ObjPointCollection) SortByDesc

func (arr *ObjPointCollection) SortByDesc(key string) ICollection

按照某个字段进行排序,倒序

func (*ObjPointCollection) ToJson

func (arr *ObjPointCollection) ToJson() ([]byte, error)

func (*ObjPointCollection) ToObjs

func (arr *ObjPointCollection) ToObjs(objs interface{}) error

type StrCollection

type StrCollection struct {
	AbsCollection
	// contains filtered or unexported fields
}

func NewStrCollection

func NewStrCollection(objs []string) *StrCollection

func (*StrCollection) Copy

func (arr *StrCollection) Copy() ICollection

Copy copy collection

func (*StrCollection) Count

func (arr *StrCollection) Count() int

func (*StrCollection) DD

func (arr *StrCollection) DD()

func (*StrCollection) FromJson

func (arr *StrCollection) FromJson(data []byte) error

func (*StrCollection) Index

func (arr *StrCollection) Index(i int) IMix

func (*StrCollection) Insert

func (arr *StrCollection) Insert(index int, obj interface{}) ICollection

func (*StrCollection) NewEmpty

func (arr *StrCollection) NewEmpty(err ...error) ICollection

func (*StrCollection) Remove

func (arr *StrCollection) Remove(i int) ICollection

func (*StrCollection) SetIndex

func (arr *StrCollection) SetIndex(i int, val interface{}) ICollection

func (*StrCollection) ToJson

func (arr *StrCollection) ToJson() ([]byte, error)

type UInt32Collection added in v1.4.0

type UInt32Collection struct {
	AbsCollection
	// contains filtered or unexported fields
}

func NewUInt32Collection added in v1.4.0

func NewUInt32Collection(objs []uint32) *UInt32Collection

NewUInt32Collection create a new UInt32Collection

func (*UInt32Collection) Copy added in v1.4.0

func (arr *UInt32Collection) Copy() ICollection

Copy copy collection

func (*UInt32Collection) Count added in v1.4.0

func (arr *UInt32Collection) Count() int

func (*UInt32Collection) DD added in v1.4.0

func (arr *UInt32Collection) DD()

func (*UInt32Collection) FromJson added in v1.4.0

func (arr *UInt32Collection) FromJson(data []byte) error

func (*UInt32Collection) Index added in v1.4.0

func (arr *UInt32Collection) Index(i int) IMix

func (*UInt32Collection) Insert added in v1.4.0

func (arr *UInt32Collection) Insert(index int, obj interface{}) ICollection

func (*UInt32Collection) NewEmpty added in v1.4.0

func (arr *UInt32Collection) NewEmpty(err ...error) ICollection

func (*UInt32Collection) Remove added in v1.4.0

func (arr *UInt32Collection) Remove(i int) ICollection

func (*UInt32Collection) SetIndex added in v1.4.0

func (arr *UInt32Collection) SetIndex(i int, val interface{}) ICollection

func (*UInt32Collection) ToJson added in v1.4.0

func (arr *UInt32Collection) ToJson() ([]byte, error)

type UInt64Collection added in v1.4.0

type UInt64Collection struct {
	AbsCollection
	// contains filtered or unexported fields
}

func NewUInt64Collection added in v1.4.0

func NewUInt64Collection(objs []uint64) *UInt64Collection

NewUInt64Collection create a new UInt64Collection

func (*UInt64Collection) Copy added in v1.4.0

func (arr *UInt64Collection) Copy() ICollection

Copy copy collection

func (*UInt64Collection) Count added in v1.4.0

func (arr *UInt64Collection) Count() int

func (*UInt64Collection) DD added in v1.4.0

func (arr *UInt64Collection) DD()

func (*UInt64Collection) FromJson added in v1.4.0

func (arr *UInt64Collection) FromJson(data []byte) error

func (*UInt64Collection) Index added in v1.4.0

func (arr *UInt64Collection) Index(i int) IMix

func (*UInt64Collection) Insert added in v1.4.0

func (arr *UInt64Collection) Insert(index int, obj interface{}) ICollection

func (*UInt64Collection) NewEmpty added in v1.4.0

func (arr *UInt64Collection) NewEmpty(err ...error) ICollection

func (*UInt64Collection) Remove added in v1.4.0

func (arr *UInt64Collection) Remove(i int) ICollection

func (*UInt64Collection) SetIndex added in v1.4.0

func (arr *UInt64Collection) SetIndex(i int, val interface{}) ICollection

func (*UInt64Collection) ToJson added in v1.4.0

func (arr *UInt64Collection) ToJson() ([]byte, error)

type UIntCollection added in v1.4.0

type UIntCollection struct {
	AbsCollection
	// contains filtered or unexported fields
}

func NewUIntCollection added in v1.4.0

func NewUIntCollection(objs []uint) *UIntCollection

NewUIntCollection create a new IntCollection

func (*UIntCollection) Copy added in v1.4.0

func (arr *UIntCollection) Copy() ICollection

Copy copy collection

func (*UIntCollection) Count added in v1.4.0

func (arr *UIntCollection) Count() int

func (*UIntCollection) DD added in v1.4.0

func (arr *UIntCollection) DD()

func (*UIntCollection) FromJson added in v1.4.0

func (arr *UIntCollection) FromJson(data []byte) error

func (*UIntCollection) Index added in v1.4.0

func (arr *UIntCollection) Index(i int) IMix

func (*UIntCollection) Insert added in v1.4.0

func (arr *UIntCollection) Insert(index int, obj interface{}) ICollection

func (*UIntCollection) NewEmpty added in v1.4.0

func (arr *UIntCollection) NewEmpty(err ...error) ICollection

func (*UIntCollection) Remove added in v1.4.0

func (arr *UIntCollection) Remove(i int) ICollection

func (*UIntCollection) SetIndex added in v1.4.0

func (arr *UIntCollection) SetIndex(i int, val interface{}) ICollection

func (*UIntCollection) ToJson added in v1.4.0

func (arr *UIntCollection) ToJson() ([]byte, error)

Jump to

Keyboard shortcuts

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