append

Append挂载一个元素到当前Collection,如果挂载的元素类型不一致,则会在Collection中产生Error

sSlice := []int{1, 2}   //没变
intColl := NewIntCollection(sSlice)
intColl.Append(3)
if intColl.Err() == nil {intColl.DD()
}

IsEmpty

IsEmpty() bool

判断一个Collection是否为空,为空返回true, 否则返回false

intColl := NewIntCollection([]int{1,2})
println(intColl.IsEmpty())  // false

IsNotEmpty

IsNotEmpty() bool

判断一个Collection是否为空,为空返回false,否则返回true

intColl := NewIntCollection([]int{1,2})
println(intColl.IsNotEmpty()) // true

Filter根据过滤函数获取Collection过滤后的元素

Filter(func(item interface{}, key int) bool) ICollection

intColl := NewIntCollection([]int{1, 2, 2, 3})
intColl.Filter(func(obj interface{}, index int) bool {val := obj.(int)if val == 2 {return true}return false
}).DD()

Reject将满足过滤条件的元素删除

Reject(func(item interface{}, key int) bool) ICollection

intColl := NewIntCollection([]int{1, 2, 3, 4, 5})
retColl := intColl.Reject(func(item interface{}, key int) bool {i := item.(int)return i > 3
})
if retColl.Count() != 3 {t.Error("Reject 重复错误")
}retColl.DD()

Each对Collection中的每个函数都进行一次函数调用,传入的参数是回调函数

Each(func(item interface{}, key int))

如果希望在某次调用的时候中止,在此次调用的时候设置Collection的Error,就可以中止调用。

intColl := NewIntCollection([]int{1, 2, 3, 4})
sum := 0
intColl.Each(func(item interface{}, key int) {v := item.(int)sum = sum + v
})if intColl.Err() != nil {t.Error(intColl.Err())
}if sum != 10 {t.Error("Each 错误")
}sum = 0
intColl.Each(func(item interface{}, key int) {v := item.(int)sum = sum + vif sum > 4 {intColl.SetErr(errors.New("stop the cycle"))return}
})if sum != 6 {t.Error("Each 错误")
}

Every判断Collection中的每个元素是否都符合某个条件,只有当每个元素都符合条件,才整体返回true,否则返回false

Every(func(item interface{}, key int) bool) bool

intColl := NewIntCollection([]int{1, 2, 3, 4})
if intColl.Every(func(item interface{}, key int) bool {i := item.(int)return i > 1
}) != false {t.Error("Every错误")
}if intColl.Every(func(item interface{}, key int) bool {i := item.(int)return i > 0
}) != true {t.Error("Every错误")
}

ForPage将Collection函数进行分页,按照每页第二个参数的个数,获取第一个参数的页数数据

ForPage(page int, perPage int) ICollection

