W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
以下常用方法列表,文檔更新可能滯后于代碼新特性,更多的方法及示例請參考代碼文檔:https://pkg.go.dev/github.com/gogf/gf/v2/container/gset
這里以?StrSet
?類型介紹方法使用,其他集合類型的方法與此類似,不再贅述。
NewStrSet
?創(chuàng)建并返回一個(gè)空的集合,其中包含沒有重復(fù)字符串的數(shù)據(jù)。參數(shù)?safe
?用于指定是否在并發(fā)安全中使用,默認(rèn)情況下是?false
?。
func NewStrSet(safe ...bool) *StrSet
func ExampleNewStrSet() {
strSet := gset.NewStrSet(true)
strSet.Add([]string{"str1", "str2", "str3"}...)
fmt.Println(strSet.Slice())
// May Output:
// [str3 str1 str2]
}
NewStrSetFrom
?通過給定的數(shù)組創(chuàng)建集合集合。參數(shù)safe用于指定是否在并發(fā)安全中使用,默認(rèn)情況下是?false
?。
func NewStrSetFrom(items []string, safe ...bool) *StrSet
func ExampleNewStrSetFrom() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
fmt.Println(strSet.Slice())
// May Output:
// [str1 str2 str3]
}
Add
?添加一個(gè)或多個(gè)元素項(xiàng)到集合中。
func (set *StrSet) Add(item ...string)
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
?檢查集合中是否存在指定元素項(xiàng)?item
?,如果不存在則將?item
?添加到集合中并返回?true
?,否則什么也不做并返回?false
?。
func (set *StrSet) AddIfNotExist(item string) bool
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
?檢查集合中存在指定元素項(xiàng)?item
?,如果不存在并且方法f返回?true
?時(shí),則將?item
?設(shè)置到集合中并返回?true
?,否則什么也不做并返回?false
?。
func (set *StrSet) AddIfNotExistFunc(item string, f func() bool) bool
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
}
AddifnotExistFuncLock
?與?AddIfNotExistFunc
?類似,不過當(dāng)多個(gè)?goroutine
?同時(shí)調(diào)用?AddifnotExistFuncLock
?方法時(shí),內(nèi)部使用并發(fā)安全鎖機(jī)制保證同時(shí)只能一個(gè)?goroutine
?執(zhí)行。該方法只有在創(chuàng)建集合時(shí)的?safe
?參數(shù)設(shè)置?true
?時(shí)有效,否則表現(xiàn)和方法一致?AddIfNotExistFunc
?。
func (set *StrSet) AddIfNotExistFuncLock(item string, f func() bool) bool
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
?刪除集合的所有元素項(xiàng)。
func (set *StrSet) Clear()
func ExampleStrSet_Clear() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
fmt.Println(strSet.Size())
strSet.Clear()
fmt.Println(strSet.Size())
// Output:
// 3
// 0
}
Intrersect
?執(zhí)行集合?set
?與?others
?的交集,并返回一個(gè)新的集合?newSet
?,在?newSet
?中的元素項(xiàng)同時(shí)存在于集合?set
?與?others
?中。
func (set *StrSet) Intersect(others ...*StrSet) (newSet *StrSet)
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]
}
Diff
?執(zhí)行集合?set
?與?others
?差集操作,并返回一個(gè)新的集合?newSet
?。在?newSet
?中的元素項(xiàng)存在于?set
?但不存在于集合?others
?。注意,參數(shù)?others
?可以指定多個(gè)集合參數(shù)。
func (set *StrSet) Diff(others ...*StrSet) (newSet *StrSet)
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]
}
union
?執(zhí)行集合?set
?與?others
?的并集操作,并返回一個(gè)新的集合?newSet
?。
func (set *StrSet) Union(others ...*StrSet) (newSet *StrSet)
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]
}
Complement
?執(zhí)行?set
?與?full
?的補(bǔ)集操作,并返回一個(gè)新集合?newSet
?。
func (set *StrSet) Complement(full *StrSet) (newSet *StrSet)
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
?包含檢查集是否包含?item
?。
func (set *StrSet) Contains(item string) bool
func ExampleStrSet_Contains() {
var set gset.StrSet
set.Add("a")
fmt.Println(set.Contains("a"))
fmt.Println(set.Contains("A"))
// Output:
// true
// false
}
ContainsI
?方法類似于?Contains
?,只是它不區(qū)分大小寫比較大小。
func (set *StrSet) ContainsI(item string) bool
func ExampleStrSet_ContainsI() {
var set gset.StrSet
set.Add("a")
fmt.Println(set.ContainsI("a"))
fmt.Println(set.ContainsI("A"))
// Output:
// true
// true
}
Equal
?檢查兩個(gè)集合是否完全相等(包括大小以及元素項(xiàng))。
func (set *StrSet) Equal(other *StrSet) bool
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
}
IsSumSetOf
?檢查當(dāng)前集合?set
?是否是指定集合?other
?的子集。
func (set *StrSet) IsSubsetOf(other *StrSet) bool
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
?迭代器通過給定的回調(diào)函數(shù)f隨機(jī)遍歷當(dāng)前集合?set
?,如果方法?f
?返回?true
?,則繼續(xù)遍歷,否則停止。
func (set *StrSet) Iterator(f func(v string) bool)
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
?將集合中的元素項(xiàng)通過字符串?glue
?拼接成新的字符串返回。
func (set *StrSet) Join(glue string) string
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
?僅在并發(fā)安全場景下有用,該方法通過寫鎖鎖定集合?set
?,并執(zhí)行回調(diào)方法?f
?。
func (set *StrSet) LockFunc(f func(m map[string]struct{}))
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]
}
RLockFunc
?僅在并發(fā)安全場景下有用,該方法通過讀鎖鎖定集合?set
?,并執(zhí)行回調(diào)方法?f
?。
func (set *StrSet) RLockFunc(f func(m map[string]struct{}))
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:{}]
}
Merge
?將集合?others
?中的所有元素項(xiàng)合并到?set
?中。
func (set *StrSet) Merge(others ...*StrSet) *StrSet
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]
}
Pop
?隨機(jī)從集合中取出一個(gè)元素項(xiàng)。
func (set *StrSet) Pop() string
func ExampleStrSet_Pop() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
fmt.Println(s1.Pop())
// May Output:
// a
}
Pops
?從集合中隨機(jī)彈出?size
?個(gè)元素項(xiàng)。如果?size == -1
?,則返回所有元素項(xiàng)。
func (set *StrSet) Pops(size int) []string
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
}
Remove
?從集合中刪除指定的元素項(xiàng)?item
?。
func (set *StrSet) Remove(item string)
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
?返回集合的大小。
func (set *StrSet) Size() int
func ExampleStrSet_Size() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
fmt.Println(s1.Size())
// Output:
// 4
}
Slice
?將集合中的元素項(xiàng)以?slice
?的形式返回。
func (set *StrSet) Slice() []string
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
?將集合按照字符串返回。
func (set *StrSet) String() string
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
?將集合中的元素項(xiàng)執(zhí)行求和,注意:只有元素項(xiàng)為數(shù)字時(shí)才有效,否則您將得到一個(gè)意想不到的結(jié)果。
func (set *StrSet) Sum() (sum int)
func ExampleStrSet_Sum() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"1", "2", "3", "4"}...)
fmt.Println(s1.Sum())
// Output:
// 10
}
Walk
?按照用戶給定的回調(diào)方法f遍歷當(dāng)前集合,并將f的返回結(jié)果重新設(shè)置當(dāng)前集合。注意,在并發(fā)安全場景中,該方法內(nèi)部使用寫鎖來保證并發(fā)安全性。
func (set *StrSet) Walk(f func(item string) string) *StrSet
func ExampleStrSet_Walk() {
var (
set gset.StrSet
names = g.SliceStr{"user", "user_detail"}
prefix = "gf_"
)
set.Add(names...)
// Add prefix for given table names.
set.Walk(func(item string) string {
return prefix + item
})
fmt.Println(set.Slice())
// May Output:
// [gf_user gf_user_detail]
}
MarshalJSON
?實(shí)現(xiàn)了?json.Marshal
?的?MarshalJSON
?接口。
func (set *StrSet) MarshalJSON() ([]byte, error)
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"]}
}
UnmarshalJSON
?實(shí)現(xiàn)了?json.Unmarshal
?中的?UnmarshalJSON
?接口。
func (set *StrSet) UnmarshalJSON(b []byte) error
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"}
}
UnfarshalValue
?實(shí)現(xiàn)?goframe
?框架內(nèi)部統(tǒng)一的設(shè)置接口,它通過一個(gè)?interface{}
?類型的參數(shù)初始化當(dāng)前對象,至于?interface{}
?參數(shù)的使用邏輯由該接口實(shí)現(xiàn)方法決定。
func (set *StrSet) UnmarshalValue(value interface{}) (err error)
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"}
}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: