From c1f856fa8eff8e9276291e1f5169271ad3423a3d Mon Sep 17 00:00:00 2001 From: "timmy.hu" Date: Tue, 2 Nov 2021 13:27:54 +0800 Subject: [PATCH 1/8] update --- container/gset/gset_z_example_str_test.go | 157 +++++++++++++++++++++- 1 file changed, 156 insertions(+), 1 deletion(-) diff --git a/container/gset/gset_z_example_str_test.go b/container/gset/gset_z_example_str_test.go index cfa0d22e1..faa8d252e 100644 --- a/container/gset/gset_z_example_str_test.go +++ b/container/gset/gset_z_example_str_test.go @@ -12,17 +12,172 @@ import ( "github.com/gogf/gf/v2/frame/g" ) +func ExampleStrSet_NewStrSet() { + strSet := gset.NewStrSet(true) + strSet.Add([]string{"str1", "str2", "str3"}...) + + // Mya Output: + //Iterator str1 + //Iterator str2 + //Iterator str3 +} + +func ExampleStrSet_Add() { + var strSet gset.StrSet + strSet.Add([]string{"str1", "str2", "str3"}...) + + // Mya Output: + //Iterator str1 + //Iterator str2 + //Iterator str3 +} + +func ExampleStrSet_AddIfNotExist() { + var strSet gset.StrSet + fmt.Println(strSet.AddIfNotExist("str")) + + // Output: + // true +} + +func ExampleStrSet_AddIfNotExistFunc() { + var strSet gset.StrSet + fmt.Println(strSet.AddIfNotExistFunc("str", func() bool { + return true + })) + + // Output: + // true +} + +func ExampleStrSet_AddIfNotExistFuncLock() { + var strSet gset.StrSet + fmt.Println(strSet.AddIfNotExistFuncLock("str", func() bool { + return true + })) + + // Output: + // true +} + +func ExampleStrSet_Clear() { + var strSet gset.StrSet + strSet.Add([]string{"str1", "str2", "str3"}...) + + strSet.Clear() + + fmt.Println(strSet.Size()) + + // Output: + // 0 +} + +func ExampleStrSet_Complement() { + strSet := gset.NewStrSet(true) + strSet.Add([]string{"str1", "str2", "str3", "str4", "str5"}...) + + var s gset.StrSet + s.Add([]string{"str1", "str2", "str3"}...) + + fmt.Println(s.Complement(strSet).Slice()) + + // May Output: + // [str4 str5] +} + func ExampleStrSet_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 +} + +func ExampleStrSet_ContainsI() { + var set gset.StrSet + set.Add("a") + fmt.Println(set.ContainsI("a")) + fmt.Println(set.ContainsI("A")) + + // Output: // true + // true +} + +func ExampleStrSet_Diff() { + s1 := gset.NewStrSet(true) + s1.Add([]string{"a", "b", "c"}...) + var s2 gset.StrSet + s2.Add([]string{"a", "b", "c", "d"}...) + // 差集 + fmt.Println(s2.Diff(s1).Slice()) + + // Output: + // [d] +} + +func ExampleStrSet_Equal() { + s1 := gset.NewStrSet(true) + s1.Add([]string{"a", "b", "c"}...) + var s2 gset.StrSet + s2.Add([]string{"a", "b", "c", "d"}...) + fmt.Println(s2.Equal(s1)) + + // Output: + // false +} + +func ExampleStrSet_Intersect() { + s1 := gset.NewStrSet(true) + s1.Add([]string{"a", "b", "c"}...) + var s2 gset.StrSet + s2.Add([]string{"a", "b", "c", "d"}...) + // 交集 + fmt.Println(s2.Intersect(s1).Slice()) + + // May Output: + // [c a b] +} + +func ExampleStrSet_IsSubsetOf() { + s1 := gset.NewStrSet(true) + s1.Add([]string{"a", "b", "c", "d"}...) + var s2 gset.StrSet + s2.Add([]string{"a", "b", "d"}...) + fmt.Println(s2.IsSubsetOf(s1)) + + // Output: + // true +} + +func ExampleStrSet_Iterator() { + s1 := gset.NewStrSet(true) + s1.Add([]string{"a", "b", "c", "d"}...) + s1.Iterator(func(v string) bool { + fmt.Println("Iterator", v) + return true + }) + // May Output: + // Iterator a + // Iterator b + // Iterator c + // Iterator d +} + +func ExampleStrSet_Join() { + s1 := gset.NewStrSet(true) + s1.Add([]string{"a", "b", "c", "d"}...) + fmt.Println(s1.Join(",")) + + // May Output: + // b,c,d,a +} + +func ExampleStrSet_LockFunc() { + } func ExampleStrSet_Walk() { From 5e34aee2d7fab23c3b160275c468a17b0ca26c4e Mon Sep 17 00:00:00 2001 From: "timmy.hu" Date: Wed, 3 Nov 2021 00:49:06 +0800 Subject: [PATCH 2/8] example over --- container/gset/gset_z_example_str_test.go | 229 ++++++++++++++++++++-- 1 file changed, 214 insertions(+), 15 deletions(-) diff --git a/container/gset/gset_z_example_str_test.go b/container/gset/gset_z_example_str_test.go index faa8d252e..671547499 100644 --- a/container/gset/gset_z_example_str_test.go +++ b/container/gset/gset_z_example_str_test.go @@ -7,21 +7,36 @@ package gset_test import ( + "encoding/json" "fmt" "github.com/gogf/gf/v2/container/gset" "github.com/gogf/gf/v2/frame/g" ) +//NewStrSet create and returns a new set, which contains un-repeated items. +//The parameter is used to specify whether using set in concurrent-safety, +//which is false in default. func ExampleStrSet_NewStrSet() { strSet := gset.NewStrSet(true) strSet.Add([]string{"str1", "str2", "str3"}...) + fmt.Println(strSet.Slice()) - // Mya Output: + // May Output: //Iterator str1 //Iterator str2 //Iterator str3 } +//NewStrSetFrom returns a new set from . +func ExampleStrSet_NewStrSetFrom() { + strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true) + fmt.Println(strSet.Slice()) + + // Output: + // [str1 str2 str3] +} + +//Add adds one or multiple items to the set. func ExampleStrSet_Add() { var strSet gset.StrSet strSet.Add([]string{"str1", "str2", "str3"}...) @@ -32,6 +47,9 @@ func ExampleStrSet_Add() { //Iterator str3 } +//AddIfNotExist checks whether item exists in the set, +//it adds the item to set and returns true if it does not exists in the set, +//or else it does nothing and returns false. func ExampleStrSet_AddIfNotExist() { var strSet gset.StrSet fmt.Println(strSet.AddIfNotExist("str")) @@ -40,6 +58,10 @@ func ExampleStrSet_AddIfNotExist() { // true } +//AddIfNotExistFunc checks whether item exists in the set, +//it adds the item to set and returns true if it does not exists in the set and function returns true, +//or else it does nothing and returns false. +//Note that, the function is executed without writing lock. func ExampleStrSet_AddIfNotExistFunc() { var strSet gset.StrSet fmt.Println(strSet.AddIfNotExistFunc("str", func() bool { @@ -50,6 +72,10 @@ func ExampleStrSet_AddIfNotExistFunc() { // true } +//AddIfNotExistFunc checks whether item exists in the set, +//it adds the item to set and returns true if it does not exists in the set and function returns true, +//or else it does nothing and returns false. +//Note that, the function is executed without writing lock. func ExampleStrSet_AddIfNotExistFuncLock() { var strSet gset.StrSet fmt.Println(strSet.AddIfNotExistFuncLock("str", func() bool { @@ -60,10 +86,9 @@ func ExampleStrSet_AddIfNotExistFuncLock() { // true } +//Clear deletes all items of the set. func ExampleStrSet_Clear() { - var strSet gset.StrSet - strSet.Add([]string{"str1", "str2", "str3"}...) - + strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true) strSet.Clear() fmt.Println(strSet.Size()) @@ -72,19 +97,19 @@ func ExampleStrSet_Clear() { // 0 } +//Complement returns a new set which is the complement from to . +//Which means, all the items in are in and not in . +//It returns the difference between and if the given set is not the full set of . func ExampleStrSet_Complement() { - strSet := gset.NewStrSet(true) - strSet.Add([]string{"str1", "str2", "str3", "str4", "str5"}...) - - var s gset.StrSet - s.Add([]string{"str1", "str2", "str3"}...) - + strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3", "str4", "str5"}, true) + s := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true) fmt.Println(s.Complement(strSet).Slice()) // May Output: // [str4 str5] } +//Contains checks whether the set contains . func ExampleStrSet_Contains() { var set gset.StrSet set.Add("a") @@ -96,6 +121,8 @@ func ExampleStrSet_Contains() { // false } +//ContainsI checks whether a value exists in the set with case-insensitively. +//Note that it internally iterates the whole set to do the comparison with case-insensitively. func ExampleStrSet_ContainsI() { var set gset.StrSet set.Add("a") @@ -107,11 +134,11 @@ func ExampleStrSet_ContainsI() { // true } +//Diff returns a new set which is the difference set from to . +//Which means, all the items in are in but not in . func ExampleStrSet_Diff() { - s1 := gset.NewStrSet(true) - s1.Add([]string{"a", "b", "c"}...) - var s2 gset.StrSet - s2.Add([]string{"a", "b", "c", "d"}...) + s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true) + s2 := gset.NewStrSetFrom([]string{"a", "b", "c", "d"}, true) // 差集 fmt.Println(s2.Diff(s1).Slice()) @@ -119,6 +146,7 @@ func ExampleStrSet_Diff() { // [d] } +//Equal checks whether the two sets equal. func ExampleStrSet_Equal() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c"}...) @@ -130,6 +158,8 @@ func ExampleStrSet_Equal() { // false } +//Intersect returns a new set which is the intersection from to . +//Which means, all the items in are in and also in . func ExampleStrSet_Intersect() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c"}...) @@ -142,6 +172,7 @@ func ExampleStrSet_Intersect() { // [c a b] } +//IsSubsetOf checks whether the current set is a sub-set of . func ExampleStrSet_IsSubsetOf() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c", "d"}...) @@ -153,6 +184,8 @@ func ExampleStrSet_IsSubsetOf() { // true } +//Iterator iterates the set readonly with given callback function , +//if returns true then continue iterating; or false to stop. func ExampleStrSet_Iterator() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c", "d"}...) @@ -167,6 +200,7 @@ func ExampleStrSet_Iterator() { // Iterator d } +//Join joins items with a string . func ExampleStrSet_Join() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c", "d"}...) @@ -176,10 +210,175 @@ func ExampleStrSet_Join() { // b,c,d,a } +//LockFunc locks writing with callback function . func ExampleStrSet_LockFunc() { - + s := gset.NewStrSet(true) + s.Add([]string{"a", "b", "c", "d"}...) + s.LockFunc(func(m map[string]struct{}) { + m["a"] = struct{}{} + }) } +//MarshalJSON implements the interface MarshalJSON for json.Marshal. +func ExampleStrSet_MarshalJSON() { + type Student struct { + Id int + Name string + Scores *gset.StrSet + } + s := Student{ + Id: 1, + Name: "john", + Scores: gset.NewStrSetFrom([]string{"100", "99", "98"}, true), + } + b, _ := json.Marshal(s) + fmt.Println(string(b)) + + // May Output: + // {"Id":1,"Name":"john","Scores":["100","99","98"]} +} + +//Merge adds items from sets into . +func ExampleStrSet_Merge() { + s1 := gset.NewStrSet(true) + s1.Add([]string{"a", "b", "c", "d"}...) + + s2 := gset.NewStrSet(true) + fmt.Println(s1.Merge(s2).Slice()) + + // May Output: + // [d a b c] +} + +//Pops randomly pops an item from set. +func ExampleStrSet_Pop() { + s1 := gset.NewStrSet(true) + s1.Add([]string{"a", "b", "c", "d"}...) + + fmt.Println(s1.Pop()) + + // May Output: + // a +} + +//Pops randomly pops items from set. +//It returns all items if size == -1. +func ExampleStrSet_Pops() { + s1 := gset.NewStrSet(true) + s1.Add([]string{"a", "b", "c", "d"}...) + for _, v := range s1.Pops(2) { + fmt.Println(v) + } + + // May Output: + // a + // b +} + +//RLockFunc locks reading with callback function . +func ExampleStrSet_RLockFunc() { + s1 := gset.NewStrSet(true) + s1.Add([]string{"a", "b", "c", "d"}...) + s1.RLockFunc(func(m map[string]struct{}) { + fmt.Println(m) + }) + + // Output: + // map[a:{} b:{} c:{} d:{}] +} + +//Remove deletes from set. +func ExampleStrSet_Remove() { + s1 := gset.NewStrSet(true) + s1.Add([]string{"a", "b", "c", "d"}...) + s1.Remove("a") + fmt.Println(s1.Slice()) + + // Output: + // [b c d] +} + +//Size returns the size of the set. +func ExampleStrSet_Size() { + s1 := gset.NewStrSet(true) + s1.Add([]string{"a", "b", "c", "d"}...) + fmt.Println(s1.Size()) + + // Output: + // 4 +} + +//Slice returns the a of items of the set as slice. +func ExampleStrSet_Slice() { + s1 := gset.NewStrSet(true) + s1.Add([]string{"a", "b", "c", "d"}...) + fmt.Println(s1.Slice()) + + // May Output: + // [a,b,c,d] +} + +//String returns items as a string, which implements like json.Marshal does. +func ExampleStrSet_String() { + s1 := gset.NewStrSet(true) + s1.Add([]string{"a", "b", "c", "d"}...) + fmt.Println(s1.String()) + + // Output: + // "a","b","c","d" +} + +//Sum sums items. Note: The items should be converted to int type, +//or you'd get a result that you unexpected. +func ExampleStrSet_Sum() { + s1 := gset.NewStrSet(true) + s1.Add([]string{"a", "b", "c", "d"}...) + fmt.Println(s1.Sum()) + + // Output: + // 0 +} + +//Union returns a new set which is the union of and . +//Which means, all the items in are in or in . +func ExampleStrSet_Union() { + s1 := gset.NewStrSet(true) + s1.Add([]string{"a", "b", "c", "d"}...) + s2 := gset.NewStrSet(true) + s2.Add([]string{"a", "b", "d"}...) + fmt.Println(s1.Union(s2).Slice()) + + // May Output: + // [a b c d] +} + +//UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. +func ExampleStrSet_UnmarshalJSON() { + b := []byte(`{"Id":1,"Name":"john","Scores":["100","99","98"]}`) + type Student struct { + Id int + Name string + Scores *gset.StrSet + } + s := Student{} + json.Unmarshal(b, &s) + fmt.Println(s) + + // May Output: + // {1 john "99","98","100"} +} + +//UnmarshalValue is an interface implement which sets any type of value for set. +func ExampleStrSet_UnmarshalValue() { + s := gset.NewStrSetFrom([]string{"a"}, true) + s.UnmarshalValue([]string{"b", "c"}) + fmt.Println(s.Slice()) + + // Output: + // [a b c] +} + +//Walk applies a user supplied function to every item of set. func ExampleStrSet_Walk() { var ( set gset.StrSet From f2bb9d65c3e3d248bf77aae83e8520a65b3d9692 Mon Sep 17 00:00:00 2001 From: "timmy.hu" Date: Wed, 3 Nov 2021 22:49:30 +0800 Subject: [PATCH 3/8] edit review --- container/gset/gset_z_example_str_test.go | 213 +++++++++++++--------- 1 file changed, 127 insertions(+), 86 deletions(-) diff --git a/container/gset/gset_z_example_str_test.go b/container/gset/gset_z_example_str_test.go index 671547499..1adb15f3b 100644 --- a/container/gset/gset_z_example_str_test.go +++ b/container/gset/gset_z_example_str_test.go @@ -11,11 +11,12 @@ import ( "fmt" "github.com/gogf/gf/v2/container/gset" "github.com/gogf/gf/v2/frame/g" + "time" ) -//NewStrSet create and returns a new set, which contains un-repeated items. -//The parameter is used to specify whether using set in concurrent-safety, -//which is false in default. +// NewStrSet create and returns a new set, which contains un-repeated items. +// The parameter `safe` is used to specify whether using set in concurrent-safety, +// which is false in default. func ExampleStrSet_NewStrSet() { strSet := gset.NewStrSet(true) strSet.Add([]string{"str1", "str2", "str3"}...) @@ -27,7 +28,7 @@ func ExampleStrSet_NewStrSet() { //Iterator str3 } -//NewStrSetFrom returns a new set from . +// NewStrSetFrom returns a new set from `items`. func ExampleStrSet_NewStrSetFrom() { strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true) fmt.Println(strSet.Slice()) @@ -36,70 +37,81 @@ func ExampleStrSet_NewStrSetFrom() { // [str1 str2 str3] } -//Add adds one or multiple items to the set. +// Add adds one or multiple items to the set. func ExampleStrSet_Add() { - var strSet gset.StrSet - strSet.Add([]string{"str1", "str2", "str3"}...) - - // Mya Output: - //Iterator str1 - //Iterator str2 - //Iterator str3 -} - -//AddIfNotExist checks whether item exists in the set, -//it adds the item to set and returns true if it does not exists in the set, -//or else it does nothing and returns false. -func ExampleStrSet_AddIfNotExist() { - var strSet gset.StrSet + strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true) + strSet.Add("str") + fmt.Println(strSet.Slice()) fmt.Println(strSet.AddIfNotExist("str")) - // Output: - // true + // Mya Output: + // [str str1 str2 str3] + // false } -//AddIfNotExistFunc checks whether item exists in the set, -//it adds the item to set and returns true if it does not exists in the set and function returns true, -//or else it does nothing and returns false. -//Note that, the function is executed without writing lock. +// AddIfNotExist checks whether item exists in the set, +// it adds the item to set and returns true if it does not exists in the set, +// or else it does nothing and returns false. +func ExampleStrSet_AddIfNotExist() { + strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true) + strSet.Add("str") + fmt.Println(strSet.Slice()) + fmt.Println(strSet.AddIfNotExist("str")) + + // Mya Output: + // [str str1 str2 str3] + // false +} + +// AddIfNotExistFunc checks whether item exists in the set, +// it adds the item to set and returns true if it does not exists in the set and function `f` returns true, +// or else it does nothing and returns false. +// Note that, the function `f` is executed without writing lock. func ExampleStrSet_AddIfNotExistFunc() { - var strSet gset.StrSet - fmt.Println(strSet.AddIfNotExistFunc("str", func() bool { + strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true) + strSet.Add("str") + fmt.Println(strSet.Slice()) + fmt.Println(strSet.AddIfNotExistFunc("str5", func() bool { return true })) - // Output: + // May Output: + // [str1 str2 str3 str] // true } -//AddIfNotExistFunc checks whether item exists in the set, -//it adds the item to set and returns true if it does not exists in the set and function returns true, -//or else it does nothing and returns false. -//Note that, the function is executed without writing lock. +// AddIfNotExistFunc checks whether item exists in the set, +// it adds the item to set and returns true if it does not exists in the set and function `f` returns true, +// or else it does nothing and returns false. +// Note that, the function `f` is executed without writing lock. func ExampleStrSet_AddIfNotExistFuncLock() { - var strSet gset.StrSet - fmt.Println(strSet.AddIfNotExistFuncLock("str", func() bool { + strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true) + strSet.Add("str") + fmt.Println(strSet.Slice()) + fmt.Println(strSet.AddIfNotExistFuncLock("str4", func() bool { return true })) - // Output: + // May Output: + // [str1 str2 str3 str] // true } -//Clear deletes all items of the set. +// Clear deletes all items of the set. func ExampleStrSet_Clear() { strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true) + fmt.Println(strSet.Size()) strSet.Clear() - fmt.Println(strSet.Size()) // Output: + // 3 // 0 } -//Complement returns a new set which is the complement from to . -//Which means, all the items in are in and not in . -//It returns the difference between and if the given set is not the full set of . +// Complement returns a new set which is the complement from `set` to `full`. +// Which means, all the items in `newSet` are in `full` and not in `set`. +// It returns the difference between `full` and `set` if the given set `full` is not the full set of `set`. func ExampleStrSet_Complement() { strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3", "str4", "str5"}, true) s := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true) @@ -109,7 +121,7 @@ func ExampleStrSet_Complement() { // [str4 str5] } -//Contains checks whether the set contains . +// Contains checks whether the set contains `item`. func ExampleStrSet_Contains() { var set gset.StrSet set.Add("a") @@ -121,8 +133,8 @@ func ExampleStrSet_Contains() { // false } -//ContainsI checks whether a value exists in the set with case-insensitively. -//Note that it internally iterates the whole set to do the comparison with case-insensitively. +// ContainsI checks whether a value exists in the set with case-insensitively. +// Note that it internally iterates the whole set to do the comparison with case-insensitively. func ExampleStrSet_ContainsI() { var set gset.StrSet set.Add("a") @@ -134,45 +146,46 @@ func ExampleStrSet_ContainsI() { // true } -//Diff returns a new set which is the difference set from to . -//Which means, all the items in are in but not in . +// Diff returns a new set which is the difference set from `set` to `other`. +// Which means, all the items in `newSet` are in `set` but not in `other`. func ExampleStrSet_Diff() { s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true) s2 := gset.NewStrSetFrom([]string{"a", "b", "c", "d"}, true) - // 差集 fmt.Println(s2.Diff(s1).Slice()) // Output: // [d] } -//Equal checks whether the two sets equal. +// Equal checks whether the two sets equal. func ExampleStrSet_Equal() { - s1 := gset.NewStrSet(true) - s1.Add([]string{"a", "b", "c"}...) - var s2 gset.StrSet - s2.Add([]string{"a", "b", "c", "d"}...) + s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true) + s2 := gset.NewStrSetFrom([]string{"a", "b", "c", "d"}, true) fmt.Println(s2.Equal(s1)) + s3 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true) + s4 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true) + fmt.Println(s3.Equal(s4)) + // Output: // false + // true } -//Intersect returns a new set which is the intersection from to . -//Which means, all the items in are in and also in . +// Intersect returns a new set which is the intersection from `set` to `other`. +// Which means, all the items in `newSet` are in `set` and also in `other`. func ExampleStrSet_Intersect() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c"}...) var s2 gset.StrSet s2.Add([]string{"a", "b", "c", "d"}...) - // 交集 fmt.Println(s2.Intersect(s1).Slice()) // May Output: // [c a b] } -//IsSubsetOf checks whether the current set is a sub-set of . +// IsSubsetOf checks whether the current set is a sub-set of `other` func ExampleStrSet_IsSubsetOf() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c", "d"}...) @@ -184,8 +197,8 @@ func ExampleStrSet_IsSubsetOf() { // true } -//Iterator iterates the set readonly with given callback function , -//if returns true then continue iterating; or false to stop. +// Iterator iterates the set readonly with given callback function `f`, +// if `f` returns true then continue iterating; or false to stop. func ExampleStrSet_Iterator() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c", "d"}...) @@ -200,7 +213,7 @@ func ExampleStrSet_Iterator() { // Iterator d } -//Join joins items with a string . +// Join joins items with a string `glue`. func ExampleStrSet_Join() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c", "d"}...) @@ -210,16 +223,38 @@ func ExampleStrSet_Join() { // b,c,d,a } -//LockFunc locks writing with callback function . +// LockFunc locks writing with callback function `f`. func ExampleStrSet_LockFunc() { - s := gset.NewStrSet(true) - s.Add([]string{"a", "b", "c", "d"}...) - s.LockFunc(func(m map[string]struct{}) { - m["a"] = struct{}{} - }) + s1 := gset.NewStrSet(true) + s1.Add([]string{"1", "2"}...) + go func() { + s1.LockFunc(func(m map[string]struct{}) { + m["3"] = struct{}{} + }) + fmt.Println("one:", s1.Slice()) + }() + time.Sleep(time.Duration(2) * time.Second) + go func() { + s1.LockFunc(func(m map[string]struct{}) { + m["4"] = struct{}{} + }) + fmt.Println("two:", s1.Slice()) + }() + time.Sleep(time.Duration(2) * time.Second) + go func() { + s1.LockFunc(func(m map[string]struct{}) { + m["5"] = struct{}{} + }) + fmt.Println("three:", s1.Slice()) + }() + time.Sleep(time.Duration(2) * time.Second) + // May Output + // [2 3 1] + // [1 2 3 4] + // [1 2 3 4 5] } -//MarshalJSON implements the interface MarshalJSON for json.Marshal. +// MarshalJSON implements the interface MarshalJSON for json.Marshal. func ExampleStrSet_MarshalJSON() { type Student struct { Id int @@ -238,7 +273,7 @@ func ExampleStrSet_MarshalJSON() { // {"Id":1,"Name":"john","Scores":["100","99","98"]} } -//Merge adds items from sets into . +// Merge adds items from `others` sets into `set`. func ExampleStrSet_Merge() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c", "d"}...) @@ -250,7 +285,7 @@ func ExampleStrSet_Merge() { // [d a b c] } -//Pops randomly pops an item from set. +// Pops randomly pops an item from set. func ExampleStrSet_Pop() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c", "d"}...) @@ -261,8 +296,8 @@ func ExampleStrSet_Pop() { // a } -//Pops randomly pops items from set. -//It returns all items if size == -1. +// Pops randomly pops `size` items from set. +// It returns all items if size == -1. func ExampleStrSet_Pops() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c", "d"}...) @@ -275,7 +310,7 @@ func ExampleStrSet_Pops() { // b } -//RLockFunc locks reading with callback function . +// RLockFunc locks reading with callback function `f`. func ExampleStrSet_RLockFunc() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c", "d"}...) @@ -287,7 +322,7 @@ func ExampleStrSet_RLockFunc() { // map[a:{} b:{} c:{} d:{}] } -//Remove deletes from set. +// Remove deletes `item` from set. func ExampleStrSet_Remove() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c", "d"}...) @@ -298,7 +333,7 @@ func ExampleStrSet_Remove() { // [b c d] } -//Size returns the size of the set. +// Size returns the size of the set. func ExampleStrSet_Size() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c", "d"}...) @@ -308,7 +343,7 @@ func ExampleStrSet_Size() { // 4 } -//Slice returns the a of items of the set as slice. +// Slice returns the a of items of the set as slice. func ExampleStrSet_Slice() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c", "d"}...) @@ -318,7 +353,7 @@ func ExampleStrSet_Slice() { // [a,b,c,d] } -//String returns items as a string, which implements like json.Marshal does. +// String returns items as a string, which implements like json.Marshal does. func ExampleStrSet_String() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c", "d"}...) @@ -328,8 +363,8 @@ func ExampleStrSet_String() { // "a","b","c","d" } -//Sum sums items. Note: The items should be converted to int type, -//or you'd get a result that you unexpected. +// Sum sums items. Note: The items should be converted to int type, +// or you'd get a result that you unexpected. func ExampleStrSet_Sum() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c", "d"}...) @@ -339,8 +374,8 @@ func ExampleStrSet_Sum() { // 0 } -//Union returns a new set which is the union of and . -//Which means, all the items in are in or in . +// Union returns a new set which is the union of `set` and `other`. +// Which means, all the items in `newSet` are in `set` or in `other`. func ExampleStrSet_Union() { s1 := gset.NewStrSet(true) s1.Add([]string{"a", "b", "c", "d"}...) @@ -352,7 +387,7 @@ func ExampleStrSet_Union() { // [a b c d] } -//UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. +// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. func ExampleStrSet_UnmarshalJSON() { b := []byte(`{"Id":1,"Name":"john","Scores":["100","99","98"]}`) type Student struct { @@ -368,17 +403,23 @@ func ExampleStrSet_UnmarshalJSON() { // {1 john "99","98","100"} } -//UnmarshalValue is an interface implement which sets any type of value for set. +// UnmarshalValue is an interface implement which sets any type of value for set. func ExampleStrSet_UnmarshalValue() { - s := gset.NewStrSetFrom([]string{"a"}, true) - s.UnmarshalValue([]string{"b", "c"}) - fmt.Println(s.Slice()) + b := []byte(`{"Id":1,"Name":"john","Scores":["100","99","98"]}`) + type Student struct { + Id int + Name string + Scores *gset.StrSet + } + s := Student{} + json.Unmarshal(b, &s) + fmt.Println(s) - // Output: - // [a b c] + // May Output: + // {1 john "99","98","100"} } -//Walk applies a user supplied function to every item of set. +// Walk applies a user supplied function `f` to every item of set. func ExampleStrSet_Walk() { var ( set gset.StrSet From c850c420fd2f579fb958d7f0314347703dd1c97c Mon Sep 17 00:00:00 2001 From: "timmy.hu" Date: Wed, 3 Nov 2021 23:06:24 +0800 Subject: [PATCH 4/8] fixCi --- container/gset/gset_z_example_str_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/container/gset/gset_z_example_str_test.go b/container/gset/gset_z_example_str_test.go index 1adb15f3b..5bc15b152 100644 --- a/container/gset/gset_z_example_str_test.go +++ b/container/gset/gset_z_example_str_test.go @@ -33,7 +33,7 @@ func ExampleStrSet_NewStrSetFrom() { strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true) fmt.Println(strSet.Slice()) - // Output: + // May Output: // [str1 str2 str3] } From 9ea2db5c817bb3278aef7dea46a967d37d8e1cfc Mon Sep 17 00:00:00 2001 From: "timmy.hu" Date: Wed, 3 Nov 2021 23:17:04 +0800 Subject: [PATCH 5/8] fix --- container/gset/gset_z_example_str_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/container/gset/gset_z_example_str_test.go b/container/gset/gset_z_example_str_test.go index 5bc15b152..c81568f2a 100644 --- a/container/gset/gset_z_example_str_test.go +++ b/container/gset/gset_z_example_str_test.go @@ -359,7 +359,7 @@ func ExampleStrSet_String() { s1.Add([]string{"a", "b", "c", "d"}...) fmt.Println(s1.String()) - // Output: + // May Output: // "a","b","c","d" } From 430102c995bbbe4e164932b61a31244c581b04c1 Mon Sep 17 00:00:00 2001 From: "timmy.hu" Date: Thu, 4 Nov 2021 22:11:09 +0800 Subject: [PATCH 6/8] update --- container/gset/gset_z_example_str_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/container/gset/gset_z_example_str_test.go b/container/gset/gset_z_example_str_test.go index c81568f2a..e3cb6cf4c 100644 --- a/container/gset/gset_z_example_str_test.go +++ b/container/gset/gset_z_example_str_test.go @@ -23,9 +23,7 @@ func ExampleStrSet_NewStrSet() { fmt.Println(strSet.Slice()) // May Output: - //Iterator str1 - //Iterator str2 - //Iterator str3 + // [str3 str1 str2] } // NewStrSetFrom returns a new set from `items`. From 3091f61a2656cf1bec8ffb02b518f81f39dc62b1 Mon Sep 17 00:00:00 2001 From: "timmy.hu" Date: Thu, 4 Nov 2021 22:26:17 +0800 Subject: [PATCH 7/8] update ExampleStrSet_Remove --- container/gset/gset_z_example_str_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/container/gset/gset_z_example_str_test.go b/container/gset/gset_z_example_str_test.go index e3cb6cf4c..3a08aaf50 100644 --- a/container/gset/gset_z_example_str_test.go +++ b/container/gset/gset_z_example_str_test.go @@ -327,7 +327,7 @@ func ExampleStrSet_Remove() { s1.Remove("a") fmt.Println(s1.Slice()) - // Output: + // May Output: // [b c d] } From ef7f7e35f8ae2e14782e2d39ef00ac0a188a92dc Mon Sep 17 00:00:00 2001 From: "timmy.hu" Date: Fri, 5 Nov 2021 00:04:24 +0800 Subject: [PATCH 8/8] update review --- container/gset/gset_z_example_str_test.go | 39 +++++++---------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/container/gset/gset_z_example_str_test.go b/container/gset/gset_z_example_str_test.go index 3a08aaf50..586e55615 100644 --- a/container/gset/gset_z_example_str_test.go +++ b/container/gset/gset_z_example_str_test.go @@ -11,13 +11,12 @@ import ( "fmt" "github.com/gogf/gf/v2/container/gset" "github.com/gogf/gf/v2/frame/g" - "time" ) // NewStrSet create and returns a new set, which contains un-repeated items. // The parameter `safe` is used to specify whether using set in concurrent-safety, // which is false in default. -func ExampleStrSet_NewStrSet() { +func ExampleNewStrSet() { strSet := gset.NewStrSet(true) strSet.Add([]string{"str1", "str2", "str3"}...) fmt.Println(strSet.Slice()) @@ -27,7 +26,7 @@ func ExampleStrSet_NewStrSet() { } // NewStrSetFrom returns a new set from `items`. -func ExampleStrSet_NewStrSetFrom() { +func ExampleNewStrSetFrom() { strSet := gset.NewStrSetFrom([]string{"str1", "str2", "str3"}, true) fmt.Println(strSet.Slice()) @@ -204,6 +203,7 @@ func ExampleStrSet_Iterator() { fmt.Println("Iterator", v) return true }) + // May Output: // Iterator a // Iterator b @@ -225,31 +225,14 @@ func ExampleStrSet_Join() { func ExampleStrSet_LockFunc() { s1 := gset.NewStrSet(true) s1.Add([]string{"1", "2"}...) - go func() { - s1.LockFunc(func(m map[string]struct{}) { - m["3"] = struct{}{} - }) - fmt.Println("one:", s1.Slice()) - }() - time.Sleep(time.Duration(2) * time.Second) - go func() { - s1.LockFunc(func(m map[string]struct{}) { - m["4"] = struct{}{} - }) - fmt.Println("two:", s1.Slice()) - }() - time.Sleep(time.Duration(2) * time.Second) - go func() { - s1.LockFunc(func(m map[string]struct{}) { - m["5"] = struct{}{} - }) - fmt.Println("three:", s1.Slice()) - }() - time.Sleep(time.Duration(2) * time.Second) + s1.LockFunc(func(m map[string]struct{}) { + m["3"] = struct{}{} + }) + fmt.Println(s1.Slice()) + // May Output // [2 3 1] - // [1 2 3 4] - // [1 2 3 4 5] + } // MarshalJSON implements the interface MarshalJSON for json.Marshal. @@ -365,11 +348,11 @@ func ExampleStrSet_String() { // or you'd get a result that you unexpected. func ExampleStrSet_Sum() { s1 := gset.NewStrSet(true) - s1.Add([]string{"a", "b", "c", "d"}...) + s1.Add([]string{"1", "2", "3", "4"}...) fmt.Println(s1.Sum()) // Output: - // 0 + // 10 } // Union returns a new set which is the union of `set` and `other`.