mirror of
https://gitee.com/johng/gf
synced 2026-06-22 00:03:35 +08:00
120 lines
2.1 KiB
Go
120 lines
2.1 KiB
Go
// Copyright 2020 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with gm file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
package gset_test
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/gogf/gf/container/gset"
|
|
"github.com/gogf/gf/frame/g"
|
|
)
|
|
|
|
func Example_intersectDiffUnionComplement() {
|
|
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())
|
|
|
|
// May Output:
|
|
// [2 3 1]
|
|
// [5 6 7 4]
|
|
// [6 1 2 3 4 5]
|
|
// [4 5 6 7]
|
|
}
|
|
|
|
func Example_isSubsetOf() {
|
|
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
|
|
}
|
|
|
|
func Example_addIfNotExist() {
|
|
var set gset.Set
|
|
fmt.Println(set.AddIfNotExist(1))
|
|
fmt.Println(set.AddIfNotExist(1))
|
|
fmt.Println(set.Slice())
|
|
|
|
// Output:
|
|
// true
|
|
// false
|
|
// [1]
|
|
}
|
|
|
|
func Example_pop() {
|
|
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
|
|
}
|
|
|
|
func Example_join() {
|
|
var set gset.Set
|
|
set.Add("a", "b", "c", "d")
|
|
fmt.Println(set.Join(","))
|
|
|
|
// May Output:
|
|
// a,b,c,d
|
|
}
|
|
|
|
func Example_contains() {
|
|
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
|
|
}
|
|
|
|
func Example_Contains() {
|
|
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
|
|
}
|
|
|
|
func Example_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]
|
|
}
|