github.com/gogf/gf/v2@v2.7.4/container/garray/garray_z_example_normal_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 this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package garray_test
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/gogf/gf/v2/internal/empty"
    13  
    14  	"github.com/gogf/gf/v2/container/garray"
    15  	"github.com/gogf/gf/v2/frame/g"
    16  )
    17  
    18  func ExampleNew() {
    19  	// A normal array.
    20  	a := garray.New()
    21  
    22  	// Adding items.
    23  	for i := 0; i < 10; i++ {
    24  		a.Append(i)
    25  	}
    26  
    27  	// Print the array length.
    28  	fmt.Println(a.Len())
    29  
    30  	// Print the array items.
    31  	fmt.Println(a.Slice())
    32  
    33  	// Retrieve item by index.
    34  	fmt.Println(a.Get(6))
    35  
    36  	// Check item existence.
    37  	fmt.Println(a.Contains(6))
    38  	fmt.Println(a.Contains(100))
    39  
    40  	// Insert item before specified index.
    41  	a.InsertAfter(9, 11)
    42  	// Insert item after specified index.
    43  	a.InsertBefore(10, 10)
    44  
    45  	fmt.Println(a.Slice())
    46  
    47  	// Modify item by index.
    48  	a.Set(0, 100)
    49  	fmt.Println(a.Slice())
    50  
    51  	fmt.Println(a.At(0))
    52  
    53  	// Search item and return its index.
    54  	fmt.Println(a.Search(5))
    55  
    56  	// Remove item by index.
    57  	a.Remove(0)
    58  	fmt.Println(a.Slice())
    59  
    60  	// Empty the array, removes all items of it.
    61  	fmt.Println(a.Slice())
    62  	a.Clear()
    63  	fmt.Println(a.Slice())
    64  
    65  	// Output:
    66  	// 10
    67  	// [0 1 2 3 4 5 6 7 8 9]
    68  	// 6 true
    69  	// true
    70  	// false
    71  	// [0 1 2 3 4 5 6 7 8 9 10 11]
    72  	// [100 1 2 3 4 5 6 7 8 9 10 11]
    73  	// 100
    74  	// 5
    75  	// [1 2 3 4 5 6 7 8 9 10 11]
    76  	// [1 2 3 4 5 6 7 8 9 10 11]
    77  	// []
    78  }
    79  
    80  func ExampleArray_Iterator() {
    81  	array := garray.NewArrayFrom(g.Slice{"a", "b", "c"})
    82  	// Iterator is alias of IteratorAsc, which iterates the array readonly in ascending order
    83  	//  with given callback function `f`.
    84  	// If `f` returns true, then it continues iterating; or false to stop.
    85  	array.Iterator(func(k int, v interface{}) bool {
    86  		fmt.Println(k, v)
    87  		return true
    88  	})
    89  	// IteratorDesc iterates the array readonly in descending order with given callback function `f`.
    90  	// If `f` returns true, then it continues iterating; or false to stop.
    91  	array.IteratorDesc(func(k int, v interface{}) bool {
    92  		fmt.Println(k, v)
    93  		return true
    94  	})
    95  
    96  	// Output:
    97  	// 0 a
    98  	// 1 b
    99  	// 2 c
   100  	// 2 c
   101  	// 1 b
   102  	// 0 a
   103  }
   104  
   105  func ExampleArray_Reverse() {
   106  	array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
   107  
   108  	// Reverse makes array with elements in reverse order.
   109  	fmt.Println(array.Reverse().Slice())
   110  
   111  	// Output:
   112  	// [9 8 7 6 5 4 3 2 1]
   113  }
   114  
   115  func ExampleArray_Shuffle() {
   116  	array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
   117  
   118  	// Shuffle randomly shuffles the array.
   119  	fmt.Println(array.Shuffle().Slice())
   120  }
   121  
   122  func ExampleArray_Rands() {
   123  	array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
   124  
   125  	// Randomly retrieve and return 2 items from the array.
   126  	// It does not delete the items from array.
   127  	fmt.Println(array.Rands(2))
   128  
   129  	// Randomly pick and return one item from the array.
   130  	// It deletes the picked up item from array.
   131  	fmt.Println(array.PopRand())
   132  }
   133  
   134  func ExampleArray_PopRand() {
   135  	array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
   136  
   137  	// Randomly retrieve and return 2 items from the array.
   138  	// It does not delete the items from array.
   139  	fmt.Println(array.Rands(2))
   140  
   141  	// Randomly pick and return one item from the array.
   142  	// It deletes the picked up item from array.
   143  	fmt.Println(array.PopRand())
   144  }
   145  
   146  func ExampleArray_Join() {
   147  	array := garray.NewFrom(g.Slice{"a", "b", "c", "d"})
   148  	fmt.Println(array.Join(","))
   149  
   150  	// Output:
   151  	// a,b,c,d
   152  }
   153  
   154  func ExampleArray_Chunk() {
   155  	array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
   156  
   157  	// Chunk splits an array into multiple arrays,
   158  	// the size of each array is determined by `size`.
   159  	// The last chunk may contain less than size elements.
   160  	fmt.Println(array.Chunk(2))
   161  
   162  	// Output:
   163  	// [[1 2] [3 4] [5 6] [7 8] [9]]
   164  }
   165  
   166  func ExampleArray_PopLeft() {
   167  	array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})
   168  
   169  	// Any Pop* functions pick, delete and return the item from array.
   170  
   171  	fmt.Println(array.PopLeft())
   172  	fmt.Println(array.PopLefts(2))
   173  	fmt.Println(array.PopRight())
   174  	fmt.Println(array.PopRights(2))
   175  
   176  	// Output:
   177  	// 1 true
   178  	// [2 3]
   179  	// 9 true
   180  	// [7 8]
   181  }
   182  
   183  func ExampleArray_PopLefts() {
   184  	array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})
   185  
   186  	// Any Pop* functions pick, delete and return the item from array.
   187  
   188  	fmt.Println(array.PopLeft())
   189  	fmt.Println(array.PopLefts(2))
   190  	fmt.Println(array.PopRight())
   191  	fmt.Println(array.PopRights(2))
   192  
   193  	// Output:
   194  	// 1 true
   195  	// [2 3]
   196  	// 9 true
   197  	// [7 8]
   198  }
   199  
   200  func ExampleArray_PopRight() {
   201  	array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})
   202  
   203  	// Any Pop* functions pick, delete and return the item from array.
   204  
   205  	fmt.Println(array.PopLeft())
   206  	fmt.Println(array.PopLefts(2))
   207  	fmt.Println(array.PopRight())
   208  	fmt.Println(array.PopRights(2))
   209  
   210  	// Output:
   211  	// 1 true
   212  	// [2 3]
   213  	// 9 true
   214  	// [7 8]
   215  }
   216  
   217  func ExampleArray_PopRights() {
   218  	array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})
   219  
   220  	// Any Pop* functions pick, delete and return the item from array.
   221  
   222  	fmt.Println(array.PopLeft())
   223  	fmt.Println(array.PopLefts(2))
   224  	fmt.Println(array.PopRight())
   225  	fmt.Println(array.PopRights(2))
   226  
   227  	// Output:
   228  	// 1 true
   229  	// [2 3]
   230  	// 9 true
   231  	// [7 8]
   232  }
   233  
   234  func ExampleArray_Contains() {
   235  	var array garray.StrArray
   236  	array.Append("a")
   237  	fmt.Println(array.Contains("a"))
   238  	fmt.Println(array.Contains("A"))
   239  	fmt.Println(array.ContainsI("A"))
   240  
   241  	// Output:
   242  	// true
   243  	// false
   244  	// true
   245  }
   246  
   247  func ExampleArray_Merge() {
   248  	array1 := garray.NewFrom(g.Slice{1, 2})
   249  	array2 := garray.NewFrom(g.Slice{3, 4})
   250  	slice1 := g.Slice{5, 6}
   251  	slice2 := []int{7, 8}
   252  	slice3 := []string{"9", "0"}
   253  	fmt.Println(array1.Slice())
   254  	array1.Merge(array1)
   255  	array1.Merge(array2)
   256  	array1.Merge(slice1)
   257  	array1.Merge(slice2)
   258  	array1.Merge(slice3)
   259  	fmt.Println(array1.Slice())
   260  
   261  	// Output:
   262  	// [1 2]
   263  	// [1 2 1 2 3 4 5 6 7 8 9 0]
   264  }
   265  
   266  func ExampleArray_Filter() {
   267  	array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
   268  	array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
   269  	fmt.Printf("%#v\n", array1.Filter(func(index int, value interface{}) bool {
   270  		return empty.IsNil(value)
   271  	}).Slice())
   272  	fmt.Printf("%#v\n", array2.Filter(func(index int, value interface{}) bool {
   273  		return empty.IsEmpty(value)
   274  	}).Slice())
   275  
   276  	// Output:
   277  	// []interface {}{0, 1, 2, "", []interface {}{}, "john"}
   278  	// []interface {}{1, 2, "john"}
   279  }
   280  
   281  func ExampleArray_FilterEmpty() {
   282  	array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
   283  	array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
   284  	fmt.Printf("%#v\n", array1.FilterNil().Slice())
   285  	fmt.Printf("%#v\n", array2.FilterEmpty().Slice())
   286  
   287  	// Output:
   288  	// []interface {}{0, 1, 2, "", []interface {}{}, "john"}
   289  	// []interface {}{1, 2, "john"}
   290  }
   291  
   292  func ExampleArray_FilterNil() {
   293  	array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
   294  	array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
   295  	fmt.Printf("%#v\n", array1.FilterNil().Slice())
   296  	fmt.Printf("%#v\n", array2.FilterEmpty().Slice())
   297  
   298  	// Output:
   299  	// []interface {}{0, 1, 2, "", []interface {}{}, "john"}
   300  	// []interface {}{1, 2, "john"}
   301  }