Documentation
¶
Index ¶
- Variables
- type Set
- func (s Set) Add(elem interface{}) bool
- func (s Set) AddList(elems ...interface{}) int
- func (s Set) AddSet(set Set)
- func (s Set) Clear()
- func (s Set) Contains(elem interface{}) bool
- func (s Set) Copy() Set
- func (s Set) Dump(list interface{}) (err error)
- func (s Set) Equals(set interface{}) bool
- func (s Set) Foreach(f func(v interface{}) bool)
- func (s Set) IsEmpty() bool
- func (s Set) Remove(elem interface{}) bool
- func (s Set) RemoveSet(set Set)
- func (s Set) RetainSet(set Set)
- func (s Set) Size() int
- func (s Set) String() string
- func (s Set) ToList() []interface{}
- type SyncSetPair
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var (
ErrTypeCast = errors.New("failed to type cast")
)
Functions ¶
This section is empty.
Types ¶
type Set ¶
type Set map[interface{}]struct{}
Set 定义集合
Example ¶
package main import ( "fmt" "github.com/recallsong/go-utils/container/set" "github.com/recallsong/go-utils/container/slice/ints" "github.com/recallsong/go-utils/container/slice/strings" "github.com/recallsong/go-utils/conv" ) func main() { s := set.Set{} // 向集合添加元素列表 s.AddList(1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 9) // 向集合添加单个元素 s.Add(10) // 排序并打印 fmt.Println(ints.Ints(conv.Interfaces(s.ToList()).ToInts()).Sort()) // 创建保存Persion结构体的集合 type Persion struct { Name string } ps := set.Set{} ps.Add(Persion{Name: "Song"}) ps.Add(Persion{Name: "Hello"}) ps.Add(Persion{Name: "Song"}) var plist []Persion // 将集合元素输出为[]Persion ps.Dump(&plist) fmt.Println(len(plist)) // 集合运算 s1 := set.Set{} s1.AddList("A", "B", "C") s2 := set.Set{} s2.AddList("A", "D") // 并集 s3 := s1.Copy() s3.AddSet(s2) fmt.Println(strings.Strings(conv.Interfaces(s3.ToList()).ToStrings()).Sort()) // 差集 s3 = s1.Copy() s3.RemoveSet(s2) fmt.Println(strings.Strings(conv.Interfaces(s3.ToList()).ToStrings()).Sort()) // 交集 s3 = s1.Copy() s3.RetainSet(s2) fmt.Println(strings.Strings(conv.Interfaces(s3.ToList()).ToStrings()).Sort()) }
Output: [1 2 3 4 5 6 7 8 9 10] 2 [A B C D] [B C] [A]
Click to show internal directories.
Click to hide internal directories.