tip
The following is a list of common methods. The documentation may lag behind new features in the code. For more methods and examples, please refer to the code documentation: https://pkg.go.dev/github.com/gogf/gf/v2/container/gset
tip
The usage of methods is introduced using the StrSet type; methods for other set types are similar and will not be repeated.
NewStrSet
- Description:
NewStrSetcreates and returns an empty set that contains unique string data. Thesafeparameter specifies whether to use in concurrent safety, with a default value offalse. - Signature:
func NewStrSet(safe ...bool) *StrSet
- Example:
func ExampleNewStrSet() {
strSet := gset.NewStrSet(true)
strSet.Add([]string{"str1", "str2", "str3"}...)
fmt.Println(strSet.Slice())
// May Output:
// [str3 str1 str2]
}
NewStrSetFrom
- Description:
NewStrSetFromcreates a set from a given array. Thesafeparameter specifies whether to use in concurrent safety, with a default value offalse. - Signature:
func NewStrSetFrom(items []string, safe ...bool) *StrSet
- Example:
func ExampleNewStrSetFrom() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
fmt.Println(strSet.Slice())
// May Output:
// [str1 str2 str3]
}
Add
- Description:
Addadds one or more elements to the set. - Signature:
func (set *StrSet) Add(item ...string)
- Example:
func ExampleStrSet_Add() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
strSet.Add("str")
fmt.Println(strSet.Slice())
fmt.Println(strSet.AddIfNotExist("str"))
// May Output:
// [str str1 str2 str3]
// false
}
AddIfNotExist
- Description:
AddIfNotExistchecks if the specified elementitemexists in the set. If it does not exist, it addsitemto the set and returnstrue; otherwise, it does nothing and returnsfalse. - Signature:
func (set *StrSet) AddIfNotExist(item string) bool
- Example:
func ExampleStrSet_AddIfNotExist() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
strSet.Add("str")
fmt.Println(strSet.Slice())
fmt.Println(strSet.AddIfNotExist("str"))
// May Output:
// [str str1 str2 str3]
// false
}
AddIfNotExistFunc
- Description:
AddIfNotExistFuncchecks if the specified elementitemexists in the set. If it does not exist and the functionfreturnstrue, it setsiteminto the set and returnstrue; otherwise, it does nothing and returnsfalse. - Signature:
func (set *StrSet) AddIfNotExistFunc(item string, f func() bool) bool
- Example:
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
- Description:
AddIfNotExistFuncLockis similar toAddIfNotExistFunc, but when multiple goroutines simultaneously callAddIfNotExistFuncLock, it uses a concurrent safety lock mechanism to ensure that only one goroutine executes at a time. This method is effective only if thesafeparameter is set totruewhen creating the set; otherwise, it behaves like theAddIfNotExistFuncmethod. - Signature:
func (set *StrSet) AddIfNotExistFuncLock(item string, f func() bool) bool
- Example:
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
- Description:
Clearremoves all elements from the set. - Signature:
func (set *StrSet) Clear()
- Example:
func ExampleStrSet_Clear() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
fmt.Println(strSet.Size())
strSet.Clear()
fmt.Println(strSet.Size())
// Output:
// 3
// 0
}
Intersect
- Description:
Intersectperforms an intersection operation between the setsetandothers, returning a new setnewSetwhere the elements exist in bothsetandothers. - Signature:
func (set *StrSet) Intersect(others ...*StrSet) (newSet *StrSet)
- Example:
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
- Description:
Diffperforms a difference operation between the setsetandothers, returning a new setnewSet. The elements innewSetexist insetbut not inothers. Note thatotherscan specify multiple set parameters. - Signature:
func (set *StrSet) Diff(others ...*StrSet) (newSet *StrSet)
- Example:
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
- Description:
Unionperforms a union operation between the setsetandothers, returning a new setnewSet. - Signature:
func (set *StrSet) Union(others ...*StrSet) (newSet *StrSet)
- Example:
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
- Description:
Complementperforms a complement operation betweensetandfull, returning a new setnewSet. - Signature:
func (set *StrSet) Complement(full *StrSet) (newSet *StrSet)
- Example:
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
- Description:
Containschecks if the set containsitem. - Signature:
func (set *StrSet) Contains(item string) bool
- Example:
func ExampleStrSet_Contains() {
var set gset.StrSet
set.Add("a")
fmt.Println(set.Contains("a"))
fmt.Println(set.Contains("A"))
// Output:
// true
// false
}
ContainsI
- Description:
ContainsIis similar toContains, but it performs a case-insensitive comparison. - Signature:
func (set *StrSet) ContainsI(item string) bool
- Example:
func ExampleStrSet_ContainsI() {
var set gset.StrSet
set.Add("a")
fmt.Println(set.ContainsI("a"))
fmt.Println(set.ContainsI("A"))
// Output:
// true
// true
}
Equal
- Description:
Equalchecks whether two sets are completely equal (including size and elements). - Signature:
func (set *StrSet) Equal(other *StrSet) bool
- Example:
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
}
IsSubSetOf
- Description:
IsSubsetOfchecks whether the current setsetis a subset of the specified setother. - Signature:
func (set *StrSet) IsSubsetOf(other *StrSet) bool
- Example:
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
- Description:
Iteratoriterates over the current setsetusing the given callback functionf. If the functionfreturnstrue, it continues iteration; otherwise, it stops. - Signature:
func (set *StrSet) Iterator(f func(v string) bool)
- Example:
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
- Description:
Joinconcatenates the elements of the set into a new string usingglue. - Signature:
func (set *StrSet) Join(glue string) string
- Example:
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
- Description:
LockFuncis useful only in concurrent safety scenarios. It locks the setsetwith a write lock and executes the callback functionf. - Signature:
func (set *StrSet) LockFunc(f func(m map[string]struct{}))
- Example:
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
- Description:
RLockFuncis useful only in concurrent safety scenarios. It locks the setsetwith a read lock and executes the callback functionf. - Signature:
func (set *StrSet) RLockFunc(f func(m map[string]struct{}))
- Example:
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
- Description:
Mergemerges all elements from theotherssets intoset. - Signature:
func (set *StrSet) Merge(others ...*StrSet) *StrSet
- Example:
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
- Description:
Poprandomly retrieves an element from the set. - Signature:
func (set *StrSet) Pop() string
- Example:
func ExampleStrSet_Pop() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
fmt.Println(s1.Pop())
// May Output:
// a
}
Pops
- Description:
Popsrandomly popssizenumber of elements from the set. Ifsize == -1, it returns all elements. - Signature:
func (set *StrSet) Pops(size int) []string
- Example:
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
- Description:
Removeremoves the specified elementitemfrom the set. - Signature:
func (set *StrSet) Remove(item string)
- Example:
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
- Description:
Sizereturns the size of the set. - Signature:
func (set *StrSet) Size() int
- Example:
func ExampleStrSet_Size() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
fmt.Println(s1.Size())
// Output:
// 4
}
Silce
- Description:
Slicereturns the elements of the set as a slice. - Signature:
func (set *StrSet) Slice() []string
- Example:
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
- Description:
Stringreturns the set as a string. - Signature:
func (set *StrSet) String() string
- Example:
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
- Description:
Sumsums up the elements of the set. Note: It is valid only when the elements are numbers; otherwise, you will get an unexpected result. - Signature:
func (set *StrSet) Sum() (sum int)
- Example:
func ExampleStrSet_Sum() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"1", "2", "3", "4"}...)
fmt.Println(s1.Sum())
// Output:
// 10
}
Walk
- Description:
Walktraverses the current set with the user-provided callback functionf, and resets the current set with the return results off. Note, in a concurrent safety scenario, this method uses a write lock internally to ensure safety. - Signature:
func (set *StrSet) Walk(f func(item string) string) *StrSet
- Example:
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
- Description:
MarshalJSONimplements theMarshalJSONinterface ofjson.Marshal. - Signature:
func (set *StrSet) MarshalJSON() ([]byte, error)
- Example:
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
- Description:
UnmarshalJSONimplements theUnmarshalJSONinterface ofjson.Unmarshal. - Signature:
func (set *StrSet) UnmarshalJSON(b []byte) error
- Example:
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
- Description:
UnmarshalValueimplements the internal unified setting interface of thegoframeframework. It initializes the current object with aninterface{}type parameter, the usage logic of which is determined by the implementation method of this interface. - Signature:
func (set *StrSet) UnmarshalValue(value interface{}) (err error)
- Example:
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"}
}