以下常用方法列表,文档更新可能滞后于代码新特性,更多的方法及示例请参考代码文档:https://pkg.GO.dev/github.com/gogf/gf/v2/container/gset

成都创新互联坚持“要么做到,要么别承诺”的工作理念,服务领域包括:网站设计、成都做网站、企业官网、英文网站、手机端网站、网站推广等服务,满足客户于互联网时代的平南网站设计、移动媒体设计的需求,帮助企业找到有效的互联网解决方案。努力成为您成熟可靠的网络建设合作伙伴!
这里以StrSet类型介绍方法使用,其他集合类型的方法与此类似,不再赘述。
NewStrSet创建并返回一个空的集合,其中包含没有重复字符串的数据。参数safe用于指定是否在并发安全中使用,默认情况下是false。 func NewStrSet(safe ...bool) *StrSet
func ExampleNewStrSet() {
	strSet := gset.NewStrSet(true)
	strSet.Add([]string{"str1", "str2", "str3"}...)
	fmt.Println(strSet.Slice())
	// May Output:
	// [str3 str1 str2]
}
NewStrSetFrom通过给定的数组创建集合集合。参数safe用于指定是否在并发安全中使用,默认情况下是false。 func NewStrSetFrom(items []string, safe ...bool) *StrSet
func ExampleNewStrSetFrom() {
	strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
	fmt.Println(strSet.Slice())
	// May Output:
	// [str1 str2 str3]
}
Add添加一个或多个元素项到集合中。 func (set *StrSet) Add(item ...string)
func ExampleStrSet_Add() {
	strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
	strSet.Add("str")
	fmt.Println(strSet.Slice())
	fmt.Println(strSet.AddIfNotExist("str"))
	// Mya Output:
	// [str str1 str2 str3]
	// false
}
Addifnotexist检查集合中是否存在指定元素项item,如果不存在则将item添加到集合中并返回true,否则什么也不做并返回false。 func (set *StrSet) AddIfNotExist(item string) bool
func ExampleStrSet_AddIfNotExist() {
	strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
	strSet.Add("str")
	fmt.Println(strSet.Slice())
	fmt.Println(strSet.AddIfNotExist("str"))
	// Mya Output:
	// [str str1 str2 str3]
	// false
}
AddIfNotExistFunc检查集合中存在指定元素项item,如果不存在并且方法f返回true时,则将item设置到集合中并返回true,否则什么也不做并返回false。 func (set *StrSet) AddIfNotExistFunc(item string, f func() bool) bool
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与AddIfNotExistFunc类似,不过当多个goroutine同时调用AddifnotExistFuncLock方法时,内部使用并发安全锁机制保证同时只能一个goroutine执行。该方法只有在创建集合时的safe参数设置true时有效,否则表现和方法一致AddIfNotExistFunc。 func (set *StrSet) AddIfNotExistFuncLock(item string, f func() bool) bool
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 删除集合的所有元素项。 func (set *StrSet) Clear()
func ExampleStrSet_Clear() {
	strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
	fmt.Println(strSet.Size())
	strSet.Clear()
	fmt.Println(strSet.Size())
	// Output:
	// 3
	// 0
}
Intrersect 执行集合set与others的交集,并返回一个新的集合newSet,在newSet中的元素项同时存在于集合set与others中。 func (set *StrSet) Intersect(others ...*StrSet) (newSet *StrSet)
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 执行集合set与others差集操作,并返回一个新的集合newSet。在newSet中的元素项存在于set但不存在于集合others。注意,参数others可以指定多个集合参数。 func (set *StrSet) Diff(others ...*StrSet) (newSet *StrSet)
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执行集合set与others的并集操作,并返回一个新的集合newSet。 func (set *StrSet) Union(others ...*StrSet) (newSet *StrSet)
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 执行set与full的补集操作,并返回一个新集合newSet。 func (set *StrSet) Complement(full *StrSet) (newSet *StrSet)
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 包含检查集是否包含item。 func (set *StrSet) Contains(item string) bool
func ExampleStrSet_Contains() {
	var set gset.StrSet
	set.Add("a")
	fmt.Println(set.Contains("a"))
	fmt.Println(set.Contains("A"))
	// Output:
	// true
	// false
}
ContainsI方法类似于Contains,只是它不区分大小写比较大小。 func (set *StrSet) ContainsI(item string) bool
func ExampleStrSet_ContainsI() {
	var set gset.StrSet
	set.Add("a")
	fmt.Println(set.ContainsI("a"))
	fmt.Println(set.ContainsI("A"))
	// Output:
	// true
	// true
}
Equal检查两个集合是否完全相等(包括大小以及元素项)。 func (set *StrSet) Equal(other *StrSet) bool
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
}
IsSumSetOf检查当前集合set是否是指定集合other的子集。 func (set *StrSet) IsSubsetOf(other *StrSet) bool
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 迭代器通过给定的回调函数f随机遍历当前集合set,如果方法f返回true,则继续遍历,否则停止。 func (set *StrSet) Iterator(f func(v string) bool)
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 将集合中的元素项通过字符串glue拼接成新的字符串返回。 func (set *StrSet) Join(glue string) string
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 仅在并发安全场景下有用,该方法通过写锁锁定集合set,并执行回调方法f。 func (set *StrSet) LockFunc(f func(m map[string]struct{}))
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 仅在并发安全场景下有用,该方法通过读锁锁定集合set,并执行回调方法f。 func (set *StrSet) RLockFunc(f func(m map[string]struct{}))
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将集合others中的所有元素项合并到set中。 func (set *StrSet) Merge(others ...*StrSet) *StrSet
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随机从集合中取出一个元素项。 func (set *StrSet) Pop() string
func ExampleStrSet_Pop() {
	s1 := gset.NewStrSet(true)
	s1.Add([]string{"a", "b", "c", "d"}...)
	fmt.Println(s1.Pop())
	// May Output:
	// a
}
Pops 从集合中随机弹出size个元素项。如果size == -1,则返回所有元素项。 func (set *StrSet) Pops(size int) []string
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从集合中删除指定的元素项item。 func (set *StrSet) Remove(item string)
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返回集合的大小。 func (set *StrSet) Size() int
func ExampleStrSet_Size() {
	s1 := gset.NewStrSet(true)
	s1.Add([]string{"a", "b", "c", "d"}...)
	fmt.Println(s1.Size())
	// Output:
	// 4
}
Slice将集合中的元素项以slice的形式返回。 func (set *StrSet) Slice() []string
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将集合按照字符串返回。 func (set *StrSet) String() string
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将集合中的元素项执行求和,注意:只有元素项为数字时才有效,否则您将得到一个意想不到的结果。 func (set *StrSet) Sum() (sum int)
func ExampleStrSet_Sum() {
	s1 := gset.NewStrSet(true)
	s1.Add([]string{"1", "2", "3", "4"}...)
	fmt.Println(s1.Sum())
	// Output:
	// 10
}
Walk按照用户给定的回调方法f遍历当前集合,并将f的返回结果重新设置当前集合。注意,在并发安全场景中,该方法内部使用写锁来保证并发安全性。 func (set *StrSet) Walk(f func(item string) string) *StrSet
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实现了json.Marshal的MarshalJSON接口。 func (set *StrSet) MarshalJSON() ([]byte, error)
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实现了json.Unmarshal中的UnmarshalJSON接口。 func (set *StrSet) UnmarshalJSON(b []byte) error
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"}
}
UnfarshalValue实现GoFrame框架内部统一的设置接口,它通过一个interface{}类型的参数初始化当前对象,至于interface{}参数的使用逻辑由该接口实现方法决定。 func (set *StrSet) UnmarshalValue(value interface{}) (err error)
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"}
}            
                标题名称:创新互联GoFrame教程:GoFramegset-方法介绍
                
                当前路径:http://www.csdahua.cn/qtweb/news22/15872.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网