github.com/gogf/gf@v1.16.9/container/gset/gset_z_example_any_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with gm file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gset_test 8 9 import ( 10 "fmt" 11 "github.com/gogf/gf/container/gset" 12 "github.com/gogf/gf/frame/g" 13 ) 14 15 func ExampleSet_Intersect() { 16 s1 := gset.NewFrom(g.Slice{1, 2, 3}) 17 s2 := gset.NewFrom(g.Slice{4, 5, 6}) 18 s3 := gset.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7}) 19 20 fmt.Println(s3.Intersect(s1).Slice()) 21 fmt.Println(s3.Diff(s1).Slice()) 22 fmt.Println(s1.Union(s2).Slice()) 23 fmt.Println(s1.Complement(s3).Slice()) 24 25 // May Output: 26 // [2 3 1] 27 // [5 6 7 4] 28 // [6 1 2 3 4 5] 29 // [4 5 6 7] 30 } 31 32 func ExampleSet_Diff() { 33 s1 := gset.NewFrom(g.Slice{1, 2, 3}) 34 s2 := gset.NewFrom(g.Slice{4, 5, 6}) 35 s3 := gset.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7}) 36 37 fmt.Println(s3.Intersect(s1).Slice()) 38 fmt.Println(s3.Diff(s1).Slice()) 39 fmt.Println(s1.Union(s2).Slice()) 40 fmt.Println(s1.Complement(s3).Slice()) 41 42 // May Output: 43 // [2 3 1] 44 // [5 6 7 4] 45 // [6 1 2 3 4 5] 46 // [4 5 6 7] 47 } 48 49 func ExampleSet_Union() { 50 s1 := gset.NewFrom(g.Slice{1, 2, 3}) 51 s2 := gset.NewFrom(g.Slice{4, 5, 6}) 52 s3 := gset.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7}) 53 54 fmt.Println(s3.Intersect(s1).Slice()) 55 fmt.Println(s3.Diff(s1).Slice()) 56 fmt.Println(s1.Union(s2).Slice()) 57 fmt.Println(s1.Complement(s3).Slice()) 58 59 // May Output: 60 // [2 3 1] 61 // [5 6 7 4] 62 // [6 1 2 3 4 5] 63 // [4 5 6 7] 64 } 65 66 func ExampleSet_Complement() { 67 s1 := gset.NewFrom(g.Slice{1, 2, 3}) 68 s2 := gset.NewFrom(g.Slice{4, 5, 6}) 69 s3 := gset.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7}) 70 71 fmt.Println(s3.Intersect(s1).Slice()) 72 fmt.Println(s3.Diff(s1).Slice()) 73 fmt.Println(s1.Union(s2).Slice()) 74 fmt.Println(s1.Complement(s3).Slice()) 75 76 // May Output: 77 // [2 3 1] 78 // [5 6 7 4] 79 // [6 1 2 3 4 5] 80 // [4 5 6 7] 81 } 82 83 func ExampleSet_IsSubsetOf() { 84 var s1, s2 gset.Set 85 s1.Add(g.Slice{1, 2, 3}...) 86 s2.Add(g.Slice{2, 3}...) 87 fmt.Println(s1.IsSubsetOf(&s2)) 88 fmt.Println(s2.IsSubsetOf(&s1)) 89 90 // Output: 91 // false 92 // true 93 } 94 95 func ExampleSet_AddIfNotExist() { 96 var set gset.Set 97 fmt.Println(set.AddIfNotExist(1)) 98 fmt.Println(set.AddIfNotExist(1)) 99 fmt.Println(set.Slice()) 100 101 // Output: 102 // true 103 // false 104 // [1] 105 } 106 107 func ExampleSet_Pop() { 108 var set gset.Set 109 set.Add(1, 2, 3, 4) 110 fmt.Println(set.Pop()) 111 fmt.Println(set.Pops(2)) 112 fmt.Println(set.Size()) 113 114 // May Output: 115 // 1 116 // [2 3] 117 // 1 118 } 119 120 func ExampleSet_Pops() { 121 var set gset.Set 122 set.Add(1, 2, 3, 4) 123 fmt.Println(set.Pop()) 124 fmt.Println(set.Pops(2)) 125 fmt.Println(set.Size()) 126 127 // May Output: 128 // 1 129 // [2 3] 130 // 1 131 } 132 133 func ExampleSet_Join() { 134 var set gset.Set 135 set.Add("a", "b", "c", "d") 136 fmt.Println(set.Join(",")) 137 138 // May Output: 139 // a,b,c,d 140 } 141 142 func ExampleSet_Contains() { 143 var set gset.StrSet 144 set.Add("a") 145 fmt.Println(set.Contains("a")) 146 fmt.Println(set.Contains("A")) 147 fmt.Println(set.ContainsI("A")) 148 149 // Output: 150 // true 151 // false 152 // true 153 } 154 155 func ExampleSet_ContainsI() { 156 var set gset.StrSet 157 set.Add("a") 158 fmt.Println(set.Contains("a")) 159 fmt.Println(set.Contains("A")) 160 fmt.Println(set.ContainsI("A")) 161 162 // Output: 163 // true 164 // false 165 // true 166 }