Collection包目标是用于替换golang原生的Slice,使用场景是在大量不追求极致性能,追求业务开发效能的场景。
Collection的使用手册线上地址:http://collection.funaio.cn/
你也可以通过类库直接在本地启动本地文档:(需要本机安装npm)
npm install npm run docs:dev // 访问地址: http://localhost:2333/
| 版本 | 说明 |
|---|---|
| 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) *IntCollectionNewInt64Collection(objs []int64) *Int64CollectionNewInt32Collection(objs []int32) *Int32CollectionNewUIntCollection(objs []uint) *UIntCollectionNewUInt64Collection(objs []uint64) *UInt64CollectionNewUInt32Collection(objs []uint32) *UInt32CollectionNewFloat64Collection(objs []float64) *Float64CollectionNewFloat32Collection(objs []float32) *Float32CollectionNewStrCollection(objs []string) *StrCollectionNewObjCollection(objs interface{}) *ObjCollectionNewObjPointCollection(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是空,返回trueEvery(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/*下面的方法对基础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 转换为JsonToJson() ([]byte, error)// FromJson 从json数组转换FromJson([]byte) error}
