Merge pull request #1459 from visualsun/master

Create garray_z_example_sorted_str_test.go
This commit is contained in:
John Guo 2021-11-05 23:27:56 +08:00 committed by GitHub
commit 66731c9c66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 1282 additions and 0 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
}