Basic Usage

  1. package main
  2. import (
  3. "github.com/gogf/gf/v2/container/gset"
  4. "fmt"
  5. )
  6. func main() {
  7. // Create a concurrent safe set object
  8. s := gset.New(true)
  9. // Add an item
  10. s.Add(1)
  11. // Add items in batch
  12. s.Add([]interface{}{1, 2, 3}...)
  13. // Size of the set
  14. fmt.Println(s.Size())
  15. // Check if an item exists in the set
  16. fmt.Println(s.Contains(2))
  17. // Return items as a slice
  18. fmt.Println(s.Slice())
  19. // Remove an item
  20. s.Remove(3)
  21. // Iterate through items
  22. s.Iterator(func(v interface{}) bool {
  23. fmt.Println("Iterator:", v)
  24. return true
  25. })
  26. // Convert the set to a string
  27. fmt.Println(s.String())
  28. // Concurrent safe write lock operation
  29. s.LockFunc(func(m map[interface{}]struct{}) {
  30. m[4] = struct{}{}
  31. })
  32. // Concurrent safe read lock operation
  33. s.RLockFunc(func(m map[interface{}]struct{}) {
  34. fmt.Println(m)
  35. })
  36. // Clear the set
  37. s.Clear()
  38. fmt.Println(s.Size())
  39. }

After execution, the output is:

  1. true
  2. [1 2 3]
  3. Iterator: 1
  4. Iterator: 2
  5. [1 2]
  6. map[1:{} 2:{} 4:{}]
  7. 0

Intersection, Difference, Union, Complement

We can achieve intersection, difference, union, and complement using the following methods, and return a new result set,

  1. func (set *Set) Intersect(others ...*Set) (newSet *Set)
  2. func (set *Set) Diff(others ...*Set) (newSet *Set)
  3. func (set *Set) Union(others ...*Set) (newSet *Set)
  4. func (set *Set) Complement(full *Set) (newSet *Set)
  1. Intersect: Intersection, a set of elements that belong to both set and others.
  2. Diff: Difference, a set of elements that belong to set and not others.
  3. Union: Union, a set of elements that belong to either set or others.
  4. Complement: Complement, (precondition: set should be a subset of full) a set of elements that belong to the universal set full but not to set. If the given full set is not the universal set of set, it returns the difference between full and set.

Through set methods, we can see that intersection, difference, and union methods support multiple set parameters for computation. The following is a simplified example, using only one parameter set.

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/container/gset"
  6. )
  7. func main() {
  8. s1 := gset.NewFrom(g.Slice{1, 2, 3})
  9. s2 := gset.NewFrom(g.Slice{4, 5, 6})
  10. s3 := gset.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7})
  11. // Intersection
  12. fmt.Println(s3.Intersect(s1).Slice())
  13. // Difference
  14. fmt.Println(s3.Diff(s1).Slice())
  15. // Union
  16. fmt.Println(s1.Union(s2).Slice())
  17. // Complement
  18. fmt.Println(s1.Complement(s3).Slice())
  19. }

After execution, the output is:

  1. [1 2 3]
  2. [4 5 6 7]
  3. [1 2 3 4 5 6]
  4. [7 4 5 6]

Contains/ContainsI Inclusion Judgment

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/gset"
  5. )
  6. func main() {
  7. var set gset.StrSet
  8. set.Add("a")
  9. fmt.Println(set.Contains("a"))
  10. fmt.Println(set.Contains("A"))
  11. fmt.Println(set.ContainsI("A"))
  12. // Output:
  13. // true
  14. // false
  15. // true
  16. }

Pop/Pops Set Item Popping

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/gset"
  5. )
  6. func main() {
  7. var set gset.Set
  8. set.Add(1, 2, 3, 4)
  9. fmt.Println(set.Pop())
  10. fmt.Println(set.Pops(2))
  11. fmt.Println(set.Size())
  12. // May Output:
  13. // 1
  14. // [2 3]
  15. // 1
  16. }

Join Set Item Concatenation

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/gset"
  5. )
  6. func main() {
  7. var set gset.Set
  8. set.Add("a", "b", "c", "d")
  9. fmt.Println(set.Join(","))
  10. // May Output:
  11. // a,b,c,d
  12. }

IsSubsetOf Subset Judgment

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/gset"
  5. "github.com/gogf/gf/v2/frame/g"
  6. )
  7. func main() {
  8. var s1, s2 gset.Set
  9. s1.Add(g.Slice{1, 2, 3}...)
  10. s2.Add(g.Slice{2, 3}...)
  11. fmt.Println(s1.IsSubsetOf(&s2))
  12. fmt.Println(s2.IsSubsetOf(&s1))
  13. // Output:
  14. // false
  15. // true
  16. }

AddIfNotExist* Conditional Writing

Conditional writing means writing if the specified item does not exist, and returning true. Otherwise, skipping writing and returning false. Related methods include:

  • AddIfNotExist
  • AddIfNotExistFunc
  • AddIfNotExistFuncLock

For detailed descriptions of the methods, please refer to the interface documentation or source code comments.

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/gset"
  5. )
  6. func main() {
  7. var set gset.Set
  8. fmt.Println(set.AddIfNotExist(1))
  9. fmt.Println(set.AddIfNotExist(1))
  10. fmt.Println(set.Slice())
  11. // Output:
  12. // true
  13. // false
  14. // [1]
  15. }

Walk Traverse and Modify

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/gset"
  5. "github.com/gogf/gf/v2/frame/g"
  6. )
  7. func main() {
  8. var (
  9. set gset.StrSet
  10. names = g.SliceStr{"user", "user_detail"}
  11. prefix = "gf_"
  12. )
  13. set.Add(names...)
  14. // Add prefix for given table names.
  15. set.Walk(func(item string) string {
  16. return prefix + item
  17. })
  18. fmt.Println(set.Slice())
  19. // May Output:
  20. // [gf_user gf_user_detail]
  21. }

JSON Serialization/Deserialization

All container types under the gset module implement interfaces for standard library json data format serialization/deserialization.

  1. Marshal
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gogf/gf/v2/container/gset"
  6. )
  7. func main() {
  8. type Student struct {
  9. Id int
  10. Name string
  11. Scores *gset.IntSet
  12. }
  13. s := Student{
  14. Id: 1,
  15. Name: "john",
  16. Scores: gset.NewIntSetFrom([]int{100, 99, 98}),
  17. }
  18. b, _ := json.Marshal(s)
  19. fmt.Println(string(b))
  20. }

After execution, the terminal output is:

  1. {"Id":1,"Name":"john","Scores":[100,99,98]}
  1. Unmarshal
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gogf/gf/v2/container/gset"
  6. )
  7. func main() {
  8. b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
  9. type Student struct {
  10. Id int
  11. Name string
  12. Scores *gset.IntSet
  13. }
  14. s := Student{}
  15. json.Unmarshal(b, &s)
  16. fmt.Println(s)
  17. }

After execution, the output is:

  1. {1 john [100,99,98]}