日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

golang中的collection

發布時間:2025/6/15 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 golang中的collection 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。