日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)GoFrame教程:GoFramegset-基本使用

基本使用

package main

import (
	"github.com/GOgf/gf/v2/container/gset"
	"fmt"
)

func main() {
	// 創(chuàng)建一個并發(fā)安全的集合對象
	s := gset.New(true)

	// 添加數(shù)據(jù)項
	s.Add(1)

	// 批量添加數(shù)據(jù)項
	s.Add([]interface{}{1, 2, 3}...)

	// 集合數(shù)據(jù)項大小
	fmt.Println(s.Size())

	// 集合中是否存在指定數(shù)據(jù)項
	fmt.Println(s.Contains(2))

	// 返回數(shù)據(jù)項slice
	fmt.Println(s.Slice())

	// 刪除數(shù)據(jù)項
	s.Remove(3)

	// 遍歷數(shù)據(jù)項
	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é)果為:

創(chuàng)新互聯(lián)建站網(wǎng)站建設(shè)公司一直秉承“誠信做人,踏實做事”的原則,不欺瞞客戶,是我們最起碼的底線! 以服務(wù)為基礎(chǔ),以質(zhì)量求生存,以技術(shù)求發(fā)展,成交一個客戶多一個朋友!專注中小微企業(yè)官網(wǎng)定制,網(wǎng)站制作、網(wǎng)站設(shè)計,塑造企業(yè)網(wǎng)絡(luò)形象打造互聯(lián)網(wǎng)企業(yè)效應(yīng)。

3
true
[1 2 3]
Iterator: 1
Iterator: 2
[1 2]
map[1:{} 2:{} 4:{}]
0

交差并補集

我們可以使用以下方法實現(xiàn)交差并補集,并返回一個新的結(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?: 補集,(前提: set應(yīng)當為full的子集)屬于全集full不屬于集合set的元素組成的集合。如果給定的full集合不是set的全集時,返回full與set的差集.

通過集合方法我們可以發(fā)現(xiàn),交差并集方法支持多個集合參數(shù)進行計算。以下為簡化示例,只使用一個參數(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())
	// 補集
	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集合項出棧

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集合項串連

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*判斷性寫入

判斷性寫入是指當指定的數(shù)據(jù)項不存在時則寫入并且方法返回?true?,否則忽略吸入并且方法返回?false?。相關(guān)方法如下:

  • ?AddIfNotExist ?
  • ?AddIfNotExistFunc ?
  • ?AddIfNotExistFuncLock?

方法具體描述請查看接口文檔或源碼注釋。

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?模塊下的所有容器類型均實現(xià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]}


文章名稱:創(chuàng)新互聯(lián)GoFrame教程:GoFramegset-基本使用
分享網(wǎng)址:http://m.5511xx.com/article/djjegij.html