Documentation
¶
Overview ¶
Package gset provides kinds of concurrent-safe/unsafe sets.
Index ¶
- type IntSet
- func (set *IntSet) Add(item ...int)
- func (set *IntSet) AddIfNotExist(item int) bool
- func (set *IntSet) AddIfNotExistFunc(item int, f func() bool) bool
- func (set *IntSet) AddIfNotExistFuncLock(item int, f func() bool) bool
- func (set *IntSet) Clear()
- func (set *IntSet) Complement(full *IntSet) (newSet *IntSet)
- func (set *IntSet) Contains(item int) bool
- func (set *IntSet) DeepCopy() any
- func (set *IntSet) Diff(others ...*IntSet) (newSet *IntSet)
- func (set *IntSet) Equal(other *IntSet) bool
- func (set *IntSet) Intersect(others ...*IntSet) (newSet *IntSet)
- func (set *IntSet) IsSubsetOf(other *IntSet) bool
- func (set *IntSet) Iterator(f func(v int) bool)
- func (set *IntSet) Join(glue string) string
- func (set *IntSet) LockFunc(f func(m map[int]struct{}))
- func (set IntSet) MarshalJSON() ([]byte, error)
- func (set *IntSet) Merge(others ...*IntSet) *IntSet
- func (set *IntSet) Pop() int
- func (set *IntSet) Pops(size int) []int
- func (set *IntSet) RLockFunc(f func(m map[int]struct{}))
- func (set *IntSet) Remove(item int)
- func (set *IntSet) Size() int
- func (set *IntSet) Slice() []int
- func (set *IntSet) String() string
- func (set *IntSet) Sum() (sum int)
- func (set *IntSet) Union(others ...*IntSet) (newSet *IntSet)
- func (set *IntSet) UnmarshalJSON(b []byte) error
- func (set *IntSet) UnmarshalValue(value any) (err error)
- func (set *IntSet) Walk(f func(item int) int) *IntSet
- type Set
- func (set *Set) Add(items ...any)
- func (set *Set) AddIfNotExist(item any) bool
- func (set *Set) AddIfNotExistFunc(item any, f func() bool) bool
- func (set *Set) AddIfNotExistFuncLock(item any, f func() bool) bool
- func (set *Set) Clear()
- func (set *Set) Complement(full *Set) (newSet *Set)
- func (set *Set) Contains(item any) bool
- func (set *Set) DeepCopy() any
- func (set *Set) Diff(others ...*Set) (newSet *Set)
- func (set *Set) Equal(other *Set) bool
- func (set *Set) Intersect(others ...*Set) (newSet *Set)
- func (set *Set) IsSubsetOf(other *Set) bool
- func (set *Set) Iterator(f func(v any) bool)
- func (set *Set) Join(glue string) string
- func (set *Set) LockFunc(f func(m map[any]struct{}))
- func (set Set) MarshalJSON() ([]byte, error)
- func (set *Set) Merge(others ...*Set) *Set
- func (set *Set) Pop() any
- func (set *Set) Pops(size int) []any
- func (set *Set) RLockFunc(f func(m map[any]struct{}))
- func (set *Set) Remove(item any)
- func (set *Set) Size() int
- func (set *Set) Slice() []any
- func (set *Set) String() string
- func (set *Set) Sum() (sum int)
- func (set *Set) Union(others ...*Set) (newSet *Set)
- func (set *Set) UnmarshalJSON(b []byte) error
- func (set *Set) UnmarshalValue(value any) (err error)
- func (set *Set) Walk(f func(item any) any) *Set
- type StrSet
- func (set *StrSet) Add(item ...string)
- func (set *StrSet) AddIfNotExist(item string) bool
- func (set *StrSet) AddIfNotExistFunc(item string, f func() bool) bool
- func (set *StrSet) AddIfNotExistFuncLock(item string, f func() bool) bool
- func (set *StrSet) Clear()
- func (set *StrSet) Complement(full *StrSet) (newSet *StrSet)
- func (set *StrSet) Contains(item string) bool
- func (set *StrSet) ContainsI(item string) bool
- func (set *StrSet) DeepCopy() any
- func (set *StrSet) Diff(others ...*StrSet) (newSet *StrSet)
- func (set *StrSet) Equal(other *StrSet) bool
- func (set *StrSet) Intersect(others ...*StrSet) (newSet *StrSet)
- func (set *StrSet) IsSubsetOf(other *StrSet) bool
- func (set *StrSet) Iterator(f func(v string) bool)
- func (set *StrSet) Join(glue string) string
- func (set *StrSet) LockFunc(f func(m map[string]struct{}))
- func (set StrSet) MarshalJSON() ([]byte, error)
- func (set *StrSet) Merge(others ...*StrSet) *StrSet
- func (set *StrSet) Pop() string
- func (set *StrSet) Pops(size int) []string
- func (set *StrSet) RLockFunc(f func(m map[string]struct{}))
- func (set *StrSet) Remove(item string)
- func (set *StrSet) Size() int
- func (set *StrSet) Slice() []string
- func (set *StrSet) String() string
- func (set *StrSet) Sum() (sum int)
- func (set *StrSet) Union(others ...*StrSet) (newSet *StrSet)
- func (set *StrSet) UnmarshalJSON(b []byte) error
- func (set *StrSet) UnmarshalValue(value any) (err error)
- func (set *StrSet) Walk(f func(item string) string) *StrSet
- type TSet
- func (set *TSet[T]) Add(items ...T)
- func (set *TSet[T]) AddIfNotExist(item T) bool
- func (set *TSet[T]) AddIfNotExistFunc(item T, f func() bool) bool
- func (set *TSet[T]) AddIfNotExistFuncLock(item T, f func() bool) bool
- func (set *TSet[T]) Clear()
- func (set *TSet[T]) Complement(full *TSet[T]) (newSet *TSet[T])
- func (set *TSet[T]) Contains(item T) bool
- func (set *TSet[T]) DeepCopy() any
- func (set *TSet[T]) Diff(others ...*TSet[T]) (newSet *TSet[T])
- func (set *TSet[T]) Equal(other *TSet[T]) bool
- func (set *TSet[T]) Intersect(others ...*TSet[T]) (newSet *TSet[T])
- func (set *TSet[T]) IsSubsetOf(other *TSet[T]) bool
- func (set *TSet[T]) Iterator(f func(v T) bool)
- func (set *TSet[T]) Join(glue string) string
- func (set *TSet[T]) LockFunc(f func(m map[T]struct{}))
- func (set TSet[T]) MarshalJSON() ([]byte, error)
- func (set *TSet[T]) Merge(others ...*TSet[T]) *TSet[T]
- func (set *TSet[T]) Pop() (item T)
- func (set *TSet[T]) Pops(size int) []T
- func (set *TSet[T]) RLockFunc(f func(m map[T]struct{}))
- func (set *TSet[T]) Remove(item T)
- func (set *TSet[T]) Size() int
- func (set *TSet[T]) Slice() []T
- func (set *TSet[T]) String() string
- func (set *TSet[T]) Sum() (sum int)
- func (set *TSet[T]) Union(others ...*TSet[T]) (newSet *TSet[T])
- func (set *TSet[T]) UnmarshalJSON(b []byte) error
- func (set *TSet[T]) UnmarshalValue(value any) (err error)
- func (set *TSet[T]) Walk(f func(item T) T) *TSet[T]
Examples ¶
- IntSet.Add
- IntSet.AddIfNotExist
- IntSet.AddIfNotExistFunc
- IntSet.AddIfNotExistFuncLock
- IntSet.Clear
- IntSet.Complement
- IntSet.Contains
- IntSet.Diff
- IntSet.Equal
- IntSet.Intersect
- IntSet.IsSubsetOf
- IntSet.Iterator
- IntSet.Join
- IntSet.LockFunc
- IntSet.MarshalJSON
- IntSet.Merge
- IntSet.Pop
- IntSet.Pops
- IntSet.RLockFunc
- IntSet.Remove
- IntSet.Size
- IntSet.Slice
- IntSet.String
- IntSet.Sum
- IntSet.Union
- IntSet.UnmarshalJSON
- IntSet.UnmarshalValue
- IntSet.Walk
- NewFrom
- NewIntSet
- NewStrSet
- NewStrSetFrom
- Set (ContainsI)
- Set.AddIfNotExist
- Set.Complement
- Set.Contains
- Set.Diff
- Set.Intersect
- Set.IsSubsetOf
- Set.Join
- Set.Pop
- Set.Pops
- Set.Union
- StrSet.Add
- StrSet.AddIfNotExist
- StrSet.AddIfNotExistFunc
- StrSet.AddIfNotExistFuncLock
- StrSet.Clear
- StrSet.Complement
- StrSet.Contains
- StrSet.ContainsI
- StrSet.Diff
- StrSet.Equal
- StrSet.Intersect
- StrSet.IsSubsetOf
- StrSet.Iterator
- StrSet.Join
- StrSet.LockFunc
- StrSet.MarshalJSON
- StrSet.Merge
- StrSet.Pop
- StrSet.Pops
- StrSet.RLockFunc
- StrSet.Remove
- StrSet.Size
- StrSet.Slice
- StrSet.String
- StrSet.Sum
- StrSet.Union
- StrSet.UnmarshalJSON
- StrSet.UnmarshalValue
- StrSet.Walk
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type IntSet ¶
IntSet is consisted of int items.
func NewIntSet ¶
NewIntSet create and returns a new set, which contains un-repeated items. The parameter `safe` is used to specify whether using set in concurrent-safety, which is false in default.
Example ¶
New create and returns a new set, which contains un-repeated items. The parameter `safe` is used to specify whether using set in concurrent-safety, which is false in default.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
intSet := gset.NewIntSet()
intSet.Add([]int{1, 2, 3}...)
fmt.Println(intSet.Slice())
// May Output:
// [2 1 3]
}
func NewIntSetFrom ¶
NewIntSetFrom returns a new set from `items`.
func (*IntSet) Add ¶
Add adds one or multiple items to the set.
Example ¶
Add adds one or multiple items to the set.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
intSet := gset.NewIntSetFrom([]int{1, 2, 3})
intSet.Add(1)
fmt.Println(intSet.Slice())
fmt.Println(intSet.AddIfNotExist(1))
// May Output:
// [1 2 3]
// false
}
func (*IntSet) AddIfNotExist ¶
AddIfNotExist checks whether item exists in the set, it adds the item to set and returns true if it does not exists in the set, or else it does nothing and returns false.
Note that, if `item` is nil, it does nothing and returns false.
Example ¶
AddIfNotExist checks whether item exists in the set, it adds the item to set and returns true if it does not exists in the set, or else it does nothing and returns false.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
intSet := gset.NewIntSetFrom([]int{1, 2, 3})
intSet.Add(1)
fmt.Println(intSet.Slice())
fmt.Println(intSet.AddIfNotExist(1))
// May Output:
// [1 2 3]
// false
}
func (*IntSet) AddIfNotExistFunc ¶
AddIfNotExistFunc checks whether item exists in the set, it adds the item to set and returns true if it does not exists in the set and function `f` returns true, or else it does nothing and returns false.
Note that, the function `f` is executed without writing lock.
Example ¶
AddIfNotExistFunc checks whether item exists in the set, it adds the item to set and returns true if it does not exists in the set and function `f` returns true, or else it does nothing and returns false. Note that, the function `f` is executed without writing lock.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
intSet := gset.NewIntSetFrom([]int{1, 2, 3})
intSet.Add(1)
fmt.Println(intSet.Slice())
fmt.Println(intSet.AddIfNotExistFunc(5, func() bool {
return true
}))
// May Output:
// [1 2 3]
// true
}
func (*IntSet) AddIfNotExistFuncLock ¶
AddIfNotExistFuncLock checks whether item exists in the set, it adds the item to set and returns true if it does not exists in the set and function `f` returns true, or else it does nothing and returns false.
Note that, the function `f` is executed without writing lock.
Example ¶
AddIfNotExistFunc checks whether item exists in the set, it adds the item to set and returns true if it does not exists in the set and function `f` returns true, or else it does nothing and returns false. Note that, the function `f` is executed without writing lock.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
intSet := gset.NewIntSetFrom([]int{1, 2, 3})
intSet.Add(1)
fmt.Println(intSet.Slice())
fmt.Println(intSet.AddIfNotExistFuncLock(4, func() bool {
return true
}))
// May Output:
// [1 2 3]
// true
}
func (*IntSet) Clear ¶
func (set *IntSet) Clear()
Clear deletes all items of the set.
Example ¶
Clear deletes all items of the set.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
intSet := gset.NewIntSetFrom([]int{1, 2, 3})
fmt.Println(intSet.Size())
intSet.Clear()
fmt.Println(intSet.Size())
}
Output: 3 0
func (*IntSet) Complement ¶
Complement returns a new set which is the complement from `set` to `full`. Which means, all the items in `newSet` are in `full` and not in `set`.
It returns the difference between `full` and `set` if the given set `full` is not the full set of `set`.
Example ¶
Complement returns a new set which is the complement from `set` to `full`. Which means, all the items in `newSet` are in `full` and not in `set`. It returns the difference between `full` and `set` if the given set `full` is not the full set of `set`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
intSet := gset.NewIntSetFrom([]int{1, 2, 3, 4, 5})
s := gset.NewIntSetFrom([]int{1, 2, 3})
fmt.Println(s.Complement(intSet).Slice())
// May Output:
// [4 5]
}
func (*IntSet) Contains ¶
Contains checks whether the set contains `item`.
Example ¶
Contains checks whether the set contains `item`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
var set1 gset.IntSet
set1.Add(1, 4, 5, 6, 7)
fmt.Println(set1.Contains(1))
var set2 gset.IntSet
set2.Add(1, 4, 5, 6, 7)
fmt.Println(set2.Contains(8))
}
Output: true false
func (*IntSet) DeepCopy ¶ added in v2.1.0
DeepCopy implements interface for deep copy of current type.
func (*IntSet) Diff ¶
Diff returns a new set which is the difference set from `set` to `other`. Which means, all the items in `newSet` are in `set` but not in `other`.
Example ¶
Diff returns a new set which is the difference set from `set` to `other`. Which means, all the items in `newSet` are in `set` but not in `other`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewIntSetFrom([]int{1, 2, 3})
s2 := gset.NewIntSetFrom([]int{1, 2, 3, 4})
fmt.Println(s2.Diff(s1).Slice())
}
Output: [4]
func (*IntSet) Equal ¶
Equal checks whether the two sets equal.
Example ¶
Equal checks whether the two sets equal.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewIntSetFrom([]int{1, 2, 3})
s2 := gset.NewIntSetFrom([]int{1, 2, 3, 4})
fmt.Println(s2.Equal(s1))
s3 := gset.NewIntSetFrom([]int{1, 2, 3})
s4 := gset.NewIntSetFrom([]int{1, 2, 3})
fmt.Println(s3.Equal(s4))
}
Output: false true
func (*IntSet) Intersect ¶
Intersect returns a new set which is the intersection from `set` to `other`. Which means, all the items in `newSet` are in `set` and also in `other`.
Example ¶
Intersect returns a new set which is the intersection from `set` to `other`. Which means, all the items in `newSet` are in `set` and also in `other`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3}...)
var s2 gset.IntSet
s2.Add([]int{1, 2, 3, 4}...)
fmt.Println(s2.Intersect(s1).Slice())
// May Output:
// [1 2 3]
}
func (*IntSet) IsSubsetOf ¶
IsSubsetOf checks whether the current set is a sub-set of `other`.
Example ¶
IsSubsetOf checks whether the current set is a sub-set of `other`
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
var s2 gset.IntSet
s2.Add([]int{1, 2, 4}...)
fmt.Println(s2.IsSubsetOf(s1))
}
Output: true
func (*IntSet) Iterator ¶
Iterator iterates the set readonly with given callback function `f`, if `f` returns true then continue iterating; or false to stop.
Example ¶
Iterator iterates the set readonly with given callback function `f`, if `f` returns true then continue iterating; or false to stop.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
s1.Iterator(func(v int) bool {
fmt.Println("Iterator", v)
return true
})
// May Output:
// Iterator 2
// Iterator 3
// Iterator 1
// Iterator 4
}
func (*IntSet) Join ¶
Join joins items with a string `glue`.
Example ¶
Join joins items with a string `glue`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
fmt.Println(s1.Join(","))
// May Output:
// 3,4,1,2
}
func (*IntSet) LockFunc ¶
LockFunc locks writing with callback function `f`.
Example ¶
LockFunc locks writing with callback function `f`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2}...)
s1.LockFunc(func(m map[int]struct{}) {
m[3] = struct{}{}
})
fmt.Println(s1.Slice())
// May Output
// [2 3 1]
}
func (IntSet) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal.
Example ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal.
package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
type Student struct {
Id int
Name string
Scores *gset.IntSet
}
s := Student{
Id: 1,
Name: "john",
Scores: gset.NewIntSetFrom([]int{100, 99, 98}),
}
b, _ := json.Marshal(s)
fmt.Println(string(b))
// May Output:
// {"Id":1,"Name":"john","Scores":[100,99,98]}
}
func (*IntSet) Merge ¶
Merge adds items from `others` sets into `set`.
Example ¶
Merge adds items from `others` sets into `set`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
s2 := gset.NewIntSet()
fmt.Println(s1.Merge(s2).Slice())
// May Output:
// [1 2 3 4]
}
func (*IntSet) Pop ¶
Pop randomly pops an item from set.
Example ¶
Pops randomly pops an item from set.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
fmt.Println(s1.Pop())
// May Output:
// 1
}
func (*IntSet) Pops ¶
Pops randomly pops `size` items from set. It returns all items if size == -1.
Example ¶
Pops randomly pops `size` items from set. It returns all items if size == -1.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
for _, v := range s1.Pops(2) {
fmt.Println(v)
}
// May Output:
// 1
// 2
}
func (*IntSet) RLockFunc ¶
RLockFunc locks reading with callback function `f`.
Example ¶
RLockFunc locks reading with callback function `f`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
s1.RLockFunc(func(m map[int]struct{}) {
fmt.Println(m)
})
}
Output: map[1:{} 2:{} 3:{} 4:{}]
func (*IntSet) Remove ¶
Remove deletes `item` from set.
Example ¶
Remove deletes `item` from set.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
s1.Remove(1)
fmt.Println(s1.Slice())
// May Output:
// [3 4 2]
}
func (*IntSet) Size ¶
Size returns the size of the set.
Example ¶
Size returns the size of the set.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
fmt.Println(s1.Size())
}
Output: 4
func (*IntSet) Slice ¶
Slice returns the an of items of the set as slice.
Example ¶
Slice returns the an of items of the set as slice.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
fmt.Println(s1.Slice())
// May Output:
// [1, 2, 3, 4]
}
func (*IntSet) String ¶
String returns items as a string, which implements like json.Marshal does.
Example ¶
String returns items as a string, which implements like json.Marshal does.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
fmt.Println(s1.String())
// May Output:
// [1,2,3,4]
}
func (*IntSet) Sum ¶
Sum sums items. Note: The items should be converted to int type, or you'd get a result that you unexpected.
Example ¶
Sum sums items. Note: The items should be converted to int type, or you'd get a result that you unexpected.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
fmt.Println(s1.Sum())
}
Output: 10
func (*IntSet) Union ¶
Union returns a new set which is the union of `set` and `other`. Which means, all the items in `newSet` are in `set` or in `other`.
Example ¶
Union returns a new set which is the union of `set` and `other`. Which means, all the items in `newSet` are in `set` or in `other`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewIntSet()
s1.Add([]int{1, 2, 3, 4}...)
s2 := gset.NewIntSet()
s2.Add([]int{1, 2, 4}...)
fmt.Println(s1.Union(s2).Slice())
// May Output:
// [3 4 1 2]
}
func (*IntSet) UnmarshalJSON ¶
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
Example ¶
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
type Student struct {
Id int
Name string
Scores *gset.IntSet
}
s := Student{}
json.Unmarshal(b, &s)
fmt.Println(s)
// May Output:
// {1 john [100,99,98]}
}
func (*IntSet) UnmarshalValue ¶
UnmarshalValue is an interface implement which sets any type of value for set.
Example ¶
UnmarshalValue is an interface implement which sets any type of value for set.
package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
b := []byte(`{"Id":1,"Name":"john","Scores":100,99,98}`)
type Student struct {
Id int
Name string
Scores *gset.IntSet
}
s := Student{}
json.Unmarshal(b, &s)
fmt.Println(s)
// May Output:
// {1 john [100,99,98]}
}
func (*IntSet) Walk ¶
Walk applies a user supplied function `f` to every item of set.
Example ¶
Walk applies a user supplied function `f` to every item of set.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var (
set gset.IntSet
names = g.SliceInt{1, 0}
delta = 10
)
set.Add(names...)
// Add prefix for given table names.
set.Walk(func(item int) int {
return delta + item
})
fmt.Println(set.Slice())
// May Output:
// [12 60]
}
type Set ¶
Set is consisted of any items.
Example (ContainsI) ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
var set gset.StrSet
set.Add("a")
fmt.Println(set.Contains("a"))
fmt.Println(set.Contains("A"))
fmt.Println(set.ContainsI("A"))
}
Output: true false true
func New ¶
New create and returns a new set, which contains un-repeated items. The parameter `safe` is used to specify whether using set in concurrent-safety, which is false in default.
func NewFrom ¶
NewFrom returns a new set from `items`. Parameter `items` can be either a variable of any type, or a slice.
Example ¶
NewIntSetFrom returns a new set from `items`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
intSet := gset.NewIntSetFrom([]int{1, 2, 3})
fmt.Println(intSet.Slice())
// May Output:
// [2 1 3]
}
func (*Set) AddIfNotExist ¶
AddIfNotExist checks whether item exists in the set, it adds the item to set and returns true if it does not exists in the set, or else it does nothing and returns false.
Note that, if `item` is nil, it does nothing and returns false.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
var set gset.Set
fmt.Println(set.AddIfNotExist(1))
fmt.Println(set.AddIfNotExist(1))
fmt.Println(set.Slice())
}
Output: true false [1]
func (*Set) AddIfNotExistFunc ¶
AddIfNotExistFunc checks whether item exists in the set, it adds the item to set and returns true if it does not exist in the set and function `f` returns true, or else it does nothing and returns false.
Note that, if `item` is nil, it does nothing and returns false. The function `f` is executed without writing lock.
func (*Set) AddIfNotExistFuncLock ¶
AddIfNotExistFuncLock checks whether item exists in the set, it adds the item to set and returns true if it does not exists in the set and function `f` returns true, or else it does nothing and returns false.
Note that, if `item` is nil, it does nothing and returns false. The function `f` is executed within writing lock.
func (*Set) Complement ¶
Complement returns a new set which is the complement from `set` to `full`. Which means, all the items in `newSet` are in `full` and not in `set`.
It returns the difference between `full` and `set` if the given set `full` is not the full set of `set`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
s1 := gset.NewFrom(g.Slice{1, 2, 3})
s2 := gset.NewFrom(g.Slice{4, 5, 6})
s3 := gset.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7})
fmt.Println(s3.Intersect(s1).Slice())
fmt.Println(s3.Diff(s1).Slice())
fmt.Println(s1.Union(s2).Slice())
fmt.Println(s1.Complement(s3).Slice())
// May Output:
// [2 3 1]
// [5 6 7 4]
// [6 1 2 3 4 5]
// [4 5 6 7]
}
func (*Set) Contains ¶
Contains checks whether the set contains `item`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
var set gset.StrSet
set.Add("a")
fmt.Println(set.Contains("a"))
fmt.Println(set.Contains("A"))
fmt.Println(set.ContainsI("A"))
}
Output: true false true
func (*Set) Diff ¶
Diff returns a new set which is the difference set from `set` to `others`. Which means, all the items in `newSet` are in `set` but not in `others`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
s1 := gset.NewFrom(g.Slice{1, 2, 3})
s2 := gset.NewFrom(g.Slice{4, 5, 6})
s3 := gset.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7})
fmt.Println(s3.Intersect(s1).Slice())
fmt.Println(s3.Diff(s1).Slice())
fmt.Println(s1.Union(s2).Slice())
fmt.Println(s1.Complement(s3).Slice())
// May Output:
// [2 3 1]
// [5 6 7 4]
// [6 1 2 3 4 5]
// [4 5 6 7]
}
func (*Set) Intersect ¶
Intersect returns a new set which is the intersection from `set` to `others`. Which means, all the items in `newSet` are in `set` and also in `others`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
s1 := gset.NewFrom(g.Slice{1, 2, 3})
s2 := gset.NewFrom(g.Slice{4, 5, 6})
s3 := gset.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7})
fmt.Println(s3.Intersect(s1).Slice())
fmt.Println(s3.Diff(s1).Slice())
fmt.Println(s1.Union(s2).Slice())
fmt.Println(s1.Complement(s3).Slice())
// May Output:
// [2 3 1]
// [5 6 7 4]
// [6 1 2 3 4 5]
// [4 5 6 7]
}
func (*Set) IsSubsetOf ¶
IsSubsetOf checks whether the current set is a sub-set of `other`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
var s1, s2 gset.Set
s1.Add(g.Slice{1, 2, 3}...)
s2.Add(g.Slice{2, 3}...)
fmt.Println(s1.IsSubsetOf(&s2))
fmt.Println(s2.IsSubsetOf(&s1))
}
Output: false true
func (*Set) Iterator ¶
Iterator iterates the set readonly with given callback function `f`, if `f` returns true then continue iterating; or false to stop.
func (*Set) Join ¶
Join joins items with a string `glue`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
var set gset.Set
set.Add("a", "b", "c", "d")
fmt.Println(set.Join(","))
// May Output:
// a,b,c,d
}
func (Set) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (*Set) Pop ¶
Pop randomly pops an item from set.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
var set gset.Set
set.Add(1, 2, 3, 4)
fmt.Println(set.Pop())
fmt.Println(set.Pops(2))
fmt.Println(set.Size())
// May Output:
// 1
// [2 3]
// 1
}
func (*Set) Pops ¶
Pops randomly pops `size` items from set. It returns all items if size == -1.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
var set gset.Set
set.Add(1, 2, 3, 4)
fmt.Println(set.Pop())
fmt.Println(set.Pops(2))
fmt.Println(set.Size())
// May Output:
// 1
// [2 3]
// 1
}
func (*Set) Sum ¶
Sum sums items. Note: The items should be converted to int type, or you'd get a result that you unexpected.
func (*Set) Union ¶
Union returns a new set which is the union of `set` and `others`. Which means, all the items in `newSet` are in `set` or in `others`.
Example ¶
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
s1 := gset.NewFrom(g.Slice{1, 2, 3})
s2 := gset.NewFrom(g.Slice{4, 5, 6})
s3 := gset.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7})
fmt.Println(s3.Intersect(s1).Slice())
fmt.Println(s3.Diff(s1).Slice())
fmt.Println(s1.Union(s2).Slice())
fmt.Println(s1.Complement(s3).Slice())
// May Output:
// [2 3 1]
// [5 6 7 4]
// [6 1 2 3 4 5]
// [4 5 6 7]
}
func (*Set) UnmarshalJSON ¶
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (*Set) UnmarshalValue ¶
UnmarshalValue is an interface implement which sets any type of value for set.
type StrSet ¶
StrSet is consisted of string items.
func NewStrSet ¶
NewStrSet create and returns a new set, which contains un-repeated items. The parameter `safe` is used to specify whether using set in concurrent-safety, which is false in default.
Example ¶
NewStrSet create and returns a new set, which contains un-repeated items. The parameter `safe` is used to specify whether using set in concurrent-safety, which is false in default.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
strSet := gset.NewStrSet(true)
strSet.Add([]string{"str1", "str2", "str3"}...)
fmt.Println(strSet.Slice())
// May Output:
// [str3 str1 str2]
}
func NewStrSetFrom ¶
NewStrSetFrom returns a new set from `items`.
Example ¶
NewStrSetFrom returns a new set from `items`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
fmt.Println(strSet.Slice())
// May Output:
// [str1 str2 str3]
}
func (*StrSet) Add ¶
Add adds one or multiple items to the set.
Example ¶
Add adds one or multiple items to the set.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
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
}
func (*StrSet) AddIfNotExist ¶
AddIfNotExist checks whether item exists in the set, it adds the item to set and returns true if it does not exist in the set, or else it does nothing and returns false.
Example ¶
AddIfNotExist checks whether item exists in the set, it adds the item to set and returns true if it does not exist in the set, or else it does nothing and returns false.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
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
}
func (*StrSet) AddIfNotExistFunc ¶
AddIfNotExistFunc checks whether item exists in the set, it adds the item to set and returns true if it does not exists in the set and function `f` returns true, or else it does nothing and returns false.
Note that, the function `f` is executed without writing lock.
Example ¶
AddIfNotExistFunc checks whether item exists in the set, it adds the item to set and returns true if it does not exist in the set and function `f` returns true, or else it does nothing and returns false. Note that, the function `f` is executed without writing lock.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
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
}
func (*StrSet) AddIfNotExistFuncLock ¶
AddIfNotExistFuncLock checks whether item exists in the set, it adds the item to set and returns true if it does not exists in the set and function `f` returns true, or else it does nothing and returns false.
Note that, the function `f` is executed without writing lock.
Example ¶
AddIfNotExistFunc checks whether item exists in the set, it adds the item to set and returns true if it does not exist in the set and function `f` returns true, or else it does nothing and returns false. Note that, the function `f` is executed without writing lock.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
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
}
func (*StrSet) Clear ¶
func (set *StrSet) Clear()
Clear deletes all items of the set.
Example ¶
Clear deletes all items of the set.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
fmt.Println(strSet.Size())
strSet.Clear()
fmt.Println(strSet.Size())
}
Output: 3 0
func (*StrSet) Complement ¶
Complement returns a new set which is the complement from `set` to `full`. Which means, all the items in `newSet` are in `full` and not in `set`.
It returns the difference between `full` and `set` if the given set `full` is not the full set of `set`.
Example ¶
Complement returns a new set which is the complement from `set` to `full`. Which means, all the items in `newSet` are in `full` and not in `set`. It returns the difference between `full` and `set` if the given set `full` is not the full set of `set`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
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]
}
func (*StrSet) Contains ¶
Contains checks whether the set contains `item`.
Example ¶
Contains checks whether the set contains `item`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
var set gset.StrSet
set.Add("a")
fmt.Println(set.Contains("a"))
fmt.Println(set.Contains("A"))
}
Output: true false
func (*StrSet) ContainsI ¶
ContainsI checks whether a value exists in the set with case-insensitively. Note that it internally iterates the whole set to do the comparison with case-insensitively.
Example ¶
ContainsI checks whether a value exists in the set with case-insensitively. Note that it internally iterates the whole set to do the comparison with case-insensitively.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
var set gset.StrSet
set.Add("a")
fmt.Println(set.ContainsI("a"))
fmt.Println(set.ContainsI("A"))
}
Output: true true
func (*StrSet) DeepCopy ¶ added in v2.1.0
DeepCopy implements interface for deep copy of current type.
func (*StrSet) Diff ¶
Diff returns a new set which is the difference set from `set` to `other`. Which means, all the items in `newSet` are in `set` but not in `other`.
Example ¶
Diff returns a new set which is the difference set from `set` to `other`. Which means, all the items in `newSet` are in `set` but not in `other`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
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]
func (*StrSet) Equal ¶
Equal checks whether the two sets equal.
Example ¶
Equal checks whether the two sets equal.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
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
func (*StrSet) Intersect ¶
Intersect returns a new set which is the intersection from `set` to `other`. Which means, all the items in `newSet` are in `set` and also in `other`.
Example ¶
Intersect returns a new set which is the intersection from `set` to `other`. Which means, all the items in `newSet` are in `set` and also in `other`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
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]
}
func (*StrSet) IsSubsetOf ¶
IsSubsetOf checks whether the current set is a sub-set of `other`.
Example ¶
IsSubsetOf checks whether the current set is a sub-set of `other`
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
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
func (*StrSet) Iterator ¶
Iterator iterates the set readonly with given callback function `f`, if `f` returns true then continue iterating; or false to stop.
Example ¶
Iterator iterates the set readonly with given callback function `f`, if `f` returns true then continue iterating; or false to stop.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
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
}
func (*StrSet) Join ¶
Join joins items with a string `glue`.
Example ¶
Join joins items with a string `glue`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
fmt.Println(s1.Join(","))
// May Output:
// b,c,d,a
}
func (*StrSet) LockFunc ¶
LockFunc locks writing with callback function `f`.
Example ¶
LockFunc locks writing with callback function `f`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
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]
}
func (StrSet) MarshalJSON ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal.
Example ¶
MarshalJSON implements the interface MarshalJSON for json.Marshal.
package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
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"]}
}
func (*StrSet) Merge ¶
Merge adds items from `others` sets into `set`.
Example ¶
Merge adds items from `others` sets into `set`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
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]
}
func (*StrSet) Pop ¶
Pop randomly pops an item from set.
Example ¶
Pops randomly pops an item from set.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
fmt.Println(s1.Pop())
// May Output:
// a
}
func (*StrSet) Pops ¶
Pops randomly pops `size` items from set. It returns all items if size == -1.
Example ¶
Pops randomly pops `size` items from set. It returns all items if size == -1.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
for _, v := range s1.Pops(2) {
fmt.Println(v)
}
// May Output:
// a
// b
}
func (*StrSet) RLockFunc ¶
RLockFunc locks reading with callback function `f`.
Example ¶
RLockFunc locks reading with callback function `f`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
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:{}]
func (*StrSet) Remove ¶
Remove deletes `item` from set.
Example ¶
Remove deletes `item` from set.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
s1.Remove("a")
fmt.Println(s1.Slice())
// May Output:
// [b c d]
}
func (*StrSet) Size ¶
Size returns the size of the set.
Example ¶
Size returns the size of the set.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
fmt.Println(s1.Size())
}
Output: 4
func (*StrSet) Slice ¶
Slice returns the an of items of the set as slice.
Example ¶
Slice returns the an of items of the set as slice.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
fmt.Println(s1.Slice())
// May Output:
// [a,b,c,d]
}
func (*StrSet) String ¶
String returns items as a string, which implements like json.Marshal does.
Example ¶
String returns items as a string, which implements like json.Marshal does.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"a", "b", "c", "d"}...)
fmt.Println(s1.String())
// May Output:
// "a","b","c","d"
}
func (*StrSet) Sum ¶
Sum sums items. Note: The items should be converted to int type, or you'd get a result that you unexpected.
Example ¶
Sum sums items. Note: The items should be converted to int type, or you'd get a result that you unexpected.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
s1 := gset.NewStrSet(true)
s1.Add([]string{"1", "2", "3", "4"}...)
fmt.Println(s1.Sum())
}
Output: 10
func (*StrSet) Union ¶
Union returns a new set which is the union of `set` and `other`. Which means, all the items in `newSet` are in `set` or in `other`.
Example ¶
Union returns a new set which is the union of `set` and `other`. Which means, all the items in `newSet` are in `set` or in `other`.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
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]
}
func (*StrSet) UnmarshalJSON ¶
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
Example ¶
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
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"}
}
func (*StrSet) UnmarshalValue ¶
UnmarshalValue is an interface implement which sets any type of value for set.
Example ¶
UnmarshalValue is an interface implement which sets any type of value for set.
package main
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/v2/container/gset"
)
func main() {
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"}
}
func (*StrSet) Walk ¶
Walk applies a user supplied function `f` to every item of set.
Example ¶
Walk applies a user supplied function `f` to every item of set.
package main
import (
"fmt"
"github.com/gogf/gf/v2/container/gset"
"github.com/gogf/gf/v2/frame/g"
)
func main() {
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]
}
type TSet ¶ added in v2.9.6
type TSet[T comparable] struct { // contains filtered or unexported fields }
TSet[T] is consisted of any items.
func NewTSet ¶ added in v2.9.6
func NewTSet[T comparable](safe ...bool) *TSet[T]
NewTSet creates and returns a new set, which contains un-repeated items. Also see New.
func NewTSetFrom ¶ added in v2.9.6
func NewTSetFrom[T comparable](items []T, safe ...bool) *TSet[T]
NewTSetFrom returns a new set from `items`. `items` - A slice of type T.
func (*TSet[T]) Add ¶ added in v2.9.6
func (set *TSet[T]) Add(items ...T)
Add adds one or multiple items to the set.
func (*TSet[T]) AddIfNotExist ¶ added in v2.9.6
AddIfNotExist checks whether item exists in the set, it adds the item to set and returns true if it does not exists in the set, or else it does nothing and returns false.
Note that, if `item` is nil, it does nothing and returns false.
func (*TSet[T]) AddIfNotExistFunc ¶ added in v2.9.6
AddIfNotExistFunc checks whether item exists in the set, it adds the item to set and returns true if it does not exist in the set and function `f` returns true, or else it does nothing and returns false.
Note that, if `item` is nil, it does nothing and returns false. The function `f` is executed without writing lock.
func (*TSet[T]) AddIfNotExistFuncLock ¶ added in v2.9.6
AddIfNotExistFuncLock checks whether item exists in the set, it adds the item to set and returns true if it does not exists in the set and function `f` returns true, or else it does nothing and returns false.
Note that, if `item` is nil, it does nothing and returns false. The function `f` is executed within writing lock.
func (*TSet[T]) Clear ¶ added in v2.9.6
func (set *TSet[T]) Clear()
Clear deletes all items of the set.
func (*TSet[T]) Complement ¶ added in v2.9.6
Complement returns a new set which is the complement from `set` to `full`. Which means, all the items in `newSet` are in `full` and not in `set`.
It returns the difference between `full` and `set` if the given set `full` is not the full set of `set`.
func (*TSet[T]) DeepCopy ¶ added in v2.9.6
DeepCopy implements interface for deep copy of current type.
func (*TSet[T]) Diff ¶ added in v2.9.6
Diff returns a new set which is the difference set from `set` to `others`. Which means, all the items in `newSet` are in `set` but not in `others`.
func (*TSet[T]) Intersect ¶ added in v2.9.6
Intersect returns a new set which is the intersection from `set` to `others`. Which means, all the items in `newSet` are in `set` and also in `others`.
func (*TSet[T]) IsSubsetOf ¶ added in v2.9.6
IsSubsetOf checks whether the current set is a sub-set of `other`.
func (*TSet[T]) Iterator ¶ added in v2.9.6
Iterator iterates the set readonly with given callback function `f`, if `f` returns true then continue iterating; or false to stop.
func (*TSet[T]) LockFunc ¶ added in v2.9.6
func (set *TSet[T]) LockFunc(f func(m map[T]struct{}))
LockFunc locks writing with callback function `f`.
func (TSet[T]) MarshalJSON ¶ added in v2.9.6
MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (*TSet[T]) Pop ¶ added in v2.9.6
func (set *TSet[T]) Pop() (item T)
Pop randomly pops an item from set.
func (*TSet[T]) Pops ¶ added in v2.9.6
Pops randomly pops `size` items from set. It returns all items if size == -1.
func (*TSet[T]) RLockFunc ¶ added in v2.9.6
func (set *TSet[T]) RLockFunc(f func(m map[T]struct{}))
RLockFunc locks reading with callback function `f`.
func (*TSet[T]) Remove ¶ added in v2.9.6
func (set *TSet[T]) Remove(item T)
Remove deletes `item` from set.
func (*TSet[T]) Slice ¶ added in v2.9.6
func (set *TSet[T]) Slice() []T
Slice returns all items of the set as slice.
func (*TSet[T]) String ¶ added in v2.9.6
String returns items as a string, which implements like json.Marshal does.
func (*TSet[T]) Sum ¶ added in v2.9.6
Sum sums items. Note: The items should be converted to int type, or you'd get a result that you unexpected.
func (*TSet[T]) Union ¶ added in v2.9.6
Union returns a new set which is the union of `set` and `others`. Which means, all the items in `newSet` are in `set` or in `others`.
func (*TSet[T]) UnmarshalJSON ¶ added in v2.9.6
UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (*TSet[T]) UnmarshalValue ¶ added in v2.9.6
UnmarshalValue is an interface implement which sets any type of value for set.