Set - Methods - 图1tip

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

Set - Methods - 图2tip

The usage of methods is introduced using the StrSet type; methods for other set types are similar and will not be repeated.

NewStrSet

  • Description: NewStrSet creates and returns an empty set that contains unique string data. The safe parameter specifies whether to use in concurrent safety, with a default value of false.
  • Signature:
  1. func NewStrSet(safe ...bool) *StrSet
  • Example:
  1. func ExampleNewStrSet() {
  2. strSet := gset.NewStrSet(true)
  3. strSet.Add([]string{"str1", "str2", "str3"}...)
  4. fmt.Println(strSet.Slice())
  5. // May Output:
  6. // [str3 str1 str2]
  7. }

NewStrSetFrom

  • Description: NewStrSetFrom creates a set from a given array. The safe parameter specifies whether to use in concurrent safety, with a default value of false.
  • Signature:
  1. func NewStrSetFrom(items []string, safe ...bool) *StrSet
  • Example:
  1. func ExampleNewStrSetFrom() {
  2. strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
  3. fmt.Println(strSet.Slice())
  4. // May Output:
  5. // [str1 str2 str3]
  6. }

Add

  • Description: Add adds one or more elements to the set.
  • Signature:
  1. func (set *StrSet) Add(item ...string)
  • Example:
  1. func ExampleStrSet_Add() {
  2. strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
  3. strSet.Add("str")
  4. fmt.Println(strSet.Slice())
  5. fmt.Println(strSet.AddIfNotExist("str"))
  6. // May Output:
  7. // [str str1 str2 str3]
  8. // false
  9. }

AddIfNotExist

  • Description: AddIfNotExist checks if the specified element item exists in the set. If it does not exist, it adds item to the set and returns true; otherwise, it does nothing and returns false.
  • Signature:
  1. func (set *StrSet) AddIfNotExist(item string) bool
  • Example:
  1. func ExampleStrSet_AddIfNotExist() {
  2. strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
  3. strSet.Add("str")
  4. fmt.Println(strSet.Slice())
  5. fmt.Println(strSet.AddIfNotExist("str"))
  6. // May Output:
  7. // [str str1 str2 str3]
  8. // false
  9. }

AddIfNotExistFunc

  • Description: AddIfNotExistFunc checks if the specified element item exists in the set. If it does not exist and the function f returns true, it sets item into the set and returns true; otherwise, it does nothing and returns false.
  • Signature:
  1. func (set *StrSet) AddIfNotExistFunc(item string, f func() bool) bool
  • Example:
  1. func ExampleStrSet_AddIfNotExistFunc() {
  2. strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
  3. strSet.Add("str")
  4. fmt.Println(strSet.Slice())
  5. fmt.Println(strSet.AddIfNotExistFunc("str5", func() bool {
  6. return true
  7. }))
  8. // May Output:
  9. // [str1 str2 str3 str]
  10. // true
  11. }

AddIfNotExistFuncLock

  • Description: AddIfNotExistFuncLock is similar to AddIfNotExistFunc, but when multiple goroutines simultaneously call AddIfNotExistFuncLock, it uses a concurrent safety lock mechanism to ensure that only one goroutine executes at a time. This method is effective only if the safe parameter is set to true when creating the set; otherwise, it behaves like the AddIfNotExistFunc method.
  • Signature:
  1. func (set *StrSet) AddIfNotExistFuncLock(item string, f func() bool) bool
  • Example:
  1. func ExampleStrSet_AddIfNotExistFuncLock() {
  2. strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
  3. strSet.Add("str")
  4. fmt.Println(strSet.Slice())
  5. fmt.Println(strSet.AddIfNotExistFuncLock("str4", func() bool {
  6. return true
  7. }))
  8. // May Output:
  9. // [str1 str2 str3 str]
  10. // true
  11. }

Clear

  • Description: Clear removes all elements from the set.
  • Signature:
  1. func (set *StrSet) Clear()
  • Example:
  1. func ExampleStrSet_Clear() {
  2. strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
  3. fmt.Println(strSet.Size())
  4. strSet.Clear()
  5. fmt.Println(strSet.Size())
  6. // Output:
  7. // 3
  8. // 0
  9. }

