Merge branch 'gogf:master' into master

This commit is contained in:
mingzaily
2021-11-06 16:12:38 +08:00
committed by GitHub
20 changed files with 4935 additions and 112 deletions

View File

@ -0,0 +1,709 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package garray_test
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/util/gconv"
)
func ExampleIntArray_Walk() {
var array garray.IntArray
tables := g.SliceInt{10, 20}
prefix := 99
array.Append(tables...)
// Add prefix for given table names.
array.Walk(func(value int) int {
return prefix + value
})
fmt.Println(array.Slice())
// Output:
// [109 119]
}
func ExampleNewIntArray() {
s := garray.NewIntArray()
s.Append(10)
s.Append(20)
s.Append(15)
s.Append(30)
fmt.Println(s.Slice())
// Output:
// [10 20 15 30]
}
func ExampleNewIntArraySize() {
s := garray.NewIntArraySize(3, 5)
s.Set(0, 10)
s.Set(1, 20)
s.Set(2, 15)
s.Set(3, 30)
fmt.Println(s.Slice(), s.Len(), cap(s.Slice()))
// Output:
// [10 20 15] 3 5
}
func ExampleNewIntArrayFrom() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30})
fmt.Println(s.Slice(), s.Len(), cap(s.Slice()))
// Output:
// [10 20 15 30] 4 4
}
func ExampleNewIntArrayFromCopy() {
s := garray.NewIntArrayFromCopy(g.SliceInt{10, 20, 15, 30})
fmt.Println(s.Slice(), s.Len(), cap(s.Slice()))
// Output:
// [10 20 15 30] 4 4
}
func ExampleIntArray_At() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30})
sAt := s.At(2)
fmt.Println(sAt)
// Output:
// 15
}
func ExampleIntArray_Get() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30})
sGet, sBool := s.Get(3)
fmt.Println(sGet, sBool)
// Output:
// 30 true
}
func ExampleIntArray_Set() {
s := garray.NewIntArraySize(3, 5)
s.Set(0, 10)
s.Set(1, 20)
s.Set(2, 15)
s.Set(3, 30)
fmt.Println(s.Slice())
// Output:
// [10 20 15]
}
func ExampleIntArray_SetArray() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30})
fmt.Println(s.Slice())
// Output:
// [10 20 15 30]
}
func ExampleIntArray_Replace() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30})
fmt.Println(s.Slice())
s.Replace(g.SliceInt{12, 13})
fmt.Println(s.Slice())
// Output:
// [10 20 15 30]
// [12 13 15 30]
}
func ExampleIntArray_Sum() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30})
a := s.Sum()
fmt.Println(a)
// Output:
// 75
}
func ExampleIntArray_Sort() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30})
a := s.Sort()
fmt.Println(a)
// Output:
// [10,15,20,30]
}
func ExampleIntArray_SortFunc() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30})
fmt.Println(s)
s.SortFunc(func(v1, v2 int) bool {
//fmt.Println(v1,v2)
return v1 > v2
})
fmt.Println(s)
s.SortFunc(func(v1, v2 int) bool {
return v1 < v2
})
fmt.Println(s)
// Output:
// [10,20,15,30]
// [30,20,15,10]
// [10,15,20,30]
}
func ExampleIntArray_InsertBefore() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30})
s.InsertBefore(1, 99)
fmt.Println(s.Slice())
// Output:
// [10 99 20 15 30]
}
func ExampleIntArray_InsertAfter() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30})
s.InsertAfter(1, 99)
fmt.Println(s.Slice())
// Output:
// [10 20 99 15 30]
}
func ExampleIntArray_Remove() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30})
fmt.Println(s)
s.Remove(1)
fmt.Println(s.Slice())
// Output:
// [10,20,15,30]
// [10 15 30]
}
func ExampleIntArray_RemoveValue() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30})
fmt.Println(s)
s.RemoveValue(20)
fmt.Println(s.Slice())
// Output:
// [10,20,15,30]
// [10 15 30]
}
func ExampleIntArray_PushLeft() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30})
fmt.Println(s)
s.PushLeft(96, 97, 98, 99)
fmt.Println(s.Slice())
// Output:
// [10,20,15,30]
// [96 97 98 99 10 20 15 30]
}
func ExampleIntArray_PushRight() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30})
fmt.Println(s)
s.PushRight(96, 97, 98, 99)
fmt.Println(s.Slice())
// Output:
// [10,20,15,30]
// [10 20 15 30 96 97 98 99]
}
func ExampleIntArray_PopLeft() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30})
fmt.Println(s)
s.PopLeft()
fmt.Println(s.Slice())
// Output:
// [10,20,15,30]
// [20 15 30]
}
func ExampleIntArray_PopRight() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30})
fmt.Println(s)
s.PopRight()
fmt.Println(s.Slice())
// Output:
// [10,20,15,30]
// [10 20 15]
}
func ExampleIntArray_PopRand() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60, 70})
fmt.Println(s)
r, _ := s.PopRand()
fmt.Println(s)
fmt.Println(r)
// May Output:
// [10,20,15,30,40,50,60,70]
// [10,20,15,30,40,60,70]
// 50
}
func ExampleIntArray_PopRands() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s)
r := s.PopRands(2)
fmt.Println(s)
fmt.Println(r)
// May Output:
// [10,20,15,30,40,50,60]
// [10,20,15,30,40]
// [50 60]
}
func ExampleIntArray_PopLefts() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s)
r := s.PopLefts(2)
fmt.Println(s)
fmt.Println(r)
// Output:
// [10,20,15,30,40,50,60]
// [15,30,40,50,60]
// [10 20]
}
func ExampleIntArray_PopRights() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s)
r := s.PopRights(2)
fmt.Println(s)
fmt.Println(r)
// Output:
// [10,20,15,30,40,50,60]
// [10,20,15,30,40]
// [50 60]
}
func ExampleIntArray_Range() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s)
r := s.Range(2, 5)
fmt.Println(r)
// Output:
// [10,20,15,30,40,50,60]
// [15 30 40]
}
func ExampleIntArray_SubSlice() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s)
r := s.SubSlice(3, 4)
fmt.Println(r)
// Output:
// [10,20,15,30,40,50,60]
// [30 40 50 60]
}
func ExampleIntArray_Append() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s)
s.Append(96, 97, 98)
fmt.Println(s)
// Output:
// [10,20,15,30,40,50,60]
// [10,20,15,30,40,50,60,96,97,98]
}
func ExampleIntArray_Len() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s)
fmt.Println(s.Len())
// Output:
// [10,20,15,30,40,50,60]
// 7
}
func ExampleIntArray_Slice() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s.Slice())
// Output:
// [10 20 15 30 40 50 60]
}
func ExampleIntArray_Interfaces() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
r := s.Interfaces()
fmt.Println(r)
// Output:
// [10 20 15 30 40 50 60]
}
func ExampleIntArray_Clone() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s)
r := s.Clone()
fmt.Println(r)
// Output:
// [10,20,15,30,40,50,60]
// [10,20,15,30,40,50,60]
}
func ExampleIntArray_Clear() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s)
fmt.Println(s.Clear())
fmt.Println(s)
// Output:
// [10,20,15,30,40,50,60]
// []
// []
}
func ExampleIntArray_Contains() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s.Contains(20))
fmt.Println(s.Contains(21))
// Output:
// true
// false
}
func ExampleIntArray_Search() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s.Search(20))
fmt.Println(s.Search(21))
// Output:
// 1
// -1
}
func ExampleIntArray_Unique() {
s := garray.NewIntArray()
s.SetArray(g.SliceInt{10, 20, 15, 15, 20, 50, 60})
fmt.Println(s)
fmt.Println(s.Unique())
// Output:
// [10,20,15,15,20,50,60]
// [10,20,15,50,60]
}
func ExampleIntArray_LockFunc() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
s.LockFunc(func(array []int) {
for i := 0; i < len(array)-1; i++ {
fmt.Println(array[i])
}
})
// Output:
// 10
// 20
// 15
// 30
// 40
// 50
}
func ExampleIntArray_RLockFunc() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
s.RLockFunc(func(array []int) {
for i := 0; i < len(array); i++ {
fmt.Println(array[i])
}
})
// Output:
// 10
// 20
// 15
// 30
// 40
// 50
// 60
}
func ExampleIntArray_Merge() {
s1 := garray.NewIntArray()
s2 := garray.NewIntArray()
s1.SetArray(g.SliceInt{10, 20, 15})
s2.SetArray(g.SliceInt{40, 50, 60})
fmt.Println(s1)
fmt.Println(s2)
s1.Merge(s2)
fmt.Println(s1)
// Output:
// [10,20,15]
// [40,50,60]
// [10,20,15,40,50,60]
}
func ExampleIntArray_Fill() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s)
s.Fill(2, 3, 99)
fmt.Println(s)
// Output:
// [10,20,15,30,40,50,60]
// [10,20,99,99,99,50,60]
}
func ExampleIntArray_Chunk() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s)
r := s.Chunk(3)
fmt.Println(r)
// Output:
// [10,20,15,30,40,50,60]
// [[10 20 15] [30 40 50] [60]]
}
func ExampleIntArray_Pad() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
s.Pad(8, 99)
fmt.Println(s)
s.Pad(-10, 89)
fmt.Println(s)
// Output:
// [10,20,15,30,40,50,60,99]
// [89,89,10,20,15,30,40,50,60,99]
}
func ExampleIntArray_Rand() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s)
fmt.Println(s.Rand())
// May Output:
// [10,20,15,30,40,50,60]
// 10 true
}
func ExampleIntArray_Rands() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s)
fmt.Println(s.Rands(3))
// May Output:
// [10,20,15,30,40,50,60]
// [20 50 20]
}
func ExampleIntArray_Shuffle() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s)
fmt.Println(s.Shuffle())
// May Output:
// [10,20,15,30,40,50,60]
// [10,40,15,50,20,60,30]
}
func ExampleIntArray_Reverse() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s)
fmt.Println(s.Reverse())
// Output:
// [10,20,15,30,40,50,60]
// [60,50,40,30,15,20,10]
}
func ExampleIntArray_Join() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s)
fmt.Println(s.Join(","))
// Output:
// [10,20,15,30,40,50,60]
// 10,20,15,30,40,50,60
}
func ExampleIntArray_CountValues() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 15, 40, 40, 40})
fmt.Println(s.CountValues())
// Output:
// map[10:1 15:2 20:1 40:3]
}
func ExampleIntArray_Iterator() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
s.Iterator(func(k int, v int) bool {
fmt.Println(k, v)
return true
})
// Output:
// 0 10
// 1 20
// 2 15
// 3 30
// 4 40
// 5 50
// 6 60
}
func ExampleIntArray_IteratorAsc() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
s.IteratorAsc(func(k int, v int) bool {
fmt.Println(k, v)
return true
})
// Output:
// 0 10
// 1 20
// 2 15
// 3 30
// 4 40
// 5 50
// 6 60
}
func ExampleIntArray_IteratorDesc() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
s.IteratorDesc(func(k int, v int) bool {
fmt.Println(k, v)
return true
})
// Output:
// 6 60
// 5 50
// 4 40
// 3 30
// 2 15
// 1 20
// 0 10
}
func ExampleIntArray_String() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s)
fmt.Println(s.String())
// Output:
// [10,20,15,30,40,50,60]
// [10,20,15,30,40,50,60]
}
func ExampleIntArray_MarshalJSON() {
type Student struct {
Id int
Name string
Scores garray.IntArray
}
var array garray.IntArray
array.SetArray(g.SliceInt{98, 97, 96})
s := Student{
Id: 1,
Name: "john",
Scores: array,
}
b, _ := json.Marshal(s)
fmt.Println(string(b))
// Output:
// {"Id":1,"Name":"john","Scores":[98,97,96]}
}
func ExampleIntArray_UnmarshalJSON() {
b := []byte(`{"Id":1,"Name":"john","Scores":[98,96,97]}`)
type Student struct {
Id int
Name string
Scores *garray.IntArray
}
s := Student{}
json.Unmarshal(b, &s)
fmt.Println(s)
// Output:
// {1 john [98,96,97]}
}
func ExampleIntArray_UnmarshalValue() {
type Student struct {
Name string
Scores *garray.IntArray
}
var s *Student
gconv.Struct(g.Map{
"name": "john",
"scores": g.SliceInt{96, 98, 97},
}, &s)
fmt.Println(s)
// Output:
// &{john [96,98,97]}
}
func ExampleIntArray_FilterEmpty() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 40, 50, 0, 0, 0, 60})
fmt.Println(s)
fmt.Println(s.FilterEmpty())
// Output:
// [10,40,50,0,0,0,60]
// [10,40,50,60]
}
func ExampleIntArray_IsEmpty() {
s := garray.NewIntArrayFrom(g.SliceInt{10, 20, 15, 30, 40, 50, 60})
fmt.Println(s.IsEmpty())
s1 := garray.NewIntArray()
fmt.Println(s1.IsEmpty())
// Output:
// false
// true
}

