W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
package main
import (
"github.com/gogf/gf/v2/container/gset"
"fmt"
)
func main() {
// 創(chuàng)建一個(gè)并發(fā)安全的集合對(duì)象
s := gset.New(true)
// 添加數(shù)據(jù)項(xiàng)
s.Add(1)
// 批量添加數(shù)據(jù)項(xiàng)
s.Add([]interface{}{1, 2, 3}...)
// 集合數(shù)據(jù)項(xiàng)大小
fmt.Println(s.Size())
// 集合中是否存在指定數(shù)據(jù)項(xiàng)
fmt.Println(s.Contains(2))
// 返回?cái)?shù)據(jù)項(xiàng)slice
fmt.Println(s.Slice())
// 刪除數(shù)據(jù)項(xiàng)
s.Remove(3)
// 遍歷數(shù)據(jù)項(xiàng)
s.Iterator(func(v interface{}) bool {
fmt.Println("Iterator:", v)
return true
})
// 將集合轉(zhuǎn)換為字符串
fmt.Println(s.String())
// 并發(fā)安全寫鎖操作
s.LockFunc(func(m map[interface{}]struct{}) {
m[4] = struct{}{}
})
// 并發(fā)安全讀鎖操作
s.RLockFunc(func(m map[interface{}]struct{}) {
fmt.Println(m)
})
// 清空集合
s.Clear()
fmt.Println(s.Size())
}
執(zhí)行后,輸出結(jié)果為:
3
true
[1 2 3]
Iterator: 1
Iterator: 2
[1 2]
map[1:{} 2:{} 4:{}]
0
我們可以使用以下方法實(shí)現(xiàn)交差并補(bǔ)集,并返回一個(gè)新的結(jié)果集合
func (set *Set) Intersect(others ...*Set) (newSet *Set)
func (set *Set) Diff(others ...*Set) (newSet *Set)
func (set *Set) Union(others ...*Set) (newSet *Set)
func (set *Set) Complement(full *Set) (newSet *Set)
Intersect
?: 交集,屬于set且屬于others的元素為元素的集合。
Diff
?: 差集,屬于set且不屬于others的元素為元素的集合。
Union
?: 并集,屬于set或?qū)儆趏thers的元素為元素的集合。
Complement
?: 補(bǔ)集,(前提: set應(yīng)當(dāng)為full的子集)屬于全集full不屬于集合set的元素組成的集合。如果給定的full集合不是set的全集時(shí),返回full與set的差集.通過集合方法我們可以發(fā)現(xiàn),交差并集方法支持多個(gè)集合參數(shù)進(jìn)行計(jì)算。以下為簡(jiǎn)化示例,只使用一個(gè)參數(shù)集合。
package main
import (
"fmt"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/container/gset"
)
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())
// 補(bǔ)集
fmt.Println(s1.Complement(s3).Slice())
}
執(zhí)行后,輸出結(jié)果為:
[1 2 3]
[4 5 6 7]
[1 2 3 4 5 6]
[7 4 5 6]
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
}
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
}
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
}
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
}
判斷性寫入是指當(dāng)指定的數(shù)據(jù)項(xiàng)不存在時(shí)則寫入并且方法返回?true
?,否則忽略吸入并且方法返回?false
?。相關(guān)方法如下:
AddIfNotExist
?AddIfNotExistFunc
?AddIfNotExistFuncLock
?方法具體描述請(qǐng)查看接口文檔或源碼注釋。
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]
}
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]
}
?gset
?模塊下的所有容器類型均實(shí)現(xiàn)了標(biāo)準(zhǔn)庫?json
?數(shù)據(jù)格式的序列化/反序列化接口。
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))
}
執(zhí)行后,終端輸出:
{"Id":1,"Name":"john","Scores":[100,99,98]}
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)
}
執(zhí)行后,輸出結(jié)果:
{1 john [100,99,98]}
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: