Ordinary Array

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/garray"
  5. )
  6. func main () {
  7. // Create a concurrency-safe int type array
  8. a := garray.NewIntArray(true)
  9. // Add data items
  10. for i := 0; i < 10; i++ {
  11. a.Append(i)
  12. }
  13. // Get the current array length
  14. fmt.Println(a.Len())
  15. // Get the current list of data items
  16. fmt.Println(a.Slice())
  17. // Get the item at a specified index
  18. fmt.Println(a.Get(6))
  19. // Insert a data item after a specified index
  20. a.InsertAfter(9, 11)
  21. // Insert a data item before a specified index
  22. a.InsertBefore(10, 10)
  23. fmt.Println(a.Slice())
  24. // Modify the data item at a specified index
  25. a.Set(0, 100)
  26. fmt.Println(a.Slice())
  27. // Search for a data item, return the found index position
  28. fmt.Println(a.Search(5))
  29. // Remove the data item at a specified index
  30. a.Remove(0)
  31. fmt.Println(a.Slice())
  32. // Concurrency-safe, write-lock operation
  33. a.LockFunc(func(array []int) {
  34. // Change the last item to 100
  35. array[len(array) - 1] = 100
  36. })
  37. // Concurrency-safe, read-lock operation
  38. a.RLockFunc(func(array []int) {
  39. fmt.Println(array[len(array) - 1])
  40. })
  41. // Clear the array
  42. fmt.Println(a.Slice())
  43. a.Clear()
  44. fmt.Println(a.Slice())
  45. }

After execution, the output is:

  1. [0 1 2 3 4 5 6 7 8 9]
  2. 6 true
  3. [0 1 2 3 4 5 6 7 8 9 10 11]
  4. [100 1 2 3 4 5 6 7 8 9 10 11]
  5. 5
  6. [1 2 3 4 5 6 7 8 9 10 11]
  7. 100
  8. [1 2 3 4 5 6 7 8 9 10 100]
  9. []

Sorted Array

The methods of sorted arrays are similar to ordinary arrays, but with automatic sorting and uniqueness filtering features.

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/garray"
  5. )
  6. func main () {
  7. // Customize sorted array, descending order (SortedIntArray manages data in ascending order)
  8. a := garray.NewSortedArray(func(v1, v2 interface{}) int {
  9. if v1.(int) < v2.(int) {
  10. return 1
  11. }
  12. if v1.(int) > v2.(int) {
  13. return -1
  14. }
  15. return 0
  16. })
  17. // Add data
  18. a.Add(2)
  19. a.Add(3)
  20. a.Add(1)
  21. fmt.Println(a.Slice())
  22. // Add duplicate data
  23. a.Add(3)
  24. fmt.Println(a.Slice())
  25. // Retrieve data, return the last comparison index position, retrieval result
  26. // Retrieval result: 0: match; <0: argument less than comparison value; >0: argument greater than comparison value
  27. fmt.Println(a.Search(1))
  28. // Set unique
  29. a.SetUnique(true)
  30. fmt.Println(a.Slice())
  31. a.Add(1)
  32. fmt.Println(a.Slice())
  33. }

After execution, the output is:

  1. [3 2 1]
  2. [3 3 2 1]
  3. 3 0
  4. [3 2 1]
  5. [3 2 1]

Iterate* Array Traversal

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/garray"
  5. "github.com/gogf/gf/v2/frame/g"
  6. )
  7. func main() {
  8. array := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
  9. // Iterator is alias of IteratorAsc, which iterates the array readonly in ascending order
  10. // with given callback function <f>.
  11. // If <f> returns true, then it continues iterating; or false to stop.
  12. array.Iterator(func(k int, v string) bool {
  13. fmt.Println(k, v)
  14. return true
  15. })
  16. // IteratorDesc iterates the array readonly in descending order with given callback function <f>.
  17. // If <f> returns true, then it continues iterating; or false to stop.
  18. array.IteratorDesc(func(k int, v string) bool {
  19. fmt.Println(k, v)
  20. return true
  21. })
  22. // Output:
  23. // 0 a
  24. // 1 b
  25. // 2 c
  26. // 2 c
  27. // 1 b
  28. // 0 a
  29. }

Pop* Array Item Pop

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/garray"
  5. )
  6. func main() {
  7. array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})
  8. // Any Pop* functions pick, delete and return the item from the array.
  9. fmt.Println(array.PopLeft())
  10. fmt.Println(array.PopLefts(2))
  11. fmt.Println(array.PopRight())
  12. fmt.Println(array.PopRights(2))
  13. // Output:
  14. // 1 true
  15. // [2 3]
  16. // 9 true
  17. // [7 8]
  18. }

Rand/PopRand Random Access/Pop

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/garray"
  5. "github.com/gogf/gf/v2/frame/g"
  6. )
  7. func main() {
  8. array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
  9. // Randomly retrieve and return 2 items from the array.
  10. // It does not delete the items from the array.
  11. fmt.Println(array.Rands(2))
  12. // Randomly pick and return one item from the array.
  13. // It deletes the picked item from the array.
  14. fmt.Println(array.PopRand())
  15. }

