GoFrame gset-基本使用

2022-04-08 11:11 更新

基本使用

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

交差并補(bǔ)集

我們可以使用以下方法實(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)
  1. ?Intersect?: 交集,屬于set且屬于others的元素為元素的集合。
  2. ?Diff?: 差集,屬于set且不屬于others的元素為元素的集合。
  3. ?Union?: 并集,屬于set或?qū)儆趏thers的元素為元素的集合。
  4. ?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]

Contains/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
}

Pop/Pops集合項(xiàng)出棧

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
}

Join集合項(xiàng)串連

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
}

IsSubsetOf子集判斷

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
}

AddIfNotExist*判斷性寫入

判斷性寫入是指當(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]
}

Walk遍歷修改

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]
}

JSON序列化/反序列

?gset?模塊下的所有容器類型均實(shí)現(xiàn)了標(biāo)準(zhǔn)庫?json?數(shù)據(jù)格式的序列化/反序列化接口。

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))
}

執(zhí)行后,終端輸出:

{"Id":1,"Name":"john","Scores":[100,99,98]}

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)
}

執(zhí)行后,輸出結(jié)果:

{1 john [100,99,98]}


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)