新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)GoFrame教程:GoFrame garray-方法介紹
以下常用方法列表,文檔更新可能滯后于代碼新特性,更多的方法及示例請參考代碼文檔:https://pkg.GO.dev/github.com/gogf/gf/v2/container/garray

專注于為中小企業(yè)提供網(wǎng)站制作、成都網(wǎng)站建設服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業(yè)鄂州免費做網(wǎng)站提供優(yōu)質的服務。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了千余家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設實現(xiàn)規(guī)模擴充和轉變。
Append
- 說明:向數(shù)組的尾部追加數(shù)據(jù),可以添加任意數(shù)量字符串。?
Append?的方法是?PushRight?的別名 - 格式:
Append(value ...string) *StrArray- 示例:建立一個空數(shù)組,設置完數(shù)據(jù)后,并在數(shù)組尾部添加新的數(shù)據(jù)。
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"]
}At
- 說明:返回數(shù)組指定索引的數(shù)據(jù)
- 格式:
At(index int) (value string)- 示例:建立一個數(shù)組,找到?
index?為2的數(shù)據(jù)。
func ExampleStrArray_At() {
s := garray.NewStrArrayFrom(g.SliceStr{"We", "are", "GF", "fans", "!"})
sAt := s.At(2)
fmt.Println(sAt)
// Output:
// GF
}Chunk
- 說明:把指定數(shù)組按指定的大小?
Size?,分割成多個數(shù)組,返回值為?[][]string?。最后一個數(shù)組包含數(shù)據(jù)的數(shù)量可能小于?Size? - 格式:
Chunk(size int) [][]string- 示例:建立一個數(shù)組,并將該數(shù)組分割成3個數(shù)組。
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]]
}Clear
- 說明:刪除當前數(shù)組中所有的數(shù)據(jù)
- 格式:
Clear() *StrArray- 示例:建立一個空數(shù)組,賦值后,并刪除該數(shù)組的數(shù)據(jù)。
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"]
// []
// []
}Clone
- 說明:克隆當前的數(shù)組。返回一個與當前數(shù)組相同的數(shù)組拷貝
- 格式:
Clone() (newArray *StrArray)- 示例:建立一個空數(shù)組,賦值后,克隆出一個新數(shù)組。
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"]
}Contains
- 說明:判斷一個數(shù)組是否包含給定的?
String?值。字符串嚴格區(qū)分大小寫。返回值為?bool? - 格式:
Contains(value string) bool- 示例:建立一個空數(shù)組,設置完數(shù)據(jù)后,判斷是否包含指定數(shù)據(jù)?
e?和?z?
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
}ContainsI
- 說明:判斷一個數(shù)組是否包含給定的?
String?值。字符串不區(qū)分大小寫。返回值為?bool? - 格式:
ContainsI(value string) bool- 示例:建立一個空數(shù)組,設置完數(shù)據(jù)后,判斷是否包含指定數(shù)據(jù)?
E?和?z?
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
}CountValues
- 說明:統(tǒng)計每個值在數(shù)組中出現(xiàn)的次數(shù)。返回值為?
map[string]int? - 格式:
CountValues() map[string]int- 示例:建立一個數(shù)組,統(tǒng)計數(shù)組中每個字符串包含的個數(shù)
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]
}Fill
- 說明:在數(shù)組中指定的開始位置?
startIndex?,用指定的?value?進行填充。返回值為?error? - 格式:
Fill(startIndex int, num int, value string) error- 示例:建立一個數(shù)組,在數(shù)組開始位置?
index?為2的地方,用字符串?here?填充3個數(shù)據(jù)
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
- 說明:過濾指定數(shù)組中的空字符串
- 格式:
FilterEmpty() *StrArray- 示例:建立一個數(shù)組,在賦值后,過濾該數(shù)組中的空字符串
func ExampleStrArray_FilterEmpty() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "", "c", "", "", "d"})
fmt.Println(s.FilterEmpty())
// Output:
// ["a","b","c","d"]
}Get
- 說明:返回數(shù)組中指定?
index?的值,返回值有2個參數(shù),返回值?value?,和是否找到指定位置的數(shù)據(jù)?found?,為?true?則找到,為?false?則未找到 - 格式:
Get(index int) (value string, found bool)- 示例:建立一個數(shù)組,在賦值后,得到數(shù)組index為3的值
func ExampleStrArray_Get() {
s := garray.NewStrArrayFrom(g.SliceStr{"We", "are", "GF", "fans", "!"})
sGet, sBool := s.Get(3)
fmt.Println(sGet, sBool)
// Output:
// fans true
}InsertAfter
- 說明:在數(shù)組中指定?
index?的位置之后插入值?value?,返回值為?error? - 格式:
InsertAfter(index int, value string) error- 示例:建立一個數(shù)組,在?
index?為1的值之后,插入字符串?here?
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]
}InsertBefore
- 說明:在數(shù)組中指定?
index?的位置之前插入值?value?,返回值為?error? - 格式:
InsertBefore(index int, value string) error- 示例:建立一個數(shù)組并初始化,在?
index?為1的值之前,插入字符串?here?
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]
}Interfaces
- 說明:把當前數(shù)組作為?
[]interface{}?進行返回 - 格式:
Interfaces() []interface{}- 示例:建立一個數(shù)組并初始化,并打印出返回值?
[]interface{}?的內(nèi)容
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]
}IsEmpty
- 說明:判斷當前數(shù)組是不是空數(shù)組,如果是空數(shù)組,則返回?
true?,如果不是空數(shù)組,則返回?false? - 格式:
IsEmpty() bool- 示例:建立2個數(shù)組并初始化,并判斷是否為空數(shù)組
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
}Iterator
- 說明:數(shù)組遍歷
- 格式:
Iterator(f func(k int, v string) bool)- 示例:建立1個數(shù)組,并對其進行遍歷
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
}IteratorAsc
- 說明:根據(jù)給定的回調(diào)函數(shù)?
f?,按升序對數(shù)組進行遍歷,如果?f?返回?true?,則繼續(xù)進行遍歷,否則停止遍歷 - 格式:
IteratorAsc(f func(k int, v string) bool)- 示例:建立1個數(shù)組,并按照自定義函數(shù)對其進行升序遍歷
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
}IteratorDesc
- 說明:根據(jù)給定的回調(diào)函數(shù)?
f?,按降序對數(shù)組進行遍歷,如果?f?返回?true?,則繼續(xù)進行遍歷,否則停止遍歷 - 格式:
IteratorAsc(f func(k int, v string) bool)- 示例:建立1個數(shù)組,并按照自定義函數(shù)對其進行降序遍歷
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
}Join
- 說明:將數(shù)組元素根據(jù)給定的字符串連接符?
gule?,連接起來 - 格式:
Join(glue string) string- 示例:給定連接符?
','?,將數(shù)組中的字符串連接起來
func ExampleStrArray_Join() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
fmt.Println(s.Join(","))
// Output:
// a,b,c
}Len
- 說明:得到數(shù)組的長度
- 格式:
Join(glue string) string- 示例:建立一個新數(shù)組,初始化后得到該數(shù)組的長度
func ExampleStrArray_Len() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
fmt.Println(s.Len())
// Output:
// 8
}LockFunc
- 說明:通過回調(diào)函數(shù)?
f?對數(shù)組進行寫鎖定 - 格式:
LockFunc(f func(array []string)) *StrArray- 示例:建立一個新數(shù)組,并對該數(shù)組在寫鎖定的狀態(tài)下,修改最后一個數(shù)據(jù)
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"]
}MarshalJSON
- 說明:實現(xiàn)?
json.Marshal?的?JSON?格式的序列化接口 - 格式:
MarshalJSON() ([]byte, error)- 示例:建立一個新?
JSON?格式的數(shù)據(jù),并對該數(shù)據(jù)進行序列化的操作后,打印出相應結果
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"]}
}Merge
- 說明:合并數(shù)組,將指定數(shù)組中的內(nèi)容合并到當前數(shù)組中。參數(shù)?
array?可以是任意?garray?或?slice?類型。?Merge?和?Append?的主要區(qū)別是?Append?僅僅支持?slice?類型,?Merge?則支持更多的參數(shù)類型 - 格式:
Merge(array interface{}) *StrArray- 示例:建立2個新數(shù)組s1和s2,并將s2的數(shù)據(jù)合并到s1上
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"]
}NewStrArray
- 說明:創(chuàng)建一個新數(shù)組。?
safe?為非必需參數(shù),布爾型,是并發(fā)安全的開關,缺省值為?False? - 格式:
NewStrArray(safe ...bool) *StrArray- 示例:建立一個空數(shù)組,并添加數(shù)據(jù)。此時沒有指定 ?
Safe?參數(shù),默認為非并發(fā)安全設置
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]
}NewStrArrayFrom
- 說明:根據(jù)給定的數(shù)組內(nèi)容,創(chuàng)建一個新數(shù)組。?
safe?為非必需參數(shù),布爾型,是并發(fā)安全的開關,缺省值為?False? - 格式:
NewStrArrayFrom(array []string, safe ...bool) *StrArray- 示例:建立一個空數(shù)組,并根據(jù)指定內(nèi)容添加數(shù)據(jù)。此時沒有指定 ?
Safe?參數(shù),默認為非并發(fā)安全設置
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
}NewStrArrayFromCopy
- 說明:根據(jù)給定的數(shù)組內(nèi)容的拷貝,創(chuàng)建一個新數(shù)組。?
safe?為非必需參數(shù),布爾型,是并發(fā)安全的開關,缺省值為?False? - 格式:
NewStrArrayFrom(array []string, safe ...bool) *StrArray- 示例:建立一個空數(shù)組,并根據(jù)指定內(nèi)容添加數(shù)據(jù)。此時沒有指定 ?
Safe?參數(shù),默認為非并發(fā)安全設置
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
}NewStrArraySize
- 說明:根據(jù)給定的?
size?和?cap?,創(chuàng)建一個新數(shù)組。?safe?為非必需參數(shù),布爾型,是并發(fā)安全的開關,缺省值為?False? - 格式:
NewStrArraySize(size int, cap int, safe ...bool) *StrArray- 示例:建立一個空數(shù)組,?
Size?為3,?Cap?為5,并添加數(shù)據(jù)。打印出相應的內(nèi)容。此時沒有指定 ?Safe?參數(shù),默認為非并發(fā)安全設置
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
}Pad
- 說明:填充指定大小為?
size?的值?value?到數(shù)組中。如果大小?size?是正數(shù),則從數(shù)組的右邊開始填充。如果?size?是負數(shù),則從數(shù)組的左邊開始填充。如果?size?的大小正好等于數(shù)組的長度,那么將不會填充任何數(shù)據(jù)。 - 格式:
Pad(size int, value string) *StrArray- 示例:建立1個新數(shù)組,先從左邊將數(shù)組,用指定的字符串?
here?填充到?size?為7,然后用指定的字符串?there?將數(shù)組用字符串填充到?size?為10
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"]
}PopLeft
- 說明:從數(shù)組的左側將一個字符串數(shù)據(jù)出棧,返回值?
value?為出棧的字符串數(shù)據(jù)。更新后的數(shù)組數(shù)據(jù)為剩余數(shù)據(jù)。當數(shù)組為空時,?found?為?false?。 - 格式:
PopLeft() (value string, found bool)- 示例:建立1個新數(shù)組,將最左邊的數(shù)據(jù)出棧,并打印出剩余的數(shù)據(jù)
func ExampleStrArray_PopLeft() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d"})
s.PopLeft()
fmt.Println(s.Slice())
// Output:
// [b c d]
}PopLefts
- 說明:從數(shù)組的左側將多個字符串數(shù)據(jù)出棧,返回值為出棧的字符串數(shù)據(jù),出棧數(shù)據(jù)的個數(shù)為?
size?。如果?size?比數(shù)組的?size?大,那么方法將返回數(shù)組中所有的數(shù)據(jù)。如果?size<=0?或者為空,那么將返回?nil? - 格式:
PopLefts(size int) []string- 示例:建立1個新數(shù)組,將最左邊的2個數(shù)據(jù)做出棧操作,并打印出出棧的數(shù)據(jù)和原數(shù)組的剩余數(shù)據(jù)
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"]
}PopRand
- 說明:從數(shù)組中隨機出棧1個數(shù)據(jù),返回值為出棧的字符串數(shù)據(jù)。如果數(shù)組為空,那么?
found?將返回?false? - 格式:
PopRand() (value string, found bool)- 示例:建立1個新數(shù)組,從數(shù)組中隨機出棧1個數(shù)據(jù),并打印出出棧的數(shù)據(jù)
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
}PopRands
- 說明:從數(shù)組中隨機出棧?
size?個數(shù)據(jù),返回值為出棧的字符串數(shù)據(jù)。如果?size<=0?或者為空,那么將返回?nil? - 格式:
PopRands(size int) []string- 示例:建立1個新數(shù)組,從數(shù)組中隨機出棧2個數(shù)據(jù),并打印出出棧的數(shù)據(jù)
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]
}PopRight
- 說明:從數(shù)組的右側將一個字符串數(shù)據(jù)出棧,返回值?
value?為出棧的字符串數(shù)據(jù)。更新后的數(shù)組數(shù)據(jù)為剩余數(shù)據(jù)。當數(shù)組為空時,?found?為?false?。 - 格式:
PopRight() (value string, found bool)- 示例:建立1個新數(shù)組,將最右邊的數(shù)據(jù)出棧,并打印出剩余的數(shù)據(jù)
func ExampleStrArray_PopRight() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"a", "b", "c", "d"})
s.PopRight()
fmt.Println(s.Slice())
// Output:
// [a b c]
}PopRights
- 說明:從數(shù)組的右側將多個字符串數(shù)據(jù)出棧,返回值為出棧的字符串數(shù)據(jù),出棧數(shù)據(jù)的個數(shù)為?
size?。如果?size?比數(shù)組的?size?大,那么方法將返回數(shù)組中所有的數(shù)據(jù)。如果?size<=0?或者為空,那么將返回?nil? - 格式:
PopRights(size int) []string- 示例:建立1個新數(shù)組,將最右邊的2個數(shù)據(jù)做出棧操作,并打印出出棧的數(shù)據(jù)和原數(shù)組的剩余數(shù)據(jù)
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"]
}PushLeft
- 說明:從數(shù)組的左側入棧一個或多個字符串
- 格式:
PushLeft(value ...string) *StrArray- 示例:建立1個新數(shù)組,從數(shù)組的左側入棧多個字符串數(shù)據(jù),并打印出更新后的數(shù)據(jù)
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]
}PushRight
- 說明:從數(shù)組的右側入棧一個或多個字符串
- 格式:
PushRight(value ...string) *StrArray- 示例:建立1個新數(shù)組,從數(shù)組的右側入棧多個字符串數(shù)據(jù),并打印出更新后的數(shù)據(jù)
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]
}Rand
- 說明:從數(shù)組中隨機取出1個字符串(非刪除式)
- 格式:
Rand() (value string, found bool)- 示例:建立1個新數(shù)組,從數(shù)組中隨機取出一個字符串
func ExampleStrArray_Rand() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c", "d", "e", "f", "g", "h"})
fmt.Println(s.Rand())
// May Output:
// c true
}Rands
- 說明:從數(shù)組中隨機取出?
size?個字符串(非刪除式) - 格式:
Rands(size int) []string- 示例:建立1個新數(shù)組,從數(shù)組中隨機取出3個字符串
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]
}Range
- 說明:獲取數(shù)組中指定范圍的數(shù)據(jù)。如果是在并發(fā)安全的模式下使用,則該方法返回一個?
slice?拷貝。 - 格式:
Range(start int, end ...int) []string- 示例:建立1個新數(shù)組,獲取數(shù)組從?
index?為2至5位置的數(shù)據(jù)
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]
}Remove
- 說明:從數(shù)組中移除位置在?
index?處的數(shù)據(jù)。如果?index?超過數(shù)組的邊界,則?found?返回?false? - 格式:
Remove(index int) (value string, found bool)- 示例:建立1個新數(shù)組,移除數(shù)組?
index?為1的數(shù)據(jù)
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]
}RemoveValue
- 說明:從數(shù)組中移除指定的數(shù)據(jù)?
value?。如果?value?在數(shù)組中被找到,則?found?返回?true?,否則?found?返回?false? - 格式:
RemoveValue(value string) bool- 示例:建立1個新數(shù)組,移除數(shù)組中值為b的數(shù)據(jù)
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]
}Replace
- 說明:將原字符串數(shù)組用指定的字符串數(shù)組進行替換,替換從原數(shù)組的頭部位置開始。
- 格式:
Replace(array []string) *StrArray- 示例:建立1個新數(shù)組,并用指定的字符串數(shù)組進行替換
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 !]
}Reverse
- 說明:將數(shù)組中的所有元素用倒序進行排序
- 格式:
Replace(array []string) *StrArray- 示例:建立1個新數(shù)組,初始化后執(zhí)行倒序操作并打印
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"]
}RLockFunc
- 說明:通過自定義回調(diào)函數(shù)?
f?進行數(shù)組的讀鎖定 - 格式:
RLockFunc(f func(array []string)) *StrArray- 示例:建立1個新數(shù)組,在回調(diào)函數(shù)?
f?中,對數(shù)組進行遍歷,并打印出數(shù)組元素
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
}Search
- 說明:在數(shù)組中搜索指定的字符串,返回值為該值在數(shù)組中的?
index?,如果沒有查詢到,則返回-1 - 格式:
Search(value string) int- 示例:建立1個新數(shù)組,并在該數(shù)組中搜索字符串e和z
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
}Set
- 說明:給數(shù)組中指定位置的?
index?設置值?value?,如果?index<0?或者?index?超出了數(shù)組的邊界,則返回錯誤?error? - 格式:
Set(index int, value string) error- 示例:建立1個新數(shù)組,長度為3,給數(shù)組設置值,值最終只按順序設置到了?
index?為2的位置,因為數(shù)組長度限制,最后一個值未設置成功
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]
}SetArray
- 說明:根據(jù)給定的?
slice?數(shù)組內(nèi)容給數(shù)組賦值 - 格式:
SetArray(array []string) *StrArray- 示例:建立1個新數(shù)組,在給其賦值后,打印出來
func ExampleStrArray_SetArray() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"We", "are", "GF", "fans", "!"})
fmt.Println(s.Slice())
// Output:
// [We are GF fans !]
}Shuffle
- 說明:將數(shù)組中的內(nèi)容進行亂序排列
- 格式:
Shuffle() *StrArray- 示例:建立1個新數(shù)組,在給其賦值后進行亂序排列,并打印出來
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"]
}Slice
- 說明:得到數(shù)組的?
slice?切片數(shù)據(jù),注意,如果是在并發(fā)安全模式下,返回的是一份拷貝數(shù)據(jù),否則返回的是指向數(shù)據(jù)的指針 - 格式:
Shuffle() *StrArray- 示例:建立1個新數(shù)組,在給其賦值后,并打印該數(shù)組的切片數(shù)據(jù)
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]
}Sort
- 說明:對數(shù)組內(nèi)容進行升序排序。?
reverse?控制排序的方向,默認為?true?升序,?false?為降序 - 格式:
Sort(reverse ...bool) *StrArray- 示例:建立1個新數(shù)組,在給其賦值后,并按升序進行排序
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"]
}SortFunc
- 說明:通過自定義函數(shù)?
less?對數(shù)組內(nèi)容進行排序。 - 格式:
SortFunc(less func(v1, v2 string) bool) *StrArray- 示例:建立1個新數(shù)組,在給其賦值后,首先用自定義函數(shù)對其進行降序排序,然后用自定義函數(shù)對其進行升序排序,并打印出相應的結果
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"]
}String
- 說明:將當前的數(shù)組轉換成?
string?, - 格式:
String() string- 示例:建立1個新數(shù)組,在給其賦值后,將數(shù)組轉換成?
string?,并打印出相應的結果
func ExampleStrArray_String() {
s := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
fmt.Println(s.String())
// Output:
// ["a","b","c"]
}Subslice
- 說明:根據(jù)給定的偏離值?
offset?和長度?length?參數(shù)獲得數(shù)組的切片,注意,如果是在并發(fā)安全模式下,返回拷貝數(shù)據(jù),否則返回指向數(shù)據(jù)的指針。如果偏離值?offset?是非負數(shù),則從數(shù)組的開始位置進行切片,否則從數(shù)組的尾部開始切片。 - 格式:
SubSlice(offset int, length ...int)- 示例:建立1個新數(shù)組,在給其賦值后,將數(shù)組轉換成?
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
- 說明:對數(shù)組中的整數(shù)值進行求和
- 格式:
Sum() (sum int)- 示例:建立1個新數(shù)組,在給其賦值后,對數(shù)組中的整數(shù)值進行求和
func ExampleStrArray_Sum() {
s := garray.NewStrArray()
s.SetArray(g.SliceStr{"3", "5", "10"})
a := s.Sum()
fmt.Println(a)
// Output:
// 18
}Unique
- 說明:對數(shù)組中的數(shù)據(jù)進行去重處理
- 格式:
Unique() *StrArray- 示例:建立1個新數(shù)組,在給其賦值后,對數(shù)組中的整數(shù)值進行求和
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"]
}UnmarshalJSON
- 說明:實現(xiàn)?
json.Unmarshal?的?UnmarshalJSON?接口 - 格式:
Unique() *StrArray- 示例:建立1個?
byte?切片,將其賦值給結構體后,進行反序列化操作,打印出相應的內(nèi)容
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
- 說明:對任意類型的值實現(xiàn)反序列化接口
- 格式:
UnmarshalValue(value interface{}) error- 示例:建立1個結構體,并對其值 進行反序列化操作,打印出相應的內(nèi)容
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"]}
}Walk
- 說明:通過自定義函數(shù)?
f?,對數(shù)組內(nèi)容進行遍歷修改 - 格式:
Walk(f func(value string) string) *StrArray- 示例:建立1個數(shù)組,對數(shù)組內(nèi)容進行遍歷修改,為每個字符串添加前綴,并打印出相應的內(nèi)容
func 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]
} 本文題目:創(chuàng)新互聯(lián)GoFrame教程:GoFrame garray-方法介紹
標題來源:http://fisionsoft.com.cn/article/djjjdoe.html


咨詢
建站咨詢
