github.com/gogf/gf/v2@v2.7.4/container/gset/gset_z_example_str_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  	"encoding/json"
    11  	"fmt"
    12  
    13  	"github.com/gogf/gf/v2/container/gset"
    14  	"github.com/gogf/gf/v2/frame/g"
    15  )
    16  
    17  // NewStrSet create and returns a new set, which contains un-repeated items.
    18  // The parameter `safe` is used to specify whether using set in concurrent-safety,
    19  // which is false in default.
    20  func ExampleNewStrSet() {
    21  	strSet := gset.NewStrSet(true)
    22  	strSet.Add([]string{"str1", "str2", "str3"}...)
    23  	fmt.Println(strSet.Slice())
    24  
    25  	// May Output:
    26  	// [str3 str1 str2]
    27  }
    28  
    29  // NewStrSetFrom returns a new set from `items`.
    30  func ExampleNewStrSetFrom() {
    31  	strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
    32  	fmt.Println(strSet.Slice())
    33  
    34  	// May Output:
    35  	// [str1 str2 str3]
    36  }
    37  
    38  // Add adds one or multiple items to the set.
    39  func ExampleStrSet_Add() {
    40  	strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
    41  	strSet.Add("str")
    42  	fmt.Println(strSet.Slice())
    43  	fmt.Println(strSet.AddIfNotExist("str"))
    44  
    45  	// May Output:
    46  	// [str str1 str2 str3]
    47  	// false
    48  }
    49  
    50  // AddIfNotExist checks whether item exists in the set,
    51  // it adds the item to set and returns true if it does not exist in the set,
    52  // or else it does nothing and returns false.
    53  func ExampleStrSet_AddIfNotExist() {
    54  	strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
    55  	strSet.Add("str")
    56  	fmt.Println(strSet.Slice())
    57  	fmt.Println(strSet.AddIfNotExist("str"))
    58  
    59  	// May Output:
    60  	// [str str1 str2 str3]
    61  	// false
    62  }
    63  
    64  // AddIfNotExistFunc checks whether item exists in the set,
    65  // it adds the item to set and returns true if it does not exist in the set and function `f` returns true,
    66  // or else it does nothing and returns false.
    67  // Note that, the function `f` is executed without writing lock.
    68  func ExampleStrSet_AddIfNotExistFunc() {
    69  	strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
    70  	strSet.Add("str")
    71  	fmt.Println(strSet.Slice())
    72  	fmt.Println(strSet.AddIfNotExistFunc("str5", func() bool {
    73  		return true
    74  	}))
    75  
    76  	// May Output:
    77  	// [str1 str2 str3 str]
    78  	// true
    79  }
    80  
    81  // AddIfNotExistFunc checks whether item exists in the set,
    82  // it adds the item to set and returns true if it does not exist in the set and function `f` returns true,
    83  // or else it does nothing and returns false.
    84  // Note that, the function `f` is executed without writing lock.
    85  func ExampleStrSet_AddIfNotExistFuncLock() {
    86  	strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
    87  	strSet.Add("str")
    88  	fmt.Println(strSet.Slice())
    89  	fmt.Println(strSet.AddIfNotExistFuncLock("str4", func() bool {
    90  		return true
    91  	}))
    92  
    93  	// May Output:
    94  	// [str1 str2 str3 str]
    95  	// true
    96  }
    97  
    98  // Clear deletes all items of the set.
    99  func ExampleStrSet_Clear() {
   100  	strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
   101  	fmt.Println(strSet.Size())
   102  	strSet.Clear()
   103  	fmt.Println(strSet.Size())
   104  
   105  	// Output:
   106  	// 3
   107  	// 0
   108  }
   109  
   110  // Complement returns a new set which is the complement from `set` to `full`.
   111  // Which means, all the items in `newSet` are in `full` and not in `set`.
   112  // It returns the difference between `full` and `set` if the given set `full` is not the full set of `set`.
   113  func ExampleStrSet_Complement() {
   114  	strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3", "str4", "str5"}, true)
   115  	s := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true)
   116  	fmt.Println(s.Complement(strSet).Slice())
   117  
   118  	// May Output:
   119  	// [str4 str5]
   120  }
   121  
   122  // Contains checks whether the set contains `item`.
   123  func ExampleStrSet_Contains() {
   124  	var set gset.StrSet
   125  	set.Add("a")
   126  	fmt.Println(set.Contains("a"))
   127  	fmt.Println(set.Contains("A"))
   128  
   129  	// Output:
   130  	// true
   131  	// false
   132  }
   133  
   134  // ContainsI checks whether a value exists in the set with case-insensitively.
   135  // Note that it internally iterates the whole set to do the comparison with case-insensitively.
   136  func ExampleStrSet_ContainsI() {
   137  	var set gset.StrSet
   138  	set.Add("a")
   139  	fmt.Println(set.ContainsI("a"))
   140  	fmt.Println(set.ContainsI("A"))
   141  
   142  	// Output:
   143  	// true
   144  	// true
   145  }
   146  
   147  // Diff returns a new set which is the difference set from `set` to `other`.
   148  // Which means, all the items in `newSet` are in `set` but not in `other`.
   149  func ExampleStrSet_Diff() {
   150  	s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
   151  	s2 := gset.NewStrSetFrom([]string{"a", "b", "c", "d"}, true)
   152  	fmt.Println(s2.Diff(s1).Slice())
   153  
   154  	// Output:
   155  	// [d]
   156  }
   157  
   158  // Equal checks whether the two sets equal.
   159  func ExampleStrSet_Equal() {
   160  	s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
   161  	s2 := gset.NewStrSetFrom([]string{"a", "b", "c", "d"}, true)
   162  	fmt.Println(s2.Equal(s1))
   163  
   164  	s3 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
   165  	s4 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
   166  	fmt.Println(s3.Equal(s4))
   167  
   168  	// Output:
   169  	// false
   170  	// true
   171  }
   172  
   173  // Intersect returns a new set which is the intersection from `set` to `other`.
   174  // Which means, all the items in `newSet` are in `set` and also in `other`.
   175  func ExampleStrSet_Intersect() {
   176  	s1 := gset.NewStrSet(true)
   177  	s1.Add([]string{"a", "b", "c"}...)
   178  	var s2 gset.StrSet
   179  	s2.Add([]string{"a", "b", "c", "d"}...)
   180  	fmt.Println(s2.Intersect(s1).Slice())
   181  
   182  	// May Output:
   183  	// [c a b]
   184  }
   185  
   186  // IsSubsetOf checks whether the current set is a sub-set of `other`
   187  func ExampleStrSet_IsSubsetOf() {
   188  	s1 := gset.NewStrSet(true)
   189  	s1.Add([]string{"a", "b", "c", "d"}...)
   190  	var s2 gset.StrSet
   191  	s2.Add([]string{"a", "b", "d"}...)
   192  	fmt.Println(s2.IsSubsetOf(s1))
   193  
   194  	// Output:
   195  	// true
   196  }
   197  
   198  // Iterator iterates the set readonly with given callback function `f`,
   199  // if `f` returns true then continue iterating; or false to stop.
   200  func ExampleStrSet_Iterator() {
   201  	s1 := gset.NewStrSet(true)
   202  	s1.Add([]string{"a", "b", "c", "d"}...)
   203  	s1.Iterator(func(v string) bool {
   204  		fmt.Println("Iterator", v)
   205  		return true
   206  	})
   207  
   208  	// May Output:
   209  	// Iterator a
   210  	// Iterator b
   211  	// Iterator c
   212  	// Iterator d
   213  }
   214  
   215  // Join joins items with a string `glue`.
   216  func ExampleStrSet_Join() {
   217  	s1 := gset.NewStrSet(true)
   218  	s1.Add([]string{"a", "b", "c", "d"}...)
   219  	fmt.Println(s1.Join(","))
   220  
   221  	// May Output:
   222  	// b,c,d,a
   223  }
   224  
   225  // LockFunc locks writing with callback function `f`.
   226  func ExampleStrSet_LockFunc() {
   227  	s1 := gset.NewStrSet(true)
   228  	s1.Add([]string{"1", "2"}...)
   229  	s1.LockFunc(func(m map[string]struct{}) {
   230  		m["3"] = struct{}{}
   231  	})
   232  	fmt.Println(s1.Slice())
   233  
   234  	// May Output
   235  	// [2 3 1]
   236  
   237  }
   238  
   239  // MarshalJSON implements the interface MarshalJSON for json.Marshal.
   240  func ExampleStrSet_MarshalJSON() {
   241  	type Student struct {
   242  		Id     int
   243  		Name   string
   244  		Scores *gset.StrSet
   245  	}
   246  	s := Student{
   247  		Id:     1,
   248  		Name:   "john",
   249  		Scores: gset.NewStrSetFrom([]string{"100", "99", "98"}, true),
   250  	}
   251  	b, _ := json.Marshal(s)
   252  	fmt.Println(string(b))
   253  
   254  	// May Output:
   255  	// {"Id":1,"Name":"john","Scores":["100","99","98"]}
   256  }
   257  
   258  // Merge adds items from `others` sets into `set`.
   259  func ExampleStrSet_Merge() {
   260  	s1 := gset.NewStrSet(true)
   261  	s1.Add([]string{"a", "b", "c", "d"}...)
   262  
   263  	s2 := gset.NewStrSet(true)
   264  	fmt.Println(s1.Merge(s2).Slice())
   265  
   266  	// May Output:
   267  	// [d a b c]
   268  }
   269  
   270  // Pops randomly pops an item from set.
   271  func ExampleStrSet_Pop() {
   272  	s1 := gset.NewStrSet(true)
   273  	s1.Add([]string{"a", "b", "c", "d"}...)
   274  
   275  	fmt.Println(s1.Pop())
   276  
   277  	// May Output:
   278  	// a
   279  }
   280  
   281  // Pops randomly pops `size` items from set.
   282  // It returns all items if size == -1.
   283  func ExampleStrSet_Pops() {
   284  	s1 := gset.NewStrSet(true)
   285  	s1.Add([]string{"a", "b", "c", "d"}...)
   286  	for _, v := range s1.Pops(2) {
   287  		fmt.Println(v)
   288  	}
   289  
   290  	// May Output:
   291  	// a
   292  	// b
   293  }
   294  
   295  // RLockFunc locks reading with callback function `f`.
   296  func ExampleStrSet_RLockFunc() {
   297  	s1 := gset.NewStrSet(true)
   298  	s1.Add([]string{"a", "b", "c", "d"}...)
   299  	s1.RLockFunc(func(m map[string]struct{}) {
   300  		fmt.Println(m)
   301  	})
   302  
   303  	// Output:
   304  	// map[a:{} b:{} c:{} d:{}]
   305  }
   306  
   307  // Remove deletes `item` from set.
   308  func ExampleStrSet_Remove() {
   309  	s1 := gset.NewStrSet(true)
   310  	s1.Add([]string{"a", "b", "c", "d"}...)
   311  	s1.Remove("a")
   312  	fmt.Println(s1.Slice())
   313  
   314  	// May Output:
   315  	// [b c d]
   316  }
   317  
   318  // Size returns the size of the set.
   319  func ExampleStrSet_Size() {
   320  	s1 := gset.NewStrSet(true)
   321  	s1.Add([]string{"a", "b", "c", "d"}...)
   322  	fmt.Println(s1.Size())
   323  
   324  	// Output:
   325  	// 4
   326  }
   327  
   328  // Slice returns the an of items of the set as slice.
   329  func ExampleStrSet_Slice() {
   330  	s1 := gset.NewStrSet(true)
   331  	s1.Add([]string{"a", "b", "c", "d"}...)
   332  	fmt.Println(s1.Slice())
   333  
   334  	// May Output:
   335  	// [a,b,c,d]
   336  }
   337  
   338  // String returns items as a string, which implements like json.Marshal does.
   339  func ExampleStrSet_String() {
   340  	s1 := gset.NewStrSet(true)
   341  	s1.Add([]string{"a", "b", "c", "d"}...)
   342  	fmt.Println(s1.String())
   343  
   344  	// May Output:
   345  	// "a","b","c","d"
   346  }
   347  
   348  // Sum sums items. Note: The items should be converted to int type,
   349  // or you'd get a result that you unexpected.
   350  func ExampleStrSet_Sum() {
   351  	s1 := gset.NewStrSet(true)
   352  	s1.Add([]string{"1", "2", "3", "4"}...)
   353  	fmt.Println(s1.Sum())
   354  
   355  	// Output:
   356  	// 10
   357  }
   358  
   359  // Union returns a new set which is the union of `set` and `other`.
   360  // Which means, all the items in `newSet` are in `set` or in `other`.
   361  func ExampleStrSet_Union() {
   362  	s1 := gset.NewStrSet(true)
   363  	s1.Add([]string{"a", "b", "c", "d"}...)
   364  	s2 := gset.NewStrSet(true)
   365  	s2.Add([]string{"a", "b", "d"}...)
   366  	fmt.Println(s1.Union(s2).Slice())
   367  
   368  	// May Output:
   369  	// [a b c d]
   370  }
   371  
   372  // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
   373  func ExampleStrSet_UnmarshalJSON() {
   374  	b := []byte(`{"Id":1,"Name":"john","Scores":["100","99","98"]}`)
   375  	type Student struct {
   376  		Id     int
   377  		Name   string
   378  		Scores *gset.StrSet
   379  	}
   380  	s := Student{}
   381  	json.Unmarshal(b, &s)
   382  	fmt.Println(s)
   383  
   384  	// May Output:
   385  	// {1 john "99","98","100"}
   386  }
   387  
   388  // UnmarshalValue is an interface implement which sets any type of value for set.
   389  func ExampleStrSet_UnmarshalValue() {
   390  	b := []byte(`{"Id":1,"Name":"john","Scores":["100","99","98"]}`)
   391  	type Student struct {
   392  		Id     int
   393  		Name   string
   394  		Scores *gset.StrSet
   395  	}
   396  	s := Student{}
   397  	json.Unmarshal(b, &s)
   398  	fmt.Println(s)
   399  
   400  	// May Output:
   401  	// {1 john "99","98","100"}
   402  }
   403  
   404  // Walk applies a user supplied function `f` to every item of set.
   405  func ExampleStrSet_Walk() {
   406  	var (
   407  		set    gset.StrSet
   408  		names  = g.SliceStr{"user", "user_detail"}
   409  		prefix = "gf_"
   410  	)
   411  	set.Add(names...)
   412  	// Add prefix for given table names.
   413  	set.Walk(func(item string) string {
   414  		return prefix + item
   415  	})
   416  	fmt.Println(set.Slice())
   417  
   418  	// May Output:
   419  	// [gf_user gf_user_detail]
   420  }