View File

@ -0,0 +1,573 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package garray_test
import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/util/gconv"
)
func ExampleSortedStrArray_Walk() {
var array garray.SortedStrArray
tables := g.SliceStr{"user", "user_detail"}
prefix := "gf_"
array.Append(tables...)
// Add prefix for given table names.
array.Walk(func(value string) string {
return prefix + value
})
fmt.Println(array.Slice())
// Output:
// [gf_user gf_user_detail]
}
func ExampleNewSortedStrArray() {
s := garray.NewSortedStrArray()
s.Append("b")
s.Append("d")
s.Append("c")
s.Append("a")
fmt.Println(s.Slice())
// Output:
// [a b c d]
}
func ExampleNewSortedStrArraySize() {
s := garray.NewSortedStrArraySize(3)
s.SetArray([]string{"b", "d", "a", "c"})
fmt.Println(s.Slice(), s.Len(), cap(s.Slice()))
// Output:
// [a b c d] 4 4
}
func ExampleNewStrArrayFromCopy() {
s := garray.NewSortedStrArrayFromCopy(g.SliceStr{"b", "d", "c", "a"})
fmt.Println(s.Slice())
// Output:
// [a b c d]
}
func ExampleSortedStrArray_At() {
s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "d", "c", "a"})
sAt := s.At(2)
fmt.Println(s)
fmt.Println(sAt)
// Output:
// ["a","b","c","d"]
// c
}
func ExampleSortedStrArray_Get() {
s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "d", "c", "a", "e"})
sGet, sBool := s.Get(3)
fmt.Println(s)
fmt.Println(sGet, sBool)
// Output:
// ["a","b","c","d","e"]
// d true
}
func ExampleSortedStrArray_SetArray() {
s := garray.NewSortedStrArray()
s.SetArray([]string{"b", "d", "a", "c"})
fmt.Println(s.Slice())
// Output:
// [a b c d]
}
func ExampleSortedStrArray_SetUnique() {
s := garray.NewSortedStrArray()
s.SetArray([]string{"b", "d", "a", "c", "c", "a"})
fmt.Println(s.SetUnique(true))
// Output:
// ["a","b","c","d"]
}
func ExampleSortedStrArray_Sum() {
s := garray.NewSortedStrArray()
s.SetArray([]string{"5", "3", "2"})
fmt.Println(s)
a := s.Sum()
fmt.Println(a)
// Output:
// ["2","3","5"]
// 10
}
func ExampleSortedStrArray_Sort() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"b", "d", "a", "c"})
fmt.Println(s)
a := s.Sort()
fmt.Println(a)
// Output:
// ["a","b","c","d"]
// ["a","b","c","d"]
}
func ExampleSortedStrArray_Remove() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"b", "d", "c", "a"})
fmt.Println(s.Slice())
s.Remove(1)
fmt.Println(s.Slice())
// Output:
// [a b c d]
// [a c d]
}
func ExampleSortedStrArray_RemoveValue() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"b", "d", "c", "a"})
fmt.Println(s.Slice())
s.RemoveValue("b")
fmt.Println(s.Slice())
// Output:
// [a b c d]
// [a c d]
}
func ExampleSortedStrArray_PopLeft() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"b", "d", "c", "a"})
r, _ := s.PopLeft()
fmt.Println(r)
fmt.Println(s.Slice())
// Output:
// a
// [b c d]
}
func ExampleSortedStrArray_PopRight() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"b", "d", "c", "a"})
fmt.Println(s.Slice())
r, _ := s.PopRight()
fmt.Println(r)
fmt.Println(s.Slice())
// Output:
// [a b c d]
// d
// [a b c]
}
func ExampleSortedStrArray_PopRights() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"})
r := s.PopRights(2)
fmt.Println(r)
fmt.Println(s)
// Output:
// [g h]
// ["a","b","c","d","e","f"]
}
func ExampleSortedStrArray_Rand() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"})
r, _ := s.PopRand()
fmt.Println(r)
fmt.Println(s)
// May Output:
// b
// ["a","c","d","e","f","g","h"]
}
func ExampleSortedStrArray_PopRands() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"})
r := s.PopRands(2)
fmt.Println(r)
fmt.Println(s)
// May Output:
// [d a]
// ["b","c","e","f","g","h"]
}
func ExampleSortedStrArray_PopLefts() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"})
r := s.PopLefts(2)
fmt.Println(r)
fmt.Println(s)
// Output:
// [a b]
// ["c","d","e","f","g","h"]
}
func ExampleSortedStrArray_Range() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"})
r := s.Range(2, 5)
fmt.Println(r)
// Output:
// [c d e]
}
func ExampleSortedStrArray_SubSlice() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"})
r := s.SubSlice(3, 4)
fmt.Println(s.Slice())
fmt.Println(r)
// Output:
// [a b c d e f g h]
// [d e f g]
}
func ExampleSortedStrArray_Add() {
s := garray.NewSortedStrArray()
s.Add("b", "d", "c", "a")
fmt.Println(s)
// Output:
// ["a","b","c","d"]
}
func ExampleSortedStrArray_Append() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"b", "d", "c", "a"})
fmt.Println(s)
s.Append("f", "e", "g")
fmt.Println(s)
// Output:
// ["a","b","c","d"]
// ["a","b","c","d","e","f","g"]
}
func ExampleSortedStrArray_Len() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"})
fmt.Println(s)
fmt.Println(s.Len())
// Output:
// ["a","b","c","d","e","f","g","h"]
// 8
}
func ExampleSortedStrArray_Slice() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"})
fmt.Println(s.Slice())
// Output:
// [a b c d e f g h]
}
func ExampleSortedStrArray_Interfaces() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"})
r := s.Interfaces()
fmt.Println(r)
// Output:
// [a b c d e f g h]
}
func ExampleSortedStrArray_Clone() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"})
r := s.Clone()
fmt.Println(r)
fmt.Println(s)
// Output:
// ["a","b","c","d","e","f","g","h"]
// ["a","b","c","d","e","f","g","h"]
}
func ExampleSortedStrArray_Clear() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"})
fmt.Println(s)
fmt.Println(s.Clear())
fmt.Println(s)
// Output:
// ["a","b","c","d","e","f","g","h"]
// []
// []
}
func ExampleSortedStrArray_Contains() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"})
fmt.Println(s.Contains("e"))
fmt.Println(s.Contains("E"))
fmt.Println(s.Contains("z"))
// Output:
// true
// false
// false
}
func ExampleSortedStrArray_ContainsI() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"})
fmt.Println(s)
fmt.Println(s.ContainsI("E"))
fmt.Println(s.ContainsI("z"))
// Output:
// ["a","b","c","d","e","f","g","h"]
// true
// false
}
func ExampleSortedStrArray_Search() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"})
fmt.Println(s)
fmt.Println(s.Search("e"))
fmt.Println(s.Search("E"))
fmt.Println(s.Search("z"))
// Output:
// ["a","b","c","d","e","f","g","h"]
// 4
// -1
// -1
}
func ExampleSortedStrArray_Unique() {
s := garray.NewSortedStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "c", "c", "d", "d"})
fmt.Println(s)
fmt.Println(s.Unique())
// Output:
// ["a","b","c","c","c","d","d"]
// ["a","b","c","d"]
}
func ExampleSortedStrArray_LockFunc() {
s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "c", "a"})
s.LockFunc(func(array []string) {
array[len(array)-1] = "GF fans"
})
fmt.Println(s)
// Output:
// ["a","b","GF fans"]
}
func ExampleSortedStrArray_RLockFunc() {
s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "c", "a"})
s.RLockFunc(func(array []string) {
array[len(array)-1] = "GF fans"
fmt.Println(array[len(array)-1])
})
fmt.Println(s)
// Output:
// GF fans
// ["a","b","GF fans"]
}
func ExampleSortedStrArray_Merge() {
s1 := garray.NewSortedStrArray()
s2 := garray.NewSortedStrArray()
s1.SetArray(g.SliceStr{"b", "c", "a"})
s2.SetArray(g.SliceStr{"e", "d", "f"})
fmt.Println(s1)
fmt.Println(s2)
s1.Merge(s2)
fmt.Println(s1)
// Output:
// ["a","b","c"]
// ["d","e","f"]
// ["a","b","c","d","e","f"]
}
func ExampleSortedStrArray_Chunk() {
s := garray.NewSortedStrArrayFrom(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"})
r := s.Chunk(3)
fmt.Println(r)
// Output:
// [[a b c] [d e f] [g h]]
}
func ExampleSortedStrArray_Rands() {
s := garray.NewSortedStrArrayFrom(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"})
fmt.Println(s)
fmt.Println(s.Rands(3))
// May Output:
// ["a","b","c","d","e","f","g","h"]
// [h g c]
}
func ExampleSortedStrArray_Join() {
s := garray.NewSortedStrArrayFrom(g.SliceStr{"c", "b", "a", "d", "f", "e", "h", "g"})
fmt.Println(s.Join(","))
// Output:
// a,b,c,d,e,f,g,h
}
func ExampleSortedStrArray_CountValues() {
s := garray.NewSortedStrArrayFrom(g.SliceStr{"a", "b", "c", "c", "c", "d", "d"})
fmt.Println(s.CountValues())
// Output:
// map[a:1 b:1 c:3 d:2]
}
func ExampleSortedStrArray_Iterator() {
s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "c", "a"})
s.Iterator(func(k int, v string) bool {
fmt.Println(k, v)
return true
})
// Output:
// 0 a
// 1 b
// 2 c
}
func ExampleSortedStrArray_IteratorAsc() {
s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "c", "a"})
s.IteratorAsc(func(k int, v string) bool {
fmt.Println(k, v)
return true
})
// Output:
// 0 a
// 1 b
// 2 c
}
func ExampleSortedStrArray_IteratorDesc() {
s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "c", "a"})
s.IteratorDesc(func(k int, v string) bool {
fmt.Println(k, v)
return true
})
// Output:
// 2 c
// 1 b
// 0 a
}
func ExampleSortedStrArray_String() {
s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "c", "a"})
fmt.Println(s.String())
// Output:
// ["a","b","c"]
}
func ExampleSortedStrArray_MarshalJSON() {
type Student struct {
ID int
Name string
Levels garray.SortedStrArray
}
r := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "c", "a"})
s := Student{
ID: 1,
Name: "john",
Levels: *r,
}
b, _ := json.Marshal(s)
fmt.Println(string(b))
// Output:
// {"ID":1,"Name":"john","Levels":["a","b","c"]}
}
func ExampleSortedStrArray_UnmarshalJSON() {
b := []byte(`{"Id":1,"Name":"john","Lessons":["Math","English","Sport"]}`)
type Student struct {
Id int
Name string
Lessons *garray.StrArray
}
s := Student{}
json.Unmarshal(b, &s)
fmt.Println(s)
// Output:
// {1 john ["Math","English","Sport"]}
}
func ExampleSortedStrArray_UnmarshalValue() {
type Student struct {
Name string
Lessons *garray.StrArray
}
var s *Student
gconv.Struct(g.Map{
"name": "john",
"lessons": []byte(`["Math","English","Sport"]`),
}, &s)
fmt.Println(s)
var s1 *Student
gconv.Struct(g.Map{
"name": "john",
"lessons": g.SliceStr{"Math", "English", "Sport"},
}, &s1)
fmt.Println(s1)
// Output:
// &{john ["Math","English","Sport"]}
// &{john ["Math","English","Sport"]}
}
func ExampleSortedStrArray_FilterEmpty() {
s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "a", "", "c", "", "", "d"})
fmt.Println(s)
fmt.Println(s.FilterEmpty())
// Output:
// ["","","","a","b","c","d"]
// ["a","b","c","d"]
}
func ExampleSortedStrArray_IsEmpty() {
s := garray.NewSortedStrArrayFrom(g.SliceStr{"b", "a", "", "c", "", "", "d"})
fmt.Println(s.IsEmpty())
s1 := garray.NewSortedStrArray()
fmt.Println(s1.IsEmpty())
// Output:
// false
// true
}