Contains/ContainsI Containment Check

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/garray"
  5. )
  6. func main() {
  7. var array garray.StrArray
  8. array.Append("a")
  9. fmt.Println(array.Contains("a"))
  10. fmt.Println(array.Contains("A"))
  11. fmt.Println(array.ContainsI("A"))
  12. // Output:
  13. // true
  14. // false
  15. // true
  16. }

FilterEmpty/FilterNil Null Value Filtering

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/garray"
  5. "github.com/gogf/gf/v2/frame/g"
  6. )
  7. func main() {
  8. array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
  9. array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
  10. fmt.Printf("%#v\n", array1.FilterNil().Slice())
  11. fmt.Printf("%#v\n", array2.FilterEmpty().Slice())
  12. // Output:
  13. // []interface {}{0, 1, 2, "", []interface {}{}, "john"}
  14. // []interface {}{1, 2, "john"}
  15. }

Reverse Array Reversal

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/garray"
  5. "github.com/gogf/gf/v2/frame/g"
  6. )
  7. func main() {
  8. array := garray.NewFrom(g.Slice{1, 5, 3})
  9. // Reverse makes array with elements reverse.
  10. fmt.Println(array.Reverse().Slice())
  11. // Output:
  12. // [3 5 1]
  13. }

Shuffle Random Sorting

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/garray"
  5. "github.com/gogf/gf/v2/frame/g"
  6. )
  7. func main() {
  8. array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
  9. // Shuffle randomly shuffles the array.
  10. fmt.Println(array.Shuffle().Slice())
  11. }

Walk Traversal Modification

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/garray"
  5. "github.com/gogf/gf/v2/frame/g"
  6. )
  7. func main() {
  8. var array garray.StrArray
  9. tables := g.SliceStr{"user", "user_detail"}
  10. prefix := "gf_"
  11. array.Append(tables...)
  12. // Add prefix for given table names.
  13. array.Walk(func(value string) string {
  14. return prefix + value
  15. })
  16. fmt.Println(array.Slice())
  17. // Output:
  18. // [gf_user gf_user_detail]
  19. }

Join Array Item Join

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/garray"
  5. "github.com/gogf/gf/v2/frame/g"
  6. )
  7. func main() {
  8. array := garray.NewFrom(g.Slice{"a", "b", "c", "d"})
  9. fmt.Println(array.Join(","))
  10. // Output:
  11. // a,b,c,d
  12. }

Chunk Array Chunking

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/garray"
  5. "github.com/gogf/gf/v2/frame/g"
  6. )
  7. func main() {
  8. array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
  9. // Chunk splits an array into multiple arrays,
  10. // the size of each array is determined by <size>.
  11. // The last chunk may contain less than size elements.
  12. fmt.Println(array.Chunk(2))
  13. // Output:
  14. // [[1 2] [3 4] [5 6] [7 8] [9]]
  15. }

Merge Array Merging

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/garray"
  5. "github.com/gogf/gf/v2/frame/g"
  6. )
  7. func main() {
  8. array1 := garray.NewFrom(g.Slice{1, 2})
  9. array2 := garray.NewFrom(g.Slice{3, 4})
  10. slice1 := g.Slice{5, 6}
  11. slice2 := []int{7, 8}
  12. slice3 := []string{"9", "0"}
  13. fmt.Println(array1.Slice())
  14. array1.Merge(array1)
  15. array1.Merge(array2)
  16. array1.Merge(slice1)
  17. array1.Merge(slice2)
  18. array1.Merge(slice3)
  19. fmt.Println(array1.Slice())
  20. // Output:
  21. // [1 2]
  22. // [1 2 1 2 3 4 5 6 7 8 9 0]
  23. }

JSON Serialization/Deserialization

All container types under the garray module implement the standard library json data format serialization/deserialization interface.

  1. Marshal
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gogf/gf/v2/container/garray"
  6. )
  7. func main() {
  8. type Student struct {
  9. Id int
  10. Name string
  11. Scores *garray.IntArray
  12. }
  13. s := Student{
  14. Id: 1,
  15. Name: "john",
  16. Scores: garray.NewIntArrayFrom([]int{100, 99, 98}),
  17. }
  18. b, _ := json.Marshal(s)
  19. fmt.Println(string(b))
  20. }

After execution, the output is:

  1. {"Id":1,"Name":"john","Scores":[100,99,98]}
  1. Unmarshal
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gogf/gf/v2/container/garray"
  6. )
  7. func main() {
  8. b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
  9. type Student struct {
  10. Id int
  11. Name string
  12. Scores *garray.IntArray
  13. }
  14. s := Student{}
  15. json.Unmarshal(b, &s)
  16. fmt.Println(s)
  17. }

After execution, the output is:

  1. {1 john [100,99,98]}