普通数组

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/garray"
  5. )
  6. func main () {
  7. // 创建并发安全的int类型数组
  8. a := garray.NewIntArray(true)
  9. // 添加数据项
  10. for i := 0; i < 10; i++ {
  11. a.Append(i)
  12. }
  13. // 获取当前数组长度
  14. fmt.Println(a.Len())
  15. // 获取当前数据项列表
  16. fmt.Println(a.Slice())
  17. // 获取指定索引项
  18. fmt.Println(a.Get(6))
  19. // 在指定索引后插入数据项
  20. a.InsertAfter(9, 11)
  21. // 在指定索引前插入数据项
  22. a.InsertBefore(10, 10)
  23. fmt.Println(a.Slice())
  24. // 修改指定索引的数据项
  25. a.Set(0, 100)
  26. fmt.Println(a.Slice())
  27. // 搜索数据项,返回搜索到的索引位置
  28. fmt.Println(a.Search(5))
  29. // 删除指定索引的数据项
  30. a.Remove(0)
  31. fmt.Println(a.Slice())
  32. // 并发安全,写锁操作
  33. a.LockFunc(func(array []int) {
  34. // 将末尾项改为100
  35. array[len(array) - 1] = 100
  36. })
  37. // 并发安全,读锁操作
  38. a.RLockFunc(func(array []int) {
  39. fmt.Println(array[len(array) - 1])
  40. })
  41. // 清空数组
  42. fmt.Println(a.Slice())
  43. a.Clear()
  44. fmt.Println(a.Slice())
  45. }

执行后,输出结果为:

  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. 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 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数组项随机获取/出栈

  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 array.
  11. fmt.Println(array.Rands(2))
  12. // Randomly pick and return one item from the array.
  13. // It deletes the picked up item from array.
  14. fmt.Println(array.PopRand())
  15. }

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

Shuffle随机排序

  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. 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数组项串连

  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数组拆分

  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数组合并

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

    执行后,输出结果:

    1. {"Id":1,"Name":"john","Scores":[100,99,98]}
  2. Unmarshal

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