Intersect

  • Description: Intersect performs an intersection operation between the set set and others, returning a new set newSet where the elements exist in both set and others.
  • Signature:
  1. func (set *StrSet) Intersect(others ...*StrSet) (newSet *StrSet)
  • Example:
  1. func ExampleStrSet_Intersect() {
  2. s1 := gset.NewStrSet(true)
  3. s1.Add([]string{"a", "b", "c"}...)
  4. var s2 gset.StrSet
  5. s2.Add([]string{"a", "b", "c", "d"}...)
  6. fmt.Println(s2.Intersect(s1).Slice())
  7. // May Output:
  8. // [c a b]
  9. }

Diff

  • Description: Diff performs a difference operation between the set set and others, returning a new set newSet. The elements in newSet exist in set but not in others. Note that others can specify multiple set parameters.
  • Signature:
  1. func (set *StrSet) Diff(others ...*StrSet) (newSet *StrSet)
  • Example:
  1. func ExampleStrSet_Diff() {
  2. s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
  3. s2 := gset.NewStrSetFrom([]string{"a", "b", "c", "d"}, true)
  4. fmt.Println(s2.Diff(s1).Slice())
  5. // Output:
  6. // [d]
  7. }

Union

  • Description: Union performs a union operation between the set set and others, returning a new set newSet.
  • Signature:
  1. func (set *StrSet) Union(others ...*StrSet) (newSet *StrSet)
  • Example:
  1. func ExampleStrSet_Union() {
  2. s1 := gset.NewStrSet(true)
  3. s1.Add([]string{"a", "b", "c", "d"}...)
  4. s2 := gset.NewStrSet(true)
  5. s2.Add([]string{"a", "b", "d"}...)
  6. fmt.Println(s1.Union(s2).Slice())
  7. // May Output:
  8. // [a b c d]
  9. }

Complement

  • Description: Complement performs a complement operation between set and full, returning a new set newSet.
  • Signature:
  1. func (set *StrSet) Complement(full *StrSet) (newSet *StrSet)
  • Example:
  1. func ExampleStrSet_Complement() {
  2. strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3", "str4", "str5"}, true)
  3. s := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
  4. fmt.Println(s.Complement(strSet).Slice())
  5. // May Output:
  6. // [str4 str5]
  7. }

Contains

  • Description: Contains checks if the set contains item.
  • Signature:
  1. func (set *StrSet) Contains(item string) bool
  • Example:
  1. func ExampleStrSet_Contains() {
  2. var set gset.StrSet
  3. set.Add("a")
  4. fmt.Println(set.Contains("a"))
  5. fmt.Println(set.Contains("A"))
  6. // Output:
  7. // true
  8. // false
  9. }

ContainsI

  • Description: ContainsI is similar to Contains, but it performs a case-insensitive comparison.
  • Signature:
  1. func (set *StrSet) ContainsI(item string) bool
  • Example:
  1. func ExampleStrSet_ContainsI() {
  2. var set gset.StrSet
  3. set.Add("a")
  4. fmt.Println(set.ContainsI("a"))
  5. fmt.Println(set.ContainsI("A"))
  6. // Output:
  7. // true
  8. // true
  9. }

Equal

  • Description: Equal checks whether two sets are completely equal (including size and elements).
  • Signature:
  1. func (set *StrSet) Equal(other *StrSet) bool
  • Example:
  1. func ExampleStrSet_Equal() {
  2. s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
  3. s2 := gset.NewStrSetFrom([]string{"a", "b", "c", "d"}, true)
  4. fmt.Println(s2.Equal(s1))
  5. s3 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
  6. s4 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
  7. fmt.Println(s3.Equal(s4))
  8. // Output:
  9. // false
  10. // true
  11. }

IsSubSetOf

  • Description: IsSubsetOf checks whether the current set set is a subset of the specified set other.
  • Signature:
  1. func (set *StrSet) IsSubsetOf(other *StrSet) bool
  • Example:
  1. func ExampleStrSet_IsSubsetOf() {
  2. s1 := gset.NewStrSet(true)
  3. s1.Add([]string{"a", "b", "c", "d"}...)
  4. var s2 gset.StrSet
  5. s2.Add([]string{"a", "b", "d"}...)
  6. fmt.Println(s2.IsSubsetOf(s1))
  7. // Output:
  8. // true
  9. }

