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