View File

@ -10,6 +10,9 @@ import (
"fmt"
"github.com/gogf/gf/v2/container/garray"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gconv"
)
func ExampleStrArray_Walk() {
@ -26,3 +29,621 @@ func ExampleStrArray_Walk() {
// Output:
// [gf_user gf_user_detail]
}
func ExampleStrArray_NewStrArray() {
s := garray.NewStrArray()
s.Append("We")
s.Append("are")
s.Append("GF")
s.Append("fans")
fmt.Println(s.Slice())
// Output:
// [We are GF fans]
}
func ExampleStrArray_NewStrArraySize() {
s := garray.NewStrArraySize(3, 5)
s.Set(0, "We")
s.Set(1, "are")
s.Set(2, "GF")
s.Set(3, "fans")
fmt.Println(s.Slice(), s.Len(), cap(s.Slice()))
// Output:
// [We are GF] 3 5
}
func ExampleStrArray_NewStrArrayFrom() {
s := garray.NewStrArrayFrom(g.SliceStr{"We", "are", "GF", "fans", "!"})
fmt.Println(s.Slice(), s.Len(), cap(s.Slice()))
// Output:
// [We are GF fans !] 5 5
}
func ExampleStrArray_NewStrArrayFromCopy() {
s := garray.NewStrArrayFromCopy(g.SliceStr{"We", "are", "GF", "fans", "!"})
fmt.Println(s.Slice(), s.Len(), cap(s.Slice()))
// Output:
// [We are GF fans !] 5 5
}
func ExampleStrArray_At() {
s := garray.NewStrArrayFrom(g.SliceStr{"We", "are", "GF", "fans", "!"})
sAt := s.At(2)
fmt.Println(sAt)
// Output:
// GF
}
func ExampleStrArray_Get() {
s := garray.NewStrArrayFrom(g.SliceStr{"We", "are", "GF", "fans", "!"})
sGet, sBool := s.Get(3)
fmt.Println(sGet, sBool)
// Output:
// fans true
}
func ExampleStrArray_Set() {
s := garray.NewStrArraySize(3, 5)
s.Set(0, "We")
s.Set(1, "are")
s.Set(2, "GF")
s.Set(3, "fans")
fmt.Println(s.Slice())
// Output:
// [We are GF]
}
func ExampleStrArray_SetArray() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"We", "are", "GF", "fans", "!"})
fmt.Println(s.Slice())
// Output:
// [We are GF fans !]
}
func ExampleStrArray_Replace() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"We", "are", "GF", "fans", "!"})
fmt.Println(s.Slice())
s.Replace(g.SliceStr{"Happy", "coding"})
fmt.Println(s.Slice())
// Output:
// [We are GF fans !]
// [Happy coding GF fans !]
}
func ExampleStrArray_Sum() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"3", "5", "10"})
a := s.Sum()
fmt.Println(a)
// Output:
// 18
}
func ExampleStrArray_Sort() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"b", "d", "a", "c"})
a := s.Sort()
fmt.Println(a)
// Output:
// ["a","b","c","d"]
}
func ExampleStrArray_SortFunc() {
s := garray.NewStrArrayFrom(g.SliceStr{"b", "c", "a"})
fmt.Println(s)
s.SortFunc(func(v1, v2 string) bool {
return gstr.Compare(v1, v2) > 0
})
fmt.Println(s)
s.SortFunc(func(v1, v2 string) bool {
return gstr.Compare(v1, v2) < 0
})
fmt.Println(s)
// Output:
// ["b","c","a"]
// ["c","b","a"]
// ["a","b","c"]
}
func ExampleStrArray_InsertBefore() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d"})
s.InsertBefore(1, "here")
fmt.Println(s.Slice())
// Output:
// [a here b c d]
}
func ExampleStrArray_InsertAfter() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d"})
s.InsertAfter(1, "here")
fmt.Println(s.Slice())
// Output:
// [a b here c d]
}
func ExampleStrArray_Remove() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d"})
s.Remove(1)
fmt.Println(s.Slice())
// Output:
// [a c d]
}
func ExampleStrArray_RemoveValue() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d"})
s.RemoveValue("b")
fmt.Println(s.Slice())
// Output:
// [a c d]
}
func ExampleStrArray_PushLeft() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d"})
s.PushLeft("We", "are", "GF", "fans")
fmt.Println(s.Slice())
// Output:
// [We are GF fans a b c d]
}
func ExampleStrArray_PushRight() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d"})
s.PushRight("We", "are", "GF", "fans")
fmt.Println(s.Slice())
// Output:
// [a b c d We are GF fans]
}
func ExampleStrArray_PopLeft() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d"})
s.PopLeft()
fmt.Println(s.Slice())
// Output:
// [b c d]
}
func ExampleStrArray_PopRight() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d"})
s.PopRight()
fmt.Println(s.Slice())
// Output:
// [a b c]
}
func ExampleStrArray_PopRand() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
r, _ := s.PopRand()
fmt.Println(r)
// May Output:
// e
}
func ExampleStrArray_PopRands() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
r := s.PopRands(2)
fmt.Println(r)
// May Output:
// [e c]
}
func ExampleStrArray_PopLefts() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
r := s.PopLefts(2)
fmt.Println(r)
fmt.Println(s)
// Output:
// [a b]
// ["c","d","e","f","g","h"]
}
func ExampleStrArray_PopRights() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
r := s.PopRights(2)
fmt.Println(r)
fmt.Println(s)
// Output:
// [g h]
// ["a","b","c","d","e","f"]
}
func ExampleStrArray_Range() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
r := s.Range(2, 5)
fmt.Println(r)
// Output:
// [c d e]
}
func ExampleStrArray_SubSlice() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
r := s.SubSlice(3, 4)
fmt.Println(r)
// Output:
// [d e f g]
}
func ExampleStrArray_Append() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"We", "are", "GF", "fans"})
s.Append("a", "b", "c")
fmt.Println(s)
// Output:
// ["We","are","GF","fans","a","b","c"]
}
func ExampleStrArray_Len() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
fmt.Println(s.Len())
// Output:
// 8
}
func ExampleStrArray_Slice() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
fmt.Println(s.Slice())
// Output:
// [a b c d e f g h]
}
func ExampleStrArray_Interfaces() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
r := s.Interfaces()
fmt.Println(r)
// Output:
// [a b c d e f g h]
}
func ExampleStrArray_Clone() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
r := s.Clone()
fmt.Println(r)
fmt.Println(s)
// Output:
// ["a","b","c","d","e","f","g","h"]
// ["a","b","c","d","e","f","g","h"]
}
func ExampleStrArray_Clear() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
fmt.Println(s)
fmt.Println(s.Clear())
fmt.Println(s)
// Output:
// ["a","b","c","d","e","f","g","h"]
// []
// []
}
func ExampleStrArray_Contains() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
fmt.Println(s.Contains("e"))
fmt.Println(s.Contains("z"))
// Output:
// true
// false
}
func ExampleStrArray_ContainsI() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
fmt.Println(s.ContainsI("E"))
fmt.Println(s.ContainsI("z"))
// Output:
// true
// false
}
func ExampleStrArray_Search() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
fmt.Println(s.Search("e"))
fmt.Println(s.Search("z"))
// Output:
// 4
// -1
}
func ExampleStrArray_Unique() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "c", "c", "d", "d"})
fmt.Println(s.Unique())
// Output:
// ["a","b","c","d"]
}
func ExampleStrArray_LockFunc() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
s.LockFunc(func(array []string) {
array[len(array)-1] = "GF fans"
})
fmt.Println(s)
// Output:
// ["a","b","GF fans"]
}
func ExampleStrArray_RLockFunc() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e"})
s.RLockFunc(func(array []string) {
for i := 0; i < len(array); i++ {
fmt.Println(array[i])
}
})
// Output:
// a
// b
// c
// d
// e
}
func ExampleStrArray_Merge() {
s1 := garray.NewStrArray()
s2 := garray.NewStrArray()
s1.SetArray(g.SliceStr{"a", "b", "c"})
s2.SetArray(g.SliceStr{"d", "e", "f"})
s1.Merge(s2)
fmt.Println(s1)
// Output:
// ["a","b","c","d","e","f"]
}
func ExampleStrArray_Fill() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
s.Fill(2, 3, "here")
fmt.Println(s)
// Output:
// ["a","b","here","here","here","f","g","h"]
}
func ExampleStrArray_Chunk() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
r := s.Chunk(3)
fmt.Println(r)
// Output:
// [[a b c] [d e f] [g h]]
}
func ExampleStrArray_Pad() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
s.Pad(7, "here")
fmt.Println(s)
s.Pad(-10, "there")
fmt.Println(s)
// Output:
// ["a","b","c","here","here","here","here"]
// ["there","there","there","a","b","c","here","here","here","here"]
}
func ExampleStrArray_Rand() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
fmt.Println(s.Rand())
// May Output:
// c true
}
func ExampleStrArray_Rands() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
fmt.Println(s.Rands(3))
// May Output:
// [e h e]
}
func ExampleStrArray_Shuffle() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
fmt.Println(s.Shuffle())
// May Output:
// ["a","c","e","d","b","g","f","h"]
}
func ExampleStrArray_Reverse() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
fmt.Println(s.Reverse())
// Output:
// ["h","g","f","e","d","c","b","a"]
}
func ExampleStrArray_Join() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
fmt.Println(s.Join(","))
// Output:
// a,b,c
}
func ExampleStrArray_CountValues() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "c", "c", "d", "d"})
fmt.Println(s.CountValues())
// Output:
// map[a:1 b:1 c:3 d:2]
}
func ExampleStrArray_Iterator() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
s.Iterator(func(k int, v string) bool {
fmt.Println(k, v)
return true
})
// Output:
// 0 a
// 1 b
// 2 c
}
func ExampleStrArray_IteratorAsc() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
s.IteratorAsc(func(k int, v string) bool {
fmt.Println(k, v)
return true
})
// Output:
// 0 a
// 1 b
// 2 c
}
func ExampleStrArray_IteratorDesc() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
s.IteratorDesc(func(k int, v string) bool {
fmt.Println(k, v)
return true
})
// Output:
// 2 c
// 1 b
// 0 a
}
func ExampleStrArray_String() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
fmt.Println(s.String())
// Output:
// ["a","b","c"]
}
func ExampleStrArray_MarshalJSON() {
type Student struct {
Id int
Name string
Lessons []string
}
s := Student{
Id: 1,
Name: "john",
Lessons: []string{"Math", "English", "Music"},
}
b, _ := json.Marshal(s)
fmt.Println(string(b))
// Output:
// {"Id":1,"Name":"john","Lessons":["Math","English","Music"]}
}
func ExampleStrArray_UnmarshalJSON() {
b := []byte(`{"Id":1,"Name":"john","Lessons":["Math","English","Sport"]}`)
type Student struct {
Id int
Name string
Lessons *garray.StrArray
}
s := Student{}
json.Unmarshal(b, &s)
fmt.Println(s)
// Output:
// {1 john ["Math","English","Sport"]}
}
func ExampleStrArray_UnmarshalValue() {
type Student struct {
Name string
Lessons *garray.StrArray
}
var s *Student
gconv.Struct(g.Map{
"name": "john",
"lessons": []byte(`["Math","English","Sport"]`),
}, &s)
fmt.Println(s)
var s1 *Student
gconv.Struct(g.Map{
"name": "john",
"lessons": g.SliceStr{"Math", "English", "Sport"},
}, &s1)
fmt.Println(s1)
// Output:
// &{john ["Math","English","Sport"]}
// &{john ["Math","English","Sport"]}
}
func ExampleStrArray_FilterEmpty() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "", "c", "", "", "d"})
fmt.Println(s.FilterEmpty())
// Output:
// ["a","b","c","d"]
}
func ExampleStrArray_IsEmpty() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "", "c", "", "", "d"})
fmt.Println(s.IsEmpty())
s1 := garray.NewStrArray()
fmt.Println(s1.IsEmpty())
// Output:
// false
// true
}

