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

排序数组

排序数组的方法与普通数组类似,但是带有自动排序功能及唯一性过滤功能。

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/garray"
  5. )
  6. func main () {
  7. // 自定义排序数组,降序排序(SortedIntArray管理的数据是升序)
  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. // 添加数据
  18. a.Add(2)
  19. a.Add(3)
  20. a.Add(1)
  21. fmt.Println(a.Slice())
  22. // 添加重复数据
  23. a.Add(3)
  24. fmt.Println(a.Slice())
  25. // 检索数据,返回最后对比的索引位置,检索结果
  26. // 检索结果:0: 匹配; <0:参数小于对比值; >0:参数大于对比值
  27. fmt.Println(a.Search(1))
  28. // 设置不可重复
  29. a.SetUnique(true)
  30. fmt.Println(a.Slice())
  31. a.Add(1)
  32. fmt.Println(a.Slice())
  33. }
  1. [3 2 1]
  2. [3 3 2 1]
  3. 3 0
  4. [3 2 1]
  5. [3 2 1]

Iterate*数组遍历

  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*数组项出栈

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

Contains/ContainsI包含判断

  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空值过滤

  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数组翻转

  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. // Reverse makes array with elements in reverse order.
  10. fmt.Println(array.Reverse().Slice())
  11. // Output:
  12. // [9 8 7 6 5 4 3 2 1]
  13. }
  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遍历修改

  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. return prefix + value
  14. fmt.Println(array.Slice())
  15. // Output:
  16. // [gf_user gf_user_detail]
  17. }

Join数组项串连

Chunk数组拆分

  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. }
  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序列化/反序列

garray模块下的所有容器类型均实现了标准库json数据格式的序列化/反序列化接口。

    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. }

    执行后,输出结果:

    1. {"Id":1,"Name":"john","Scores":[100,99,98]}
  1. ``` package main

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