Iterator

  • Description: Iterator iterates over the current set set using the given callback function f. If the function f returns true, it continues iteration; otherwise, it stops.
  • Signature:
  1. func (set *StrSet) Iterator(f func(v string) bool)
  • Example:
  1. func ExampleStrSet_Iterator() {
  2. s1 := gset.NewStrSet(true)
  3. s1.Add([]string{"a", "b", "c", "d"}...)
  4. s1.Iterator(func(v string) bool {
  5. fmt.Println("Iterator", v)
  6. return true
  7. })
  8. // May Output:
  9. // Iterator a
  10. // Iterator b
  11. // Iterator c
  12. // Iterator d
  13. }

Join

  • Description: Join concatenates the elements of the set into a new string using glue.
  • Signature:
  1. func (set *StrSet) Join(glue string) string
  • Example:
  1. func ExampleStrSet_Join() {
  2. s1 := gset.NewStrSet(true)
  3. s1.Add([]string{"a", "b", "c", "d"}...)
  4. fmt.Println(s1.Join(","))
  5. // May Output:
  6. // b,c,d,a
  7. }

LockFunc

  • Description: LockFunc is useful only in concurrent safety scenarios. It locks the set set with a write lock and executes the callback function f.
  • Signature:
  1. func (set *StrSet) LockFunc(f func(m map[string]struct{}))
  • Example:
  1. func ExampleStrSet_LockFunc() {
  2. s1 := gset.NewStrSet(true)
  3. s1.Add([]string{"1", "2"}...)
  4. s1.LockFunc(func(m map[string]struct{}) {
  5. m["3"] = struct{}{}
  6. })
  7. fmt.Println(s1.Slice())
  8. // May Output
  9. // [2 3 1]
  10. }

RLockFunc

  • Description: RLockFunc is useful only in concurrent safety scenarios. It locks the set set with a read lock and executes the callback function f.
  • Signature:
  1. func (set *StrSet) RLockFunc(f func(m map[string]struct{}))
  • Example:
  1. func ExampleStrSet_RLockFunc() {
  2. s1 := gset.NewStrSet(true)
  3. s1.Add([]string{"a", "b", "c", "d"}...)
  4. s1.RLockFunc(func(m map[string]struct{}) {
  5. fmt.Println(m)
  6. })
  7. // Output:
  8. // map[a:{} b:{} c:{} d:{}]
  9. }

Merge

  • Description: Merge merges all elements from the others sets into set.
  • Signature:
  1. func (set *StrSet) Merge(others ...*StrSet) *StrSet
  • Example:
  1. func ExampleStrSet_Merge() {
  2. s1 := gset.NewStrSet(true)
  3. s1.Add([]string{"a", "b", "c", "d"}...)
  4. s2 := gset.NewStrSet(true)
  5. fmt.Println(s1.Merge(s2).Slice())
  6. // May Output:
  7. // [d a b c]
  8. }

Pop

  • Description: Pop randomly retrieves an element from the set.
  • Signature:
  1. func (set *StrSet) Pop() string
  • Example:
  1. func ExampleStrSet_Pop() {
  2. s1 := gset.NewStrSet(true)
  3. s1.Add([]string{"a", "b", "c", "d"}...)
  4. fmt.Println(s1.Pop())
  5. // May Output:
  6. // a
  7. }

Pops

  • Description: Pops randomly pops size number of elements from the set. If size == -1, it returns all elements.
  • Signature:
  1. func (set *StrSet) Pops(size int) []string
  • Example:
  1. func ExampleStrSet_Pops() {
  2. s1 := gset.NewStrSet(true)
  3. s1.Add([]string{"a", "b", "c", "d"}...)
  4. for _, v := range s1.Pops(2) {
  5. fmt.Println(v)
  6. }
  7. // May Output:
  8. // a
  9. // b
  10. }

Remove

  • Description: Remove removes the specified element item from the set.
  • Signature:
  1. func (set *StrSet) Remove(item string)
  • Example:
  1. func ExampleStrSet_Remove() {
  2. s1 := gset.NewStrSet(true)
  3. s1.Add([]string{"a", "b", "c", "d"}...)
  4. s1.Remove("a")
  5. fmt.Println(s1.Slice())
  6. // May Output:
  7. // [b c d]
  8. }

Size

  • Description: Size returns the size of the set.
  • Signature:
  1. func (set *StrSet) Size() int
  • Example:
  1. func ExampleStrSet_Size() {
  2. s1 := gset.NewStrSet(true)
  3. s1.Add([]string{"a", "b", "c", "d"}...)
  4. fmt.Println(s1.Size())
  5. // Output:
  6. // 4
  7. }