View File

@ -7,17 +7,401 @@
package gset_test
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/frame/g"
)
// New create and returns a new set, which contains un-repeated items.
// The parameter `safe` is used to specify whether using set in concurrent-safety,
// which is false in default.
func ExampleNewIntSet() {
intSet := gset.NewIntSet()
intSet.Add([]int{1, 2, 3}...)
fmt.Println(intSet.Slice())
// May Output:
// [2 1 3]
}
// NewIntSetFrom returns a new set from `items`.
func ExampleNewFrom() {
intSet := gset.NewIntSetFrom([]int{1, 2, 3})
fmt.Println(intSet.Slice())
// May Output:
// [2 1 3]
}
// Add adds one or multiple items to the set.
func ExampleIntSet_Add() {
intSet := gset.NewIntSetFrom([]int{1, 2, 3})
intSet.Add(1)
fmt.Println(intSet.Slice())
fmt.Println(intSet.AddIfNotExist(1))
// Mya Output:
// [1 2 3]
// false
}
// AddIfNotExist checks whether item exists in the set,
// it adds the item to set and returns true if it does not exists in the set,
// or else it does nothing and returns false.
func ExampleIntSet_AddIfNotExist() {
intSet := gset.NewIntSetFrom([]int{1, 2, 3})
intSet.Add(1)
fmt.Println(intSet.Slice())
fmt.Println(intSet.AddIfNotExist(1))
// Mya Output:
// [1 2 3]
// false
}
// AddIfNotExistFunc checks whether item exists in the set,
// it adds the item to set and returns true if it does not exists in the set and function `f` returns true,
// or else it does nothing and returns false.
// Note that, the function `f` is executed without writing lock.
func ExampleIntSet_AddIfNotExistFunc() {
intSet := gset.NewIntSetFrom([]int{1, 2, 3})
intSet.Add(1)
fmt.Println(intSet.Slice())
fmt.Println(intSet.AddIfNotExistFunc(5, func() bool {
return true
}))
// May Output:
// [1 2 3]
// true
}
// AddIfNotExistFunc checks whether item exists in the set,
// it adds the item to set and returns true if it does not exists in the set and function `f` returns true,
// or else it does nothing and returns false.
// Note that, the function `f` is executed without writing lock.
func ExampleIntSet_AddIfNotExistFuncLock() {
intSet := gset.NewIntSetFrom([]int{1, 2, 3})
intSet.Add(1)
fmt.Println(intSet.Slice())
fmt.Println(intSet.AddIfNotExistFuncLock(4, func() bool {
return true
}))
// May Output:
// [1 2 3]
// true
}
// Clear deletes all items of the set.
func ExampleIntSet_Clear() {
intSet := gset.NewIntSetFrom([]int{1, 2, 3})
fmt.Println(intSet.Size())
intSet.Clear()
fmt.Println(intSet.Size())
// Output:
// 3
// 0
}
// Complement returns a new set which is the complement from `set` to `full`.
// Which means, all the items in `newSet` are in `full` and not in `set`.
// It returns the difference between `full` and `set` if the given set `full` is not the full set of `set`.
func ExampleIntSet_Complement() {
intSet := gset.NewIntSetFrom([]int{1, 2, 3, 4, 5})
s := gset.NewIntSetFrom([]int{1, 2, 3})
fmt.Println(s.Complement(intSet).Slice())
// May Output:
// [4 5]
}
// Contains checks whether the set contains `item`.
func ExampleIntSet_Contains() {
var set gset.IntSet
set.Add(1)
fmt.Println(set.Contains(1))
fmt.Println(set.Contains(2))
var set1 gset.IntSet
set1.Add(1, 4, 5, 6, 7)
fmt.Println(set1.Contains(1))
var set2 gset.IntSet
set2.Add(1, 4, 5, 6, 7)
fmt.Println(set2.Contains(8))
// Output:
// true
// false
}
// Diff returns a new set which is the difference set from `set` to `other`.
// Which means, all the items in `newSet` are in `set` but not in `other`.
func ExampleIntSet_Diff() {
s1 := gset.NewIntSetFrom([]int{1, 2, 3})
s2 := gset.NewIntSetFrom([]int{1, 2, 3, 4})
fmt.Println(s2.Diff(s1).Slice())
// Output:
// [4]
}
// Equal checks whether the two sets equal.
func ExampleIntSet_Equal() {
s1 := gset.NewIntSetFrom([]int{1, 2, 3})
s2 := gset.NewIntSetFrom([]int{1, 2, 3, 4})
fmt.Println(s2.Equal(s1))
s3 := gset.NewIntSetFrom([]int{1, 2, 3})
s4 := gset.NewIntSetFrom([]int{1, 2, 3})
fmt.Println(s3.Equal(s4))
// Output:
// false
// true
}
// Intersect returns a new set which is the intersection from `set` to `other`.
// Which means, all the items in `newSet` are in `set` and also in `other`.
func ExampleIntSet_Intersect() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3}...)
var s2 gset.IntSet
s2.Add([]int{1, 2, 3, 4}...)
fmt.Println(s2.Intersect(s1).Slice())
// May Output:
// [1 2 3]
}
// IsSubsetOf checks whether the current set is a sub-set of `other`
func ExampleIntSet_IsSubsetOf() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
var s2 gset.IntSet
s2.Add([]int{1, 2, 4}...)
fmt.Println(s2.IsSubsetOf(s1))
// Output:
// true
}
// Iterator iterates the set readonly with given callback function `f`,
// if `f` returns true then continue iterating; or false to stop.
func ExampleIntSet_Iterator() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
s1.Iterator(func(v int) bool {
fmt.Println("Iterator", v)
return true
})
// May Output:
// Iterator 2
// Iterator 3
// Iterator 1
// Iterator 4
}
// Join joins items with a string `glue`.
func ExampleIntSet_Join() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
fmt.Println(s1.Join(","))
// May Output:
// 3,4,1,2
}
// LockFunc locks writing with callback function `f`.
func ExampleIntSet_LockFunc() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2}...)
s1.LockFunc(func(m map[int]struct{}) {
m[3] = struct{}{}
})
fmt.Println(s1.Slice())
// May Output
// [2 3 1]
}
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func ExampleIntSet_MarshalJSON() {
type Student struct {
Id int
Name string
Scores *gset.IntSet
}
s := Student{
Id: 1,
Name: "john",
Scores: gset.NewIntSetFrom([]int{100, 99, 98}),
}
b, _ := json.Marshal(s)
fmt.Println(string(b))
// May Output:
// {"Id":1,"Name":"john","Scores":[100,99,98]}
}
// Merge adds items from `others` sets into `set`.
func ExampleIntSet_Merge() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
s2 := gset.NewIntSet()
fmt.Println(s1.Merge(s2).Slice())
// May Output:
// [1 2 3 4]
}
// Pops randomly pops an item from set.
func ExampleIntSet_Pop() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
fmt.Println(s1.Pop())
// May Output:
// 1
}
// Pops randomly pops `size` items from set.
// It returns all items if size == -1.
func ExampleIntSet_Pops() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
for _, v := range s1.Pops(2) {
fmt.Println(v)
}
// May Output:
// 1
// 2
}
// RLockFunc locks reading with callback function `f`.
func ExampleIntSet_RLockFunc() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
s1.RLockFunc(func(m map[int]struct{}) {
fmt.Println(m)
})
// Output:
// map[1:{} 2:{} 3:{} 4:{}]
}
// Remove deletes `item` from set.
func ExampleIntSet_Remove() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
s1.Remove(1)
fmt.Println(s1.Slice())
// May Output:
// [3 4 2]
}
// Size returns the size of the set.
func ExampleIntSet_Size() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
fmt.Println(s1.Size())
// Output:
// 4
}
// Slice returns the a of items of the set as slice.
func ExampleIntSet_Slice() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
fmt.Println(s1.Slice())
// May Output:
// [1, 2, 3, 4]
}
// String returns items as a string, which implements like json.Marshal does.
func ExampleIntSet_String() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
fmt.Println(s1.String())
// May Output:
// [1,2,3,4]
}
// Sum sums items. Note: The items should be converted to int type,
// or you'd get a result that you unexpected.
func ExampleIntSet_Sum() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
fmt.Println(s1.Sum())
// Output:
// 10
}
// Union returns a new set which is the union of `set` and `other`.
// Which means, all the items in `newSet` are in `set` or in `other`.
func ExampleIntSet_Union() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
s2 := gset.NewIntSet()
s2.Add([]int{1, 2, 4}...)
fmt.Println(s1.Union(s2).Slice())
// May Output:
// [3 4 1 2]
}
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func ExampleIntSet_UnmarshalJSON() {
b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
type Student struct {
Id int
Name string
Scores *gset.IntSet
}
s := Student{}
json.Unmarshal(b, &s)
fmt.Println(s)
// May Output:
// {1 john [100,99,98]}
}
// UnmarshalValue is an interface implement which sets any type of value for set.
func ExampleIntSet_UnmarshalValue() {
b := []byte(`{"Id":1,"Name":"john","Scores":100,99,98}`)
type Student struct {
Id int
Name string
Scores *gset.IntSet
}
s := Student{}
json.Unmarshal(b, &s)
fmt.Println(s)
// May Output:
// {1 john [100,99,98]}
}
// Walk applies a user supplied function `f` to every item of set.
func ExampleIntSet_Walk() {
var (
set gset.IntSet
names = g.SliceInt{1, 0}
delta = 10
)
set.Add(names...)
// Add prefix for given table names.
set.Walk(func(item int) int {
return delta + item
})
fmt.Println(set.Slice())
// May Output:
// [12 60]
}

