diff --git a/container/garray/garray_z_example_any_test.go b/container/garray/garray_z_example_any_test.go new file mode 100644 index 000000000..9a427a10b --- /dev/null +++ b/container/garray/garray_z_example_any_test.go @@ -0,0 +1,245 @@ +// Copyright 2018 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 this file, +// You can obtain one at https://github.com/gogf/gf. + +package garray_test + +import ( + "fmt" + "github.com/gogf/gf/frame/g" + + "github.com/gogf/gf/container/garray" +) + +func ExampleNew() { + // A normal array. + a := garray.New() + + // Adding items. + for i := 0; i < 10; i++ { + a.Append(i) + } + + // Print the array length. + fmt.Println(a.Len()) + + // Print the array items. + fmt.Println(a.Slice()) + + // Retrieve item by index. + fmt.Println(a.Get(6)) + + // Check item existence. + fmt.Println(a.Contains(6)) + fmt.Println(a.Contains(100)) + + // Insert item before specified index. + a.InsertAfter(9, 11) + // Insert item after specified index. + a.InsertBefore(10, 10) + + fmt.Println(a.Slice()) + + // Modify item by index. + a.Set(0, 100) + fmt.Println(a.Slice()) + + // Search item and return its index. + fmt.Println(a.Search(5)) + + // Remove item by index. + a.Remove(0) + fmt.Println(a.Slice()) + + // Empty the array, removes all items of it. + fmt.Println(a.Slice()) + a.Clear() + fmt.Println(a.Slice()) + + // Output: + // 10 + // [0 1 2 3 4 5 6 7 8 9] + // 6 true + // true + // false + // [0 1 2 3 4 5 6 7 8 9 10 11] + // [100 1 2 3 4 5 6 7 8 9 10 11] + // 5 + // [1 2 3 4 5 6 7 8 9 10 11] + // [1 2 3 4 5 6 7 8 9 10 11] + // [] +} + +func ExampleArray_Iterator() { + array := garray.NewArrayFrom(g.Slice{"a", "b", "c"}) + // Iterator is alias of IteratorAsc, which iterates the array readonly in ascending order + // with given callback function . + // If returns true, then it continues iterating; or false to stop. + array.Iterator(func(k int, v interface{}) bool { + fmt.Println(k, v) + return true + }) + // IteratorDesc iterates the array readonly in descending order with given callback function . + // If returns true, then it continues iterating; or false to stop. + array.IteratorDesc(func(k int, v interface{}) bool { + fmt.Println(k, v) + return true + }) + + // Output: + // 0 a + // 1 b + // 2 c + // 2 c + // 1 b + // 0 a +} + +func ExampleArray_Reverse() { + array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) + + // Reverse makes array with elements in reverse order. + fmt.Println(array.Reverse().Slice()) + + // Output: + // [9 8 7 6 5 4 3 2 1] +} + +func ExampleArray_Shuffle() { + array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) + + // Shuffle randomly shuffles the array. + fmt.Println(array.Shuffle().Slice()) +} + +func ExampleArray_Rands() { + array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) + + // Randomly retrieve and return 2 items from the array. + // It does not delete the items from array. + fmt.Println(array.Rands(2)) + + // Randomly pick and return one item from the array. + // It deletes the picked up item from array. + fmt.Println(array.PopRand()) +} + +func ExampleArray_PopRand() { + array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) + + // Randomly retrieve and return 2 items from the array. + // It does not delete the items from array. + fmt.Println(array.Rands(2)) + + // Randomly pick and return one item from the array. + // It deletes the picked up item from array. + fmt.Println(array.PopRand()) +} + +func ExampleArray_Join() { + array := garray.NewFrom(g.Slice{"a", "b", "c", "d"}) + fmt.Println(array.Join(",")) + + // Output: + // a,b,c,d +} + +func ExampleArray_Chunk() { + array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) + + // Chunk splits an array into multiple arrays, + // the size of each array is determined by . + // The last chunk may contain less than size elements. + fmt.Println(array.Chunk(2)) + + // Output: + // [[1 2] [3 4] [5 6] [7 8] [9]] +} + +func Example_popItem() { + array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9}) + + // Any Pop* functions pick, delete and return the item from array. + + fmt.Println(array.PopLeft()) + fmt.Println(array.PopLefts(2)) + fmt.Println(array.PopRight()) + fmt.Println(array.PopRights(2)) + + // Output: + // 1 true + // [2 3] + // 9 true + // [7 8] +} + +func Example_walk() { + var array garray.StrArray + tables := g.SliceStr{"user", "user_detail"} + prefix := "gf_" + array.Append(tables...) + // Add prefix for given table names. + array.Walk(func(value string) string { + return prefix + value + }) + fmt.Println(array.Slice()) + + // Output: + // [gf_user gf_user_detail] +} + +func ExampleArray_Contains() { + var array garray.StrArray + array.Append("a") + fmt.Println(array.Contains("a")) + fmt.Println(array.Contains("A")) + fmt.Println(array.ContainsI("A")) + + // Output: + // true + // false + // true +} + +func ExampleArray_Merge() { + array1 := garray.NewFrom(g.Slice{1, 2}) + array2 := garray.NewFrom(g.Slice{3, 4}) + slice1 := g.Slice{5, 6} + slice2 := []int{7, 8} + slice3 := []string{"9", "0"} + fmt.Println(array1.Slice()) + array1.Merge(array1) + array1.Merge(array2) + array1.Merge(slice1) + array1.Merge(slice2) + array1.Merge(slice3) + fmt.Println(array1.Slice()) + + // Output: + // [1 2] + // [1 2 1 2 3 4 5 6 7 8 9 0] +} + +func ExampleArray_FilterEmpty() { + array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"}) + array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"}) + fmt.Printf("%#v\n", array1.FilterNil().Slice()) + fmt.Printf("%#v\n", array2.FilterEmpty().Slice()) + + // Output: + // []interface {}{0, 1, 2, "", []interface {}{}, "john"} + // []interface {}{1, 2, "john"} +} + +func ExampleArray_FilterNil() { + array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"}) + array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"}) + fmt.Printf("%#v\n", array1.FilterNil().Slice()) + fmt.Printf("%#v\n", array2.FilterEmpty().Slice()) + + // Output: + // []interface {}{0, 1, 2, "", []interface {}{}, "john"} + // []interface {}{1, 2, "john"} +} diff --git a/container/garray/garray_z_example_test.go b/container/garray/garray_z_example_test.go index 881dae1f2..d0b7b78e5 100644 --- a/container/garray/garray_z_example_test.go +++ b/container/garray/garray_z_example_test.go @@ -5,218 +5,3 @@ // You can obtain one at https://github.com/gogf/gf. package garray_test - -import ( - "fmt" - "github.com/gogf/gf/frame/g" - - "github.com/gogf/gf/container/garray" -) - -func Example_basic() { - // A normal array. - a := garray.New() - - // Adding items. - for i := 0; i < 10; i++ { - a.Append(i) - } - - // Print the array length. - fmt.Println(a.Len()) - - // Print the array items. - fmt.Println(a.Slice()) - - // Retrieve item by index. - fmt.Println(a.Get(6)) - - // Check item existence. - fmt.Println(a.Contains(6)) - fmt.Println(a.Contains(100)) - - // Insert item before specified index. - a.InsertAfter(9, 11) - // Insert item after specified index. - a.InsertBefore(10, 10) - - fmt.Println(a.Slice()) - - // Modify item by index. - a.Set(0, 100) - fmt.Println(a.Slice()) - - // Search item and return its index. - fmt.Println(a.Search(5)) - - // Remove item by index. - a.Remove(0) - fmt.Println(a.Slice()) - - // Empty the array, removes all items of it. - fmt.Println(a.Slice()) - a.Clear() - fmt.Println(a.Slice()) - - // Output: - // 10 - // [0 1 2 3 4 5 6 7 8 9] - // 6 true - // true - // false - // [0 1 2 3 4 5 6 7 8 9 10 11] - // [100 1 2 3 4 5 6 7 8 9 10 11] - // 5 - // [1 2 3 4 5 6 7 8 9 10 11] - // [1 2 3 4 5 6 7 8 9 10 11] - // [] -} - -func Example_iterate() { - array := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"}) - // Iterator is alias of IteratorAsc, which iterates the array readonly in ascending order - // with given callback function . - // If returns true, then it continues iterating; or false to stop. - array.Iterator(func(k int, v string) bool { - fmt.Println(k, v) - return true - }) - // IteratorDesc iterates the array readonly in descending order with given callback function . - // If returns true, then it continues iterating; or false to stop. - array.IteratorDesc(func(k int, v string) bool { - fmt.Println(k, v) - return true - }) - - // Output: - // 0 a - // 1 b - // 2 c - // 2 c - // 1 b - // 0 a -} - -func Example_reverse() { - array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) - - // Reverse makes array with elements in reverse order. - fmt.Println(array.Reverse().Slice()) - - // Output: - // [9 8 7 6 5 4 3 2 1] -} - -func Example_shuffle() { - array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) - - // Shuffle randomly shuffles the array. - fmt.Println(array.Shuffle().Slice()) -} - -func Example_rand() { - array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) - - // Randomly retrieve and return 2 items from the array. - // It does not delete the items from array. - fmt.Println(array.Rands(2)) - - // Randomly pick and return one item from the array. - // It deletes the picked up item from array. - fmt.Println(array.PopRand()) -} - -func Example_join() { - array := garray.NewFrom(g.Slice{"a", "b", "c", "d"}) - fmt.Println(array.Join(",")) - - // Output: - // a,b,c,d -} - -func Example_chunk() { - array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) - - // Chunk splits an array into multiple arrays, - // the size of each array is determined by . - // The last chunk may contain less than size elements. - fmt.Println(array.Chunk(2)) - - // Output: - // [[1 2] [3 4] [5 6] [7 8] [9]] -} - -func Example_popItem() { - array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9}) - - // Any Pop* functions pick, delete and return the item from array. - - fmt.Println(array.PopLeft()) - fmt.Println(array.PopLefts(2)) - fmt.Println(array.PopRight()) - fmt.Println(array.PopRights(2)) - - // Output: - // 1 true - // [2 3] - // 9 true - // [7 8] -} - -func Example_walk() { - var array garray.StrArray - tables := g.SliceStr{"user", "user_detail"} - prefix := "gf_" - array.Append(tables...) - // Add prefix for given table names. - array.Walk(func(value string) string { - return prefix + value - }) - fmt.Println(array.Slice()) - - // Output: - // [gf_user gf_user_detail] -} - -func ExampleArray_Contains() { - var array garray.StrArray - array.Append("a") - fmt.Println(array.Contains("a")) - fmt.Println(array.Contains("A")) - fmt.Println(array.ContainsI("A")) - - // Output: - // true - // false - // true -} - -func Example_mergeArray() { - array1 := garray.NewFrom(g.Slice{1, 2}) - array2 := garray.NewFrom(g.Slice{3, 4}) - slice1 := g.Slice{5, 6} - slice2 := []int{7, 8} - slice3 := []string{"9", "0"} - fmt.Println(array1.Slice()) - array1.Merge(array1) - array1.Merge(array2) - array1.Merge(slice1) - array1.Merge(slice2) - array1.Merge(slice3) - fmt.Println(array1.Slice()) - - // Output: - // [1 2] - // [1 2 1 2 3 4 5 6 7 8 9 0] -} - -func Example_filter() { - array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"}) - array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"}) - fmt.Printf("%#v\n", array1.FilterNil().Slice()) - fmt.Printf("%#v\n", array2.FilterEmpty().Slice()) - - // Output: - // []interface {}{0, 1, 2, "", []interface {}{}, "john"} - // []interface {}{1, 2, "john"} -} diff --git a/container/glist/glist_example_test.go b/container/glist/glist_example_test.go index 37fa37f92..1931f3e51 100644 --- a/container/glist/glist_example_test.go +++ b/container/glist/glist_example_test.go @@ -15,7 +15,7 @@ import ( "github.com/gogf/gf/container/glist" ) -func Example_basic() { +func ExampleNew() { n := 10 l := glist.New() for i := 0; i < n; i++ { @@ -32,14 +32,14 @@ func Example_basic() { fmt.Println(l.Len()) // Output: - //10 - //[0 1 2 3 4 5 6 7 8 9] - //[9 8 7 6 5 4 3 2 1 0] - //0123456789 - //0 + // 10 + // [0 1 2 3 4 5 6 7 8 9] + // [9 8 7 6 5 4 3 2 1 0] + // 0123456789 + // 0 } -func Example_iterate() { +func ExampleList_RLockFunc() { // concurrent-safe list. l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true) // iterate reading from head. @@ -63,21 +63,39 @@ func Example_iterate() { }) fmt.Println() + // Output: + // 12345678910 + // 10987654321 +} +func ExampleList_IteratorAsc() { + // concurrent-safe list. + l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true) // iterate reading from head using IteratorAsc. l.IteratorAsc(func(e *glist.Element) bool { fmt.Print(e.Value) return true }) - fmt.Println() + + // Output: + // 12345678910 +} + +func ExampleList_IteratorDesc() { + // concurrent-safe list. + l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true) // iterate reading from tail using IteratorDesc. l.IteratorDesc(func(e *glist.Element) bool { fmt.Print(e.Value) return true }) + // Output: + // 10987654321 +} - fmt.Println() - +func ExampleList_LockFunc() { + // concurrent-safe list. + l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true) // iterate writing from head. l.LockFunc(func(list *list.List) { length := list.Len() @@ -92,21 +110,14 @@ func Example_iterate() { }) fmt.Println(l) - //output: - //12345678910 - //10987654321 - //12345678910 - //10987654321 - //[1,2,3,4,5,M,7,8,9,10] + // Output: + // [1,2,3,4,5,M,7,8,9,10] } -func Example_popItem() { +func ExampleList_PopBack() { l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) fmt.Println(l.PopBack()) - fmt.Println(l.PopBacks(2)) - fmt.Println(l.PopFront()) - fmt.Println(l.PopFronts(2)) // Output: // 9 @@ -114,8 +125,34 @@ func Example_popItem() { // 1 // [2 3] } +func ExampleList_PopBacks() { + l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) -func Example_join() { + fmt.Println(l.PopBacks(2)) + + // Output: + // [9 8] +} + +func ExampleList_PopFront() { + l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) + + fmt.Println(l.PopFront()) + + // Output: + // 1 +} + +func ExampleList_PopFronts() { + l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9}) + + fmt.Println(l.PopFronts(2)) + + // Output: + // [1 2] +} + +func ExampleList_Join() { var l glist.List l.PushBacks(g.Slice{"a", "b", "c", "d"}) diff --git a/container/gmap/gmap_z_example_test.go b/container/gmap/gmap_z_example_any_any_test.go similarity index 50% rename from container/gmap/gmap_z_example_test.go rename to container/gmap/gmap_z_example_any_any_test.go index b264b1a51..68675b72a 100644 --- a/container/gmap/gmap_z_example_test.go +++ b/container/gmap/gmap_z_example_any_any_test.go @@ -13,62 +13,68 @@ import ( "github.com/gogf/gf/container/gmap" ) -func Example_normalBasic() { +func ExampleNew() { m := gmap.New() - //Add data + // Add data. m.Set("key1", "val1") - //Print size + // Print size. fmt.Println(m.Size()) - //output 1 - add_map := make(map[interface{}]interface{}) - add_map["key2"] = "val2" - add_map["key3"] = "val3" - add_map[1] = 1 + addMap := make(map[interface{}]interface{}) + addMap["key2"] = "val2" + addMap["key3"] = "val3" + addMap[1] = 1 fmt.Println(m.Values()) - //Batch add data - m.Sets(add_map) + // Batch add data. + m.Sets(addMap) - //Gets the value of the corresponding key - key3_val := m.Get("key3") - fmt.Println(key3_val) + // Gets the value of the corresponding key. + fmt.Println(m.Get("key3")) - //Get the value by key, or set it with given key-value if not exist. - get_or_set_val := m.GetOrSet("key4", "val4") - fmt.Println(get_or_set_val) + // Get the value by key, or set it with given key-value if not exist. + fmt.Println(m.GetOrSet("key4", "val4")) // Set key-value if the key does not exist, then return true; or else return false. - is_set := m.SetIfNotExist("key3", "val3") - fmt.Println(is_set) + fmt.Println(m.SetIfNotExist("key3", "val3")) - //Remove key + // Remove key m.Remove("key2") fmt.Println(m.Keys()) - //Batch remove keys - remove_keys := []interface{}{"key1", 1} - m.Removes(remove_keys) + // Batch remove keys. + m.Removes([]interface{}{"key1", 1}) fmt.Println(m.Keys()) - //Contains checks whether a key exists. - is_contain := m.Contains("key3") - fmt.Println(is_contain) + // Contains checks whether a key exists. + fmt.Println(m.Contains("key3")) - //Flip exchanges key-value of the map, it will change key-value to value-key. + // Flip exchanges key-value of the map, it will change key-value to value-key. m.Flip() fmt.Println(m.Map()) - // Clear deletes all data of the map, + // Clear deletes all data of the map. m.Clear() fmt.Println(m.Size()) + + // May Output: + // 1 + // [val1] + // val3 + // val4 + // false + // [key4 key1 key3 1] + // [key4 key3] + // true + // map[val3:key3 val4:key4] + // 0 } -func Example_keysValues() { +func ExampleAnyAnyMap_Keys() { var m gmap.Map m.Sets(g.MapAnyAny{ "k1": "v1", @@ -84,7 +90,23 @@ func Example_keysValues() { // [v2 v3 v4 v1] } -func Example_flip() { +func ExampleAnyAnyMap_Values() { + var m gmap.Map + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + fmt.Println(m.Keys()) + fmt.Println(m.Values()) + + // May Output: + // [k1 k2 k3 k4] + // [v2 v3 v4 v1] +} + +func ExampleAnyAnyMap_Flip() { var m gmap.Map m.Sets(g.MapAnyAny{ "k1": "v1", @@ -97,7 +119,7 @@ func Example_flip() { // map[v1:k1 v2:k2] } -func Example_pop() { +func ExampleAnyAnyMap_Pop() { var m gmap.Map m.Sets(g.MapAnyAny{ "k1": "v1", @@ -115,30 +137,53 @@ func Example_pop() { // 1 } -func Example_filter() { - m1 := gmap.NewFrom(g.MapAnyAny{ +func ExampleAnyAnyMap_Pops() { + var m gmap.Map + m.Sets(g.MapAnyAny{ + "k1": "v1", + "k2": "v2", + "k3": "v3", + "k4": "v4", + }) + fmt.Println(m.Pop()) + fmt.Println(m.Pops(2)) + fmt.Println(m.Size()) + + // May Output: + // k1 v1 + // map[k2:v2 k4:v4] + // 1 +} + +func ExampleAnyAnyMap_FilterEmpty() { + m := gmap.NewFrom(g.MapAnyAny{ "k1": "", "k2": nil, "k3": 0, "k4": 1, }) - m2 := gmap.NewFrom(g.MapAnyAny{ - "k1": "", - "k2": nil, - "k3": 0, - "k4": 1, - }) - m1.FilterEmpty() - m2.FilterNil() - fmt.Println(m1.Map()) - fmt.Println(m2.Map()) + m.FilterEmpty() + fmt.Println(m.Map()) // May Output: // map[k4:1] +} + +func ExampleAnyAnyMap_FilterNil() { + m := gmap.NewFrom(g.MapAnyAny{ + "k1": "", + "k2": nil, + "k3": 0, + "k4": 1, + }) + m.FilterNil() + fmt.Println(m.Map()) + + // May Output: // map[k1: k3:0 k4:1] } -func Example_setIfNotExist() { +func ExampleAnyAnyMap_SetIfNotExist() { var m gmap.Map fmt.Println(m.SetIfNotExist("k1", "v1")) fmt.Println(m.SetIfNotExist("k1", "v1")) @@ -150,7 +195,7 @@ func Example_setIfNotExist() { // map[k1:v1] } -func Example_normalMerge() { +func ExampleAnyAnyMap_Merge() { var m1, m2 gmap.Map m1.Set("key1", "val1") m2.Set("key2", "val2") diff --git a/container/gset/gset_z_example_test.go b/container/gset/gset_z_example_any_test.go similarity index 52% rename from container/gset/gset_z_example_test.go rename to container/gset/gset_z_example_any_test.go index 02ec6ad9b..2fa07cd00 100644 --- a/container/gset/gset_z_example_test.go +++ b/container/gset/gset_z_example_any_test.go @@ -12,7 +12,7 @@ import ( "github.com/gogf/gf/frame/g" ) -func Example_intersectDiffUnionComplement() { +func ExampleSet_Intersect() { 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}) @@ -29,7 +29,58 @@ func Example_intersectDiffUnionComplement() { // [4 5 6 7] } -func Example_isSubsetOf() { +func ExampleSet_Diff() { + 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 ExampleSet_Union() { + 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 ExampleSet_Complement() { + 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 ExampleSet_IsSubsetOf() { var s1, s2 gset.Set s1.Add(g.Slice{1, 2, 3}...) s2.Add(g.Slice{2, 3}...) @@ -41,7 +92,7 @@ func Example_isSubsetOf() { // true } -func Example_addIfNotExist() { +func ExampleSet_AddIfNotExist() { var set gset.Set fmt.Println(set.AddIfNotExist(1)) fmt.Println(set.AddIfNotExist(1)) @@ -53,7 +104,7 @@ func Example_addIfNotExist() { // [1] } -func Example_pop() { +func ExampleSet_Pop() { var set gset.Set set.Add(1, 2, 3, 4) fmt.Println(set.Pop()) @@ -66,7 +117,20 @@ func Example_pop() { // 1 } -func Example_join() { +func ExampleSet_Pops() { + 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 ExampleSet_Join() { var set gset.Set set.Add("a", "b", "c", "d") fmt.Println(set.Join(",")) @@ -75,7 +139,7 @@ func Example_join() { // a,b,c,d } -func Example_contains() { +func ExampleSet_Contains() { var set gset.StrSet set.Add("a") fmt.Println(set.Contains("a")) @@ -88,7 +152,7 @@ func Example_contains() { // true } -func Example_Contains() { +func ExampleSet_ContainsI() { var set gset.StrSet set.Add("a") fmt.Println(set.Contains("a")) @@ -100,20 +164,3 @@ func Example_Contains() { // 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] -} diff --git a/container/gset/gset_z_example_str_test.go b/container/gset/gset_z_example_str_test.go new file mode 100644 index 000000000..f511e2ceb --- /dev/null +++ b/container/gset/gset_z_example_str_test.go @@ -0,0 +1,43 @@ +// 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 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 + // true +} + +func ExampleStrSet_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] +} diff --git a/frame/g/g_z_example_test.go b/frame/g/g_z_example_test.go new file mode 100644 index 000000000..414b41d88 --- /dev/null +++ b/frame/g/g_z_example_test.go @@ -0,0 +1,22 @@ +// 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 this file, +// You can obtain one at https://github.com/gogf/gf. + +package g_test + +import ( + "github.com/gogf/gf/frame/g" + "github.com/gogf/gf/net/ghttp" +) + +func ExampleServer() { + // A hello world example. + s := g.Server() + s.BindHandler("/", func(r *ghttp.Request) { + r.Response.Write("hello world") + }) + s.SetPort(8999) + s.Run() +} diff --git a/net/ghttp/ghttp_z_example_test.go b/net/ghttp/ghttp_z_example_test.go index 70dff71ae..0a7b3203e 100644 --- a/net/ghttp/ghttp_z_example_test.go +++ b/net/ghttp/ghttp_z_example_test.go @@ -11,7 +11,7 @@ import ( "github.com/gogf/gf/net/ghttp" ) -func Example_helloWorld() { +func ExampleGetServer() { s := g.Server() s.BindHandler("/", func(r *ghttp.Request) { r.Response.Write("hello world") @@ -20,7 +20,7 @@ func Example_helloWorld() { s.Run() } -func Example_clientDump() { +func ExampleClientResponse_RawDump() { response, err := g.Client().Get("https://goframe.org") if err != nil { panic(err)