<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [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) ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看