intColl := NewIntCollection([]int{1, 2, 3, 4, 5, 6})
ret := intColl.ForPage(1, 2)
ret.DD()if ret.Count() != 2 {t.Error("For page错误")
}/*
IntCollection(2):{0:    31: 4
}

展示

业务开发最核心的也就是对数组的处理,Collection封装了多种数据数组类型。

Collection包目前支持的元素类型:int, int64, float32, float64, string, struct。除了struct数组使用了反射之外,其他的数组并没有使用反射机制,效率和易用性得到一定的平衡。

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

NewIntCollection(objs []int) *IntCollectionNewInt64Collection(objs []int64) *Int64CollectionNewFloat64Collection(objs []float64) *Float64CollectionNewFloat32Collection(objs []float32) *Float32CollectionNewStrCollection(objs []string) *StrCollectionNewObjCollection(objs interface{}) *ObjCollection

所有的初始化函数都是很方便的将要初始化的slice传递进入,返回了一个实现了ICollection的具体对象

格式展示

首先业务是很需要进行代码调试的,这里封装了一个 DD 方法,能按照友好的格式展示这个 Collection

a1 := Foo{A: "a1"}
a2 := Foo{A: "a2"}objColl := NewObjCollection([]Foo{a1, a2})
objColl.DD()

查找功能

在一个数组中查找对应的元素,这个是非常常见的功能

Search(item interface{}) int

查找Collection中第一个匹配查询元素的下标,如果存在,返回下标;如果不存在,返回-1

注意 此函数要求设置compare方法,基础元素数组(int, int64, float32, float64, string)可直接调用!

intColl := collection.NewIntCollection([]int{1,2})
fmt.Println(intColl.Search(2))intColl = collection.NewIntCollection([]int{1,2, 3, 3, 2})
fmt.Println(intColl.Search(3))

排重功能Unique

将Collection中重复的元素进行合并,返回唯一的一个数组。

intColl := NewIntCollection([]int{1,2, 3, 3, 2})
uniqColl := intColl.Unique()
if uniqColl.Count() != 3 {t.Error("Unique 重复错误")
}uniqColl.DD()

获取最后一个

获取该Collection中满足过滤的最后一个元素,如果没有填写过滤条件,默认返回最后一个元素

intColl := NewIntCollection([]int{1, 2, 3, 4, 3, 2})
last, err := intColl.Last().ToInt()
if err != nil {t.Error("last get error")
}
if last != 2 {t.Error("last 获取错误")
}last, err = intColl.Last(func(item interface{}, key int) bool {i := item.(int)return i > 2
}).ToInt()if err != nil {t.Error("last get error")
}
if last != 3 {t.Error("last 获取错误")
}

Map/reduce

Map

Map(func(item interface{}, key int) interface{}) ICollection

对Collection中的每个函数都进行一次函数调用,并将返回值组装成ICollection

这个回调函数形如:func(item interface{}, key int) interface{}

如果希望在某此调用的时候中止,就在此次调用的时候设置Collection的Error,就可以中止,且此次回调函数生成的结构不合并到最终生成的ICollection

intColl := collection.NewIntCollection([]int{1, 2, 3, 4})
newIntColl := intColl.Map(func(item interface{}, key int) interface{} {v := item.(int)return v * 2
})
newIntColl.DD()if newIntColl.Count() != 4 {fmt.Println("Map错误")
}

中止map

intColl := collection.NewIntCollection([]int{1, 2, 3, 4})
newIntColl2 := intColl.Map(func(item interface{}, key int) interface{} {v := item.(int)if key > 2 {intColl.SetErr(errors.New("break"))return nil}return v * 2
})
newIntColl2.DD()

Reduce

Reduce(func(carry IMix, item IMix) IMix) IMix
对Collection中的所有元素进行聚合计算。

如果希望在某次调用的时候中止,在此次调用的时候设置Collection的Error,就可以中止调用

intColl := collection.NewIntCollection([]int{1, 2, 3, 4})
sumMix := intColl.Reduce(func(carry collection.IMix, item collection.IMix) collection.IMix {carryInt, _ := carry.ToInt()itemInt, _ := item.ToInt()return collection.NewMix(carryInt + itemInt)
})sumMix.DD()sum, err := sumMix.ToInt()
if err != nil {fmt.Println(err.Error())
}
if sum != 10 {fmt.Println("Reduce计算错误")
}

sort排列

将Collection中的元素进行升序排列输出

intColl := NewIntCollection([]int{2, 4, 3})
intColl2 := intColl.Sort()
if intColl2.Err() != nil {fmt.Println(intColl2.Err())
}
intColl2.DD()

SortDesc降序

SortDesc() ICollection

将Collection中的元素按照降序排列输出,必须设置compare函数

intColl := NewIntCollection([]int{2, 4, 3})
intColl2 := intColl.SortDesc()
if intColl2.Err() != nil {t.Error(intColl2.Err())
}
intColl2.DD()

SortBy

SortBy(key string) ICollection

根据对象数组中的某个元素进行Collection升序排列。这个元素必须是Public元素

注:这个函数只对ObjCollection生效。这个对象数组的某个元素必须是基础类型。

type Foo struct {A stringB int
}func TestObjCollection_SortBy(t *testing.T) {a1 := Foo{A: "a1", B: 3}a2 := Foo{A: "a2", B: 2}objColl := NewObjCollection([]Foo{a1, a2})newObjColl := objColl.SortBy("B")newObjColl.DD()obj, err := newObjColl.Index(0).ToInterface()if err != nil {t.Error(err)}foo := obj.(Foo)if foo.B != 2 {t.Error("SortBy error")}
}/*
ObjCollection(2)(collection.Foo):{0:    {A:a2 B:2}1:    {A:a1 B:3}
}
*/

SortByDesc

SortByDesc(key string) ICollection

根据对象数组中的某个元素进行Collection降序排列。这个元素必须是Public元素

注:这个函数只对ObjCollection生效。这个对象数组的某个元素必须是基础类型。

type Foo struct {A stringB int
}func collection_SortByDesc() {a1 := Foo{A: "a1", B: 2}a2 := Foo{A: "a2", B: 3}objColl := NewObjCollection([]Foo{a1, a2})newObjColl := objColl.SortByDesc("B")newObjColl.DD()obj, _ := newObjColl.Index(0).ToInterface()foo := obj.(Foo)fmt.Println(foo)
}

合并join

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

将Collection中的元素按照某种方式聚合成字符串。该函数接受一个或者两个参数,第一个参数是聚合字符串的分隔符号,第二个参数是聚合时候每个元素的格式化函数,如果没有设置第二个参数,则使用fmt.Sprintf("%v")来该格式化