Silce

  • Description: Slice returns the elements of the set as a slice.
  • Signature:
  1. func (set *StrSet) Slice() []string
  • Example:
  1. func ExampleStrSet_Slice() {
  2. s1 := gset.NewStrSet(true)
  3. s1.Add([]string{"a", "b", "c", "d"}...)
  4. fmt.Println(s1.Slice())
  5. // May Output:
  6. // [a,b,c,d]
  7. }

String

  • Description: String returns the set as a string.
  • Signature:
  1. func (set *StrSet) String() string
  • Example:
  1. func ExampleStrSet_String() {
  2. s1 := gset.NewStrSet(true)
  3. s1.Add([]string{"a", "b", "c", "d"}...)
  4. fmt.Println(s1.String())
  5. // May Output:
  6. // "a","b","c","d"
  7. }

Sum

  • Description: Sum sums up the elements of the set. Note: It is valid only when the elements are numbers; otherwise, you will get an unexpected result.
  • Signature:
  1. func (set *StrSet) Sum() (sum int)
  • Example:
  1. func ExampleStrSet_Sum() {
  2. s1 := gset.NewStrSet(true)
  3. s1.Add([]string{"1", "2", "3", "4"}...)
  4. fmt.Println(s1.Sum())
  5. // Output:
  6. // 10
  7. }

Walk

  • Description: Walk traverses the current set with the user-provided callback function f, and resets the current set with the return results of f. Note, in a concurrent safety scenario, this method uses a write lock internally to ensure safety.
  • Signature:
  1. func (set *StrSet) Walk(f func(item string) string) *StrSet
  • Example:
  1. func ExampleStrSet_Walk() {
  2. var (
  3. set gset.StrSet
  4. names = g.SliceStr{"user", "user_detail"}
  5. prefix = "gf_"
  6. )
  7. set.Add(names...)
  8. // Add prefix for given table names.
  9. set.Walk(func(item string) string {
  10. return prefix + item
  11. })
  12. fmt.Println(set.Slice())
  13. // May Output:
  14. // [gf_user gf_user_detail]
  15. }

MarshalJSON

  • Description: MarshalJSON implements the MarshalJSON interface of json.Marshal.
  • Signature:
  1. func (set *StrSet) MarshalJSON() ([]byte, error)
  • Example:
  1. func ExampleStrSet_MarshalJSON() {
  2. type Student struct {
  3. Id int
  4. Name string
  5. Scores *gset.StrSet
  6. }
  7. s := Student{
  8. Id: 1,
  9. Name: "john",
  10. Scores: gset.NewStrSetFrom([]string{"100", "99", "98"}, true),
  11. }
  12. b, _ := json.Marshal(s)
  13. fmt.Println(string(b))
  14. // May Output:
  15. // {"Id":1,"Name":"john","Scores":["100","99","98"]}
  16. }

UnmarshalJSON

  • Description: UnmarshalJSON implements the UnmarshalJSON interface of json.Unmarshal.
  • Signature:
  1. func (set *StrSet) UnmarshalJSON(b []byte) error
  • Example:
  1. func ExampleStrSet_UnmarshalJSON() {
  2. b := []byte(`{"Id":1,"Name":"john","Scores":["100","99","98"]}`)
  3. type Student struct {
  4. Id int
  5. Name string
  6. Scores *gset.StrSet
  7. }
  8. s := Student{}
  9. json.Unmarshal(b, &s)
  10. fmt.Println(s)
  11. // May Output:
  12. // {1 john "99","98","100"}
  13. }

UnmarshalValue

  • Description: UnmarshalValue implements the internal unified setting interface of the goframe framework. It initializes the current object with an interface{} type parameter, the usage logic of which is determined by the implementation method of this interface.
  • Signature:
  1. func (set *StrSet) UnmarshalValue(value interface{}) (err error)
  • Example:
  1. func ExampleStrSet_UnmarshalValue() {
  2. b := []byte(`{"Id":1,"Name":"john","Scores":["100","99","98"]}`)
  3. type Student struct {
  4. Id int
  5. Name string
  6. Scores *gset.StrSet
  7. }
  8. s := Student{}
  9. json.Unmarshal(b, &s)
  10. fmt.Println(s)
  11. // May Output:
  12. // {1 john "99","98","100"}
  13. }