View File

@ -7,24 +7,400 @@
package gset_test
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/frame/g"
)
// NewStrSet create and returns a new set, which contains un-repeated items.
// The parameter `safe` is used to specify whether using set in concurrent-safety,
// which is false in default.
func ExampleNewStrSet() {
strSet := gset.NewStrSet(true)
strSet.Add([]string{"str1", "str2", "str3"}...)
fmt.Println(strSet.Slice())
// May Output:
// [str3 str1 str2]
}
// NewStrSetFrom returns a new set from `items`.
func ExampleNewStrSetFrom() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
fmt.Println(strSet.Slice())
// May Output:
// [str1 str2 str3]
}
// Add adds one or multiple items to the set.
func ExampleStrSet_Add() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
strSet.Add("str")
fmt.Println(strSet.Slice())
fmt.Println(strSet.AddIfNotExist("str"))
// Mya Output:
// [str str1 str2 str3]
// false
}
// AddIfNotExist checks whether item exists in the set,
// it adds the item to set and returns true if it does not exists in the set,
// or else it does nothing and returns false.
func ExampleStrSet_AddIfNotExist() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
strSet.Add("str")
fmt.Println(strSet.Slice())
fmt.Println(strSet.AddIfNotExist("str"))
// Mya Output:
// [str str1 str2 str3]
// false
}
// AddIfNotExistFunc checks whether item exists in the set,
// it adds the item to set and returns true if it does not exists in the set and function `f` returns true,
// or else it does nothing and returns false.
// Note that, the function `f` is executed without writing lock.
func ExampleStrSet_AddIfNotExistFunc() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
strSet.Add("str")
fmt.Println(strSet.Slice())
fmt.Println(strSet.AddIfNotExistFunc("str5", func() bool {
return true
}))
// May Output:
// [str1 str2 str3 str]
// true
}
// AddIfNotExistFunc checks whether item exists in the set,
// it adds the item to set and returns true if it does not exists in the set and function `f` returns true,
// or else it does nothing and returns false.
// Note that, the function `f` is executed without writing lock.
func ExampleStrSet_AddIfNotExistFuncLock() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
strSet.Add("str")
fmt.Println(strSet.Slice())
fmt.Println(strSet.AddIfNotExistFuncLock("str4", func() bool {
return true
}))
// May Output:
// [str1 str2 str3 str]
// true
}
// Clear deletes all items of the set.
func ExampleStrSet_Clear() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
fmt.Println(strSet.Size())
strSet.Clear()
fmt.Println(strSet.Size())
// Output:
// 3
// 0
}
// Complement returns a new set which is the complement from `set` to `full`.
// Which means, all the items in `newSet` are in `full` and not in `set`.
// It returns the difference between `full` and `set` if the given set `full` is not the full set of `set`.
func ExampleStrSet_Complement() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3", "str4", "str5"}, true)
s := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
fmt.Println(s.Complement(strSet).Slice())
// May Output:
// [str4 str5]
}
// Contains checks whether the set contains `item`.
func ExampleStrSet_Contains() {
var set gset.StrSet
set.Add("a")
fmt.Println(set.Contains("a"))
fmt.Println(set.Contains("A"))
fmt.Println(set.ContainsI("A"))
// Output:
// true
// false
}
// ContainsI checks whether a value exists in the set with case-insensitively.
// Note that it internally iterates the whole set to do the comparison with case-insensitively.
func ExampleStrSet_ContainsI() {
var set gset.StrSet
set.Add("a")
fmt.Println(set.ContainsI("a"))
fmt.Println(set.ContainsI("A"))
// Output:
// true
// true
}
// Diff returns a new set which is the difference set from `set` to `other`.
// Which means, all the items in `newSet` are in `set` but not in `other`.
func ExampleStrSet_Diff() {
s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
s2 := gset.NewStrSetFrom([]string{"a", "b", "c", "d"}, true)
fmt.Println(s2.Diff(s1).Slice())
// Output:
// [d]
}
// Equal checks whether the two sets equal.
func ExampleStrSet_Equal() {
s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
s2 := gset.NewStrSetFrom([]string{"a", "b", "c", "d"}, true)
fmt.Println(s2.Equal(s1))
s3 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
s4 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
fmt.Println(s3.Equal(s4))
// Output:
// false
// true
}
// Intersect returns a new set which is the intersection from `set` to `other`.
// Which means, all the items in `newSet` are in `set` and also in `other`.
func ExampleStrSet_Intersect() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c"}...)
var s2 gset.StrSet
s2.Add([]string{"a", "b", "c", "d"}...)
fmt.Println(s2.Intersect(s1).Slice())
// May Output:
// [c a b]
}
// IsSubsetOf checks whether the current set is a sub-set of `other`
func ExampleStrSet_IsSubsetOf() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
var s2 gset.StrSet
s2.Add([]string{"a", "b", "d"}...)
fmt.Println(s2.IsSubsetOf(s1))
// Output:
// true
}
// Iterator iterates the set readonly with given callback function `f`,
// if `f` returns true then continue iterating; or false to stop.
func ExampleStrSet_Iterator() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
s1.Iterator(func(v string) bool {
fmt.Println("Iterator", v)
return true
})
// May Output:
// Iterator a
// Iterator b
// Iterator c
// Iterator d
}
// Join joins items with a string `glue`.
func ExampleStrSet_Join() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
fmt.Println(s1.Join(","))
// May Output:
// b,c,d,a
}
// LockFunc locks writing with callback function `f`.
func ExampleStrSet_LockFunc() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"1", "2"}...)
s1.LockFunc(func(m map[string]struct{}) {
m["3"] = struct{}{}
})
fmt.Println(s1.Slice())
// May Output
// [2 3 1]
}
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func ExampleStrSet_MarshalJSON() {
type Student struct {
Id int
Name string
Scores *gset.StrSet
}
s := Student{
Id: 1,
Name: "john",
Scores: gset.NewStrSetFrom([]string{"100", "99", "98"}, true),
}
b, _ := json.Marshal(s)
fmt.Println(string(b))
// May Output:
// {"Id":1,"Name":"john","Scores":["100","99","98"]}
}
// Merge adds items from `others` sets into `set`.
func ExampleStrSet_Merge() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
s2 := gset.NewStrSet(true)
fmt.Println(s1.Merge(s2).Slice())
// May Output:
// [d a b c]
}
// Pops randomly pops an item from set.
func ExampleStrSet_Pop() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
fmt.Println(s1.Pop())
// May Output:
// a
}
// Pops randomly pops `size` items from set.
// It returns all items if size == -1.
func ExampleStrSet_Pops() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
for _, v := range s1.Pops(2) {
fmt.Println(v)
}
// May Output:
// a
// b
}
// RLockFunc locks reading with callback function `f`.
func ExampleStrSet_RLockFunc() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
s1.RLockFunc(func(m map[string]struct{}) {
fmt.Println(m)
})
// Output:
// map[a:{} b:{} c:{} d:{}]
}
// Remove deletes `item` from set.
func ExampleStrSet_Remove() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
s1.Remove("a")
fmt.Println(s1.Slice())
// May Output:
// [b c d]
}
// Size returns the size of the set.
func ExampleStrSet_Size() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
fmt.Println(s1.Size())
// Output:
// 4
}
// Slice returns the a of items of the set as slice.
func ExampleStrSet_Slice() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
fmt.Println(s1.Slice())
// May Output:
// [a,b,c,d]
}
// String returns items as a string, which implements like json.Marshal does.
func ExampleStrSet_String() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
fmt.Println(s1.String())
// May Output:
// "a","b","c","d"
}
// Sum sums items. Note: The items should be converted to int type,
// or you'd get a result that you unexpected.
func ExampleStrSet_Sum() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"1", "2", "3", "4"}...)
fmt.Println(s1.Sum())
// Output:
// 10
}
// Union returns a new set which is the union of `set` and `other`.
// Which means, all the items in `newSet` are in `set` or in `other`.
func ExampleStrSet_Union() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
s2 := gset.NewStrSet(true)
s2.Add([]string{"a", "b", "d"}...)
fmt.Println(s1.Union(s2).Slice())
// May Output:
// [a b c d]
}
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func ExampleStrSet_UnmarshalJSON() {
b := []byte(`{"Id":1,"Name":"john","Scores":["100","99","98"]}`)
type Student struct {
Id int
Name string
Scores *gset.StrSet
}
s := Student{}
json.Unmarshal(b, &s)
fmt.Println(s)
// May Output:
// {1 john "99","98","100"}
}
// UnmarshalValue is an interface implement which sets any type of value for set.
func ExampleStrSet_UnmarshalValue() {
b := []byte(`{"Id":1,"Name":"john","Scores":["100","99","98"]}`)
type Student struct {
Id int
Name string
Scores *gset.StrSet
}
s := Student{}
json.Unmarshal(b, &s)
fmt.Println(s)
// May Output:
// {1 john "99","98","100"}
}
// Walk applies a user supplied function `f` to every item of set.
func ExampleStrSet_Walk() {
var (
set gset.StrSet