intColl := NewIntCollection([]int{2, 4, 3})
out := intColl.Join(",")
fmt.Println(out)out = intColl.Join(",", func(item interface{}) string {return fmt.Sprintf("'%d'", item.(int))
})
fmt.Println(out)

golang中的collection相关推荐

  1. golang中连接mongo数据库并进行操作

    golang中连接mongo数据库并进行操作 golang中连接mongo数据库并进行操作 首先,我们需要下载mongo模块 执行 go get github.com/mongodb/mongo-go ...

  2. Golang中Buffer高效拼接字符串以及自定义线程安全Buffer

    本文原创文章,转载注明出处,博客地址 https://segmentfault.com/u/to... 第一时间看后续精彩文章.觉得好的话,顺手分享到朋友圈吧,感谢支持. Go中可以使用"+ ...

  3. 如何在golang中关闭bufio.reader_Golang 并发模型系列:1. 轻松入门流水线模型

    Go语言中文网,致力于每日分享编码.开源等知识,欢迎关注我,会有意想不到的收获! Golang作为一个实用主义的编程语言,非常注重性能,在语言特性上天然支持并发,它有多种并发模型,通过流水线模型系列文 ...

  4. go语言的iota是什么意思_关于Golang中的iota

    快速一览 iota是Golang中提供的一个简化常量和枚举编程的标识符,合理的使用这个标识符可以让代码变得更简洁,省去大量的不必要的代码. 比如下面的这个常量定义 const ( a = 1 b = ...

  5. Golang中的panic和recover(捕获异常)

    Golang中的panic和recover(捕获异常) 参考文章: (1)Golang中的panic和recover(捕获异常) (2)https://www.cnblogs.com/zhzhlong ...

  6. golang 中string和int类型相互转换

    总结了golang中字符串和各种int类型之间的相互转换方式: string转成int: test_int, err := strconv.Atoi(test_string) if err != ni ...

  7. golang中并发sync和channel

    golang中并发sync和channel chenbaoke · 2014-12-08 13:00:01 · 19151 次点击 · 预计阅读时间 5 分钟 · 不到1分钟之前 开始浏览 这是一个创 ...

  8. golang中的sync.WaitGroup

    golang中的sync.WaitGroup Posted on 2015/04/09刚才看golang的sync的包,看见一个很有用的功能.就是WaitGroup. 先说说WaitGroup的用途: ...

  9. 初步解读Golang中的接口相关编写方法

    初步解读Golang中的接口相关编写方法 概述如果说goroutine和channel是Go并发的两大基石,那么接口是Go语言编程中数据类型的关键.在Go语言的实际编程中,几乎所有的数据结构都围绕接口 ...

最新文章

  1. linux的那些破事
  2. 上海师范大学提前批计算机科学与技术,2020年上海师范大学提前批录取分数线公布...
  3. 获取图片像素颜色,转换为CSS3 box-shadow显示
  4. TDD测试驱动开发过程
  5. 从头开始敲代码之《从BaseApplication/Activity开始》
  6. pythonweb开发工作内容_我的第一个python web开发框架(5)——开发前准备工作(了解编码前需要知道的一些常识)...
  7. 分布式训练PyTorch 源码解读
  8. Windows 下修改 MySQL 编码为 utf8
  9. FREESPACE 发布 logo v1.1
  10. XenApp / XenDesktop 7.6 初体验三 StoreFront和Citrix Receiver
  11. SQL语法提示工具SQL Prompt 发布v10.6
  12. Windows 引导修复
  13. java代码编写九九乘法表
  14. STM32F407极简串口设置
  15. python音频分析(一)绘制wav的时域频域图
  16. 【定量分析、量化金融与统计学】分类变量与多元非线性关系与子集的选择
  17. 单片机STM32开发环境的安装
  18. vue项目中常见的跨域问题解决
  19. IPQ4xx Ethernet Analysis
  20. [转帖]房博士教你购房(二)

热门文章

  1. 【MATLAB】进阶绘图 ( Bar 条形图 | bar 函数 | bar3 函数 | Bar 条形图样式 | 堆叠条形图 | 水平条形图 | barh 函数 )
  2. 【Android NDK 开发】Android.mk 配置静态库 ( Android Studio 配置静态库 | 配置动态库与静态库区别 | 动态库与静态库打包对比 )
  3. Subversion基本操作
  4. JavaSript模块规范 - AMD规范与CMD规范介绍[转]
  5. [Windows]ping itsafe环境变量
  6. 操作笔记:tomcat在正式环境的常见问题和idea的远程调试
  7. IE11与Cognos怪谈
  8. Shell的一些基本用法
  9. uCOS-II 学习笔记--------OSInit函数
  10. ROS学习(二):在ubuntu 16.04安装ROS Kinetic