以下常用方法列表,文档更新可能滞后于代码新特性,更多的方法及示例请参考代码文档:https://pkg.GO.dev/github.com/gogf/gf/v2/container/garray

专注于为中小企业提供网站制作、成都网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业鄂州免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了千余家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
Append的方法是PushRight的别名Append(value ...string) *StrArrayfunc 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"]
}At(index int) (value string)index为2的数据。func ExampleStrArray_At() {
s := garray.NewStrArrayFrom(g.SliceStr{"We", "are", "GF", "fans", "!"})
sAt := s.At(2)
fmt.Println(sAt)
// Output:
// GF
}Size,分割成多个数组,返回值为[][]string。最后一个数组包含数据的数量可能小于SizeChunk(size int) [][]stringfunc 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]]
}Clear() *StrArrayfunc 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"]
// []
// []
}Clone() (newArray *StrArray)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"]
}String值。字符串严格区分大小写。返回值为boolContains(value string) boole和zfunc 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
}String值。字符串不区分大小写。返回值为boolContainsI(value string) boolE和zfunc 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
}map[string]intCountValues() map[string]intfunc 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]
}startIndex,用指定的value进行填充。返回值为errorFill(startIndex int, num int, value string) errorindex为2的地方,用字符串here填充3个数据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"]
}FilterEmpty() *StrArrayfunc ExampleStrArray_FilterEmpty() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "", "c", "", "", "d"})
fmt.Println(s.FilterEmpty())
// Output:
// ["a","b","c","d"]
}index的值,返回值有2个参数,返回值value,和是否找到指定位置的数据found,为true则找到,为false则未找到Get(index int) (value string, found bool)func ExampleStrArray_Get() {
s := garray.NewStrArrayFrom(g.SliceStr{"We", "are", "GF", "fans", "!"})
sGet, sBool := s.Get(3)
fmt.Println(sGet, sBool)
// Output:
// fans true
}index的位置之后插入值value,返回值为errorInsertAfter(index int, value string) errorindex为1的值之后,插入字符串herefunc 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]
}index的位置之前插入值value,返回值为errorInsertBefore(index int, value string) errorindex为1的值之前,插入字符串herefunc 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]
}[]interface{}进行返回Interfaces() []interface{}[]interface{}的内容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]
}true,如果不是空数组,则返回falseIsEmpty() boolfunc 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
}Iterator(f func(k int, v string) bool)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
}f,按升序对数组进行遍历,如果f返回true,则继续进行遍历,否则停止遍历IteratorAsc(f func(k int, v string) bool)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
}f,按降序对数组进行遍历,如果f返回true,则继续进行遍历,否则停止遍历IteratorAsc(f func(k int, v string) bool)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
}gule,连接起来Join(glue string) string',',将数组中的字符串连接起来func ExampleStrArray_Join() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
fmt.Println(s.Join(","))
// Output:
// a,b,c
}Join(glue string) stringfunc ExampleStrArray_Len() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
fmt.Println(s.Len())
// Output:
// 8
}f对数组进行写锁定LockFunc(f func(array []string)) *StrArrayfunc 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"]
}json.Marshal的JSON格式的序列化接口MarshalJSON() ([]byte, error)JSON格式的数据,并对该数据进行序列化的操作后,打印出相应结果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"]}
}array可以是任意garray或slice类型。Merge和Append的主要区别是Append仅仅支持slice类型,Merge则支持更多的参数类型Merge(array interface{}) *StrArrayfunc 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"]
}safe为非必需参数,布尔型,是并发安全的开关,缺省值为FalseNewStrArray(safe ...bool) *StrArraySafe参数,默认为非并发安全设置func ExampleNewStrArray() {
s := garray.NewStrArray()
s.Append("We")
s.Append("are")
s.Append("GF")
s.Append("Fans")
fmt.Println(s.Slice())
// Output:
// [We are GF Fans]
}safe为非必需参数,布尔型,是并发安全的开关,缺省值为FalseNewStrArrayFrom(array []string, safe ...bool) *StrArraySafe参数,默认为非并发安全设置func ExampleNewStrArrayFrom() {
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
}safe为非必需参数,布尔型,是并发安全的开关,缺省值为FalseNewStrArrayFrom(array []string, safe ...bool) *StrArraySafe参数,默认为非并发安全设置func ExampleNewStrArrayFromCopy() {
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
}size和cap,创建一个新数组。safe为非必需参数,布尔型,是并发安全的开关,缺省值为FalseNewStrArraySize(size int, cap int, safe ...bool) *StrArraySize为3,Cap为5,并添加数据。打印出相应的内容。此时没有指定 Safe参数,默认为非并发安全设置func ExampleNewStrArraySize() {
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
}size的值value到数组中。如果大小size是正数,则从数组的右边开始填充。如果size是负数,则从数组的左边开始填充。如果size的大小正好等于数组的长度,那么将不会填充任何数据。Pad(size int, value string) *StrArrayhere填充到size为7,然后用指定的字符串there将数组用字符串填充到size为10func 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"]
}value为出栈的字符串数据。更新后的数组数据为剩余数据。当数组为空时,found为false。PopLeft() (value string, found bool)func ExampleStrArray_PopLeft() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d"})
s.PopLeft()
fmt.Println(s.Slice())
// Output:
// [b c d]
}size。如果size比数组的size大,那么方法将返回数组中所有的数据。如果size<=0或者为空,那么将返回nilPopLefts(size int) []stringfunc 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"]
}found将返回falsePopRand() (value string, found bool)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
}size个数据,返回值为出栈的字符串数据。如果size<=0或者为空,那么将返回nilPopRands(size int) []stringfunc 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]
}value为出栈的字符串数据。更新后的数组数据为剩余数据。当数组为空时,found为false。PopRight() (value string, found bool)func ExampleStrArray_PopRight() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d"})
s.PopRight()
fmt.Println(s.Slice())
// Output:
// [a b c]
}size。如果size比数组的size大,那么方法将返回数组中所有的数据。如果size<=0或者为空,那么将返回nilPopRights(size int) []stringfunc 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"]
}PushLeft(value ...string) *StrArrayfunc 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]
}PushRight(value ...string) *StrArrayfunc 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]
}Rand() (value string, found bool)func ExampleStrArray_Rand() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
fmt.Println(s.Rand())
// May Output:
// c true
}size个字符串(非删除式)Rands(size int) []stringfunc 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]
}slice拷贝。Range(start int, end ...int) []stringindex为2至5位置的数据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]
}index处的数据。如果index超过数组的边界,则found返回falseRemove(index int) (value string, found bool)index为1的数据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]
}value。如果value在数组中被找到,则found返回true,否则found返回falseRemoveValue(value string) boolfunc ExampleStrArray_RemoveValue() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d"})
s.RemoveValue("b")
fmt.Println(s.Slice())
// Output:
// [a c d]
}Replace(array []string) *StrArrayfunc 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 !]
}Replace(array []string) *StrArrayfunc 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"]
}f进行数组的读锁定RLockFunc(f func(array []string)) *StrArrayf中,对数组进行遍历,并打印出数组元素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
}index,如果没有查询到,则返回-1Search(value string) intfunc 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
}index设置值value,如果index<0或者index超出了数组的边界,则返回错误errorSet(index int, value string) errorindex为2的位置,因为数组长度限制,最后一个值未设置成功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]
}slice数组内容给数组赋值SetArray(array []string) *StrArrayfunc ExampleStrArray_SetArray() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"We", "are", "GF", "fans", "!"})
fmt.Println(s.Slice())
// Output:
// [We are GF fans !]
}Shuffle() *StrArrayfunc 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"]
}slice切片数据,注意,如果是在并发安全模式下,返回的是一份拷贝数据,否则返回的是指向数据的指针Shuffle() *StrArrayfunc 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]
}reverse控制排序的方向,默认为true升序,false为降序Sort(reverse ...bool) *StrArrayfunc ExampleStrArray_Sort() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"b", "d", "a", "c"})
a := s.Sort()
fmt.Println(a)
// Output:
// ["a","b","c","d"]
}less对数组内容进行排序。SortFunc(less func(v1, v2 string) bool) *StrArrayfunc 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"]
}string,String() stringstring,并打印出相应的结果func ExampleStrArray_String() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
fmt.Println(s.String())
// Output:
// ["a","b","c"]
}offset和长度length参数获得数组的切片,注意,如果是在并发安全模式下,返回拷贝数据,否则返回指向数据的指针。如果偏离值offset是非负数,则从数组的开始位置进行切片,否则从数组的尾部开始切片。SubSlice(offset int, length ...int)string,并打印出相应的结果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]
}Sum() (sum int)func ExampleStrArray_Sum() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"3", "5", "10"})
a := s.Sum()
fmt.Println(a)
// Output:
// 18
}Unique() *StrArrayfunc 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"]
}json.Unmarshal的UnmarshalJSON接口Unique() *StrArraybyte切片,将其赋值给结构体后,进行反序列化操作,打印出相应的内容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"]}
}UnmarshalValue(value interface{}) errorfunc 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"]}
}f,对数组内容进行遍历修改Walk(f func(value string) string) *StrArrayfunc ExampleStrArray_Walk() {
var array garray.StrArray
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]
}