[TOC]
# 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
~~~go
intColl := NewIntCollection([]int{1,2})
println(intColl.IsEmpty()) // false
~~~
# IsNotEmpty
`IsNotEmpty() bool`
判斷一個Collection是否為空,為空返回false,否則返回true
~~~go
intColl := NewIntCollection([]int{1,2})
println(intColl.IsNotEmpty()) // true
~~~
# Filter根據過濾函數獲取Collection過濾后的元素
`Filter(func(item interface{}, key int) bool) ICollection`
~~~go
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`
~~~go
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,就可以中止調用。
~~~go
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 + v
if 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`
~~~go
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`
~~~go
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: 3
1: 4
}
~~~
# 展示
業務開發最核心的也就是對數組的處理,Collection封裝了多種數據數組類型。
Collection包目前支持的元素類型:int, int64, float32, float64, string, struct。除了struct數組使用了反射之外,其他的數組并沒有使用反射機制,效率和易用性得到一定的平衡。
使用下列幾個方法進行初始化Collection:
~~~
NewIntCollection(objs []int) *IntCollection
NewInt64Collection(objs []int64) *Int64Collection
NewFloat64Collection(objs []float64) *Float64Collection
NewFloat32Collection(objs []float32) *Float32Collection
NewStrCollection(objs []string) *StrCollection
NewObjCollection(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函數
~~~go
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生效。這個對象數組的某個元素必須是基礎類型。
~~~go
type Foo struct {
A string
B 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 string
B 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)
~~~
- 基礎
- 簡介
- 主要特征
- 變量和常量
- 編碼轉換
- 數組
- byte與rune
- big
- sort接口
- 和mysql類型對應
- 函數
- 閉包
- 工作區
- 復合類型
- 指針
- 切片
- map
- 結構體
- sync.Map
- 隨機數
- 面向對象
- 匿名組合
- 方法
- 接口
- 權限
- 類型查詢
- 異常處理
- error
- panic
- recover
- 自定義錯誤
- 字符串處理
- 正則表達式
- json
- 文件操作
- os
- 文件讀寫
- 目錄
- bufio
- ioutil
- gob
- 棧幀的內存布局
- shell
- 時間處理
- time詳情
- time使用
- new和make的區別
- container
- list
- heap
- ring
- 測試
- 單元測試
- Mock依賴
- delve
- 命令
- TestMain
- path和filepath包
- log日志
- 反射
- 詳解
- plugin包
- 信號
- goto
- 協程
- 簡介
- 創建
- 協程退出
- runtime
- channel
- select
- 死鎖
- 互斥鎖
- 讀寫鎖
- 條件變量
- 嵌套
- 計算單個協程占用內存
- 執行規則
- 原子操作
- WaitGroup
- 定時器
- 對象池
- sync.once
- 網絡編程
- 分層模型
- socket
- tcp
- udp
- 服務端
- 客戶端
- 并發服務器
- Http
- 簡介
- http服務器
- http客戶端
- 爬蟲
- 平滑重啟
- context
- httptest
- 優雅中止
- web服務平滑重啟
- beego
- 安裝
- 路由器
- orm
- 單表增刪改查
- 多級表
- orm使用
- 高級查詢
- 關系查詢
- SQL查詢
- 元數據二次定義
- 控制器
- 參數解析
- 過濾器
- 數據輸出
- 表單數據驗證
- 錯誤處理
- 日志
- 模塊
- cache
- task
- 調試模塊
- config
- 部署
- 一些包
- gjson
- goredis
- collection
- sjson
- redigo
- aliyunoss
- 密碼
- 對稱加密
- 非對稱加密
- 單向散列函數
- 消息認證
- 數字簽名
- mysql優化
- 常見錯誤
- go run的錯誤
- 新手常見錯誤
- 中級錯誤
- 高級錯誤
- 常用工具
- 協程-泄露
- go env
- gometalinter代碼檢查
- go build
- go clean
- go test
- 包管理器
- go mod
- gopm
- go fmt
- pprof
- 提高編譯
- go get
- 代理
- 其他的知識
- go內存對齊
- 細節總結
- nginx路由匹配
- 一些博客
- redis為什么快
- cpu高速緩存
- 常用命令
- Go 永久阻塞的方法
- 常用技巧
- 密碼加密解密
- for 循環迭代變量
- 備注
- 垃圾回收
- 協程和纖程
- tar-gz
- 紅包算法
- 解決golang.org/x 下載失敗
- 逃逸分析
- docker
- 鏡像
- 容器
- 數據卷
- 網絡管理
- 網絡模式
- dockerfile
- docker-composer
- 微服務
- protoBuf
- GRPC
- tls
- consul
- micro
- crontab
- shell調用
- gorhill/cronexpr
- raft
- go操作etcd
- mongodb