基础使用

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/glist"
  5. )
  6. func main() {
  7. // Not concurrent-safe in default.
  8. l := glist.New()
  9. // Push
  10. l.PushBack(1) //从后面插入值
  11. l.PushBack(2) //从后面插入值
  12. e := l.PushFront(0) //从前面插入值
  13. // Insert
  14. l.InsertBefore(e, -1) //从0的前面插入值
  15. l.InsertAfter(e, "a") //从0的后面插入值
  16. fmt.Println(l)
  17. // Pop Pop 出栈后,从list里移除
  18. fmt.Println(l.PopFront()) // 从前面出栈,返回出栈的值
  19. fmt.Println(l.PopBack()) //从后面出栈
  20. fmt.Println(l)
  21. // All
  22. fmt.Println(l.FrontAll()) //正序返回一个复本
  23. fmt.Println(l.BackAll()) //逆序返回一个复本
  24. // Output:
  25. // [-1,0,"a",1,2]
  26. // -1
  27. // 2
  28. // [0,"a",1]
  29. // [0 "a" 1]
  30. // [1 "a" 0]
  31. }

链表遍历

该示例中我们将通过读锁和写锁遍历一个并发安全的链表,分别通过RLockFuncLockFunc实现。执行后,输出结果为:

  1. package main
  2. import (
  3. "container/list"
  4. "fmt"
  5. "github.com/gogf/gf/v2/container/garray"
  6. "github.com/gogf/gf/v2/container/glist"
  7. )
  8. func main() {
  9. // concurrent-safe list.
  10. l := glist.NewFrom(garray.NewArrayRange(1, 9, 1).Slice(), true)
  11. fmt.Println(l)
  12. // iterate reading from head.
  13. l.RLockFunc(func(list *list.List) {
  14. length := list.Len()
  15. if length > 0 {
  16. for i, e := 0, list.Front(); i < length; i, e = i+1, e.Next() {
  17. fmt.Print(e.Value)
  18. }
  19. }
  20. })
  21. fmt.Println()
  22. // iterate reading from tail.
  23. l.RLockFunc(func(list *list.List) {
  24. length := list.Len()
  25. if length > 0 {
  26. for i, e := 0, list.Back(); i < length; i, e = i+1, e.Prev() {
  27. fmt.Print(e.Value)
  28. }
  29. }
  30. })
  31. fmt.Println()
  32. // iterate reading from head using IteratorAsc.
  33. l.IteratorAsc(func(e *glist.Element) bool {
  34. fmt.Print(e.Value)
  35. return true
  36. })
  37. fmt.Println()
  38. // iterate reading from tail using IteratorDesc.
  39. l.IteratorDesc(func(e *glist.Element) bool {
  40. fmt.Print(e.Value)
  41. return true
  42. })
  43. fmt.Println()
  44. // iterate writing from head.
  45. l.LockFunc(func(list *list.List) {
  46. length := list.Len()
  47. if length > 0 {
  48. for i, e := 0, list.Front(); i < length; i, e = i+1, e.Next() {
  49. if e.Value == 6 {
  50. e.Value = "M"
  51. break
  52. }
  53. }
  54. }
  55. })
  56. fmt.Println(l)
  57. // Output:
  58. // [1,2,3,4,5,6,7,8,9]
  59. // 123456789
  60. // 987654321
  61. // 123456789
  62. // 987654321
  63. // [1,2,3,4,5,M,7,8,9]

Push*元素项入栈

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/glist"
  5. "github.com/gogf/gf/v2/frame/g"
  6. )
  7. func main() {
  8. l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5})
  9. l.PushBack(6)
  10. fmt.Println(l)
  11. l.PushFront(0)
  12. fmt.Println(l)
  13. // 正数从右边入栈
  14. l.PushBacks(g.Slice{7, 8})
  15. fmt.Println(l)
  16. // 负数从左边入栈
  17. l.PushFronts(g.Slice{-1, -2})
  18. fmt.Println(l)
  19. l.PushFrontList(glist.NewFrom(g.Slice{"a", "b", "c"}))
  20. l.PushBackList(glist.NewFrom(g.Slice{"d", "e", "f"}))
  21. fmt.Println(l)
  22. // Output:
  23. // [1,2,3,4,5,6]
  24. // [0,1,2,3,4,5,6]
  25. // [0,1,2,3,4,5,6,7,8]
  26. // [-2,-1,0,1,2,3,4,5,6,7,8]
  27. // ["a","b","c",-2,-1,0,1,2,3,4,5,6,7,8,"d","e","f"]
  28. }

Pop*元素项出栈

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/glist"
  5. "github.com/gogf/gf/v2/frame/g"
  6. )
  7. func main() {
  8. l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
  9. fmt.Println(l.PopBack())
  10. fmt.Println(l.PopBacks(2))
  11. fmt.Println(l.PopFront())
  12. fmt.Println(l.PopFronts(2))
  13. fmt.Println(glist.NewFrom(g.Slice{"a", "b", "c", "d"}).PopFrontAll())
  14. fmt.Println(glist.NewFrom(g.Slice{"a", "b", "c", "d"}).PopBackAll())
  15. // Output:
  16. // 9
  17. // [8 7]
  18. // 1
  19. // [2 3]
  20. // [4,5,6]
  21. // [a b c d]
  22. // [d c b a]
  23. }

Move*/Insert*元素项移动、插入

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/glist"
  5. "github.com/gogf/gf/v2/frame/g"
  6. )
  7. func main() {
  8. l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
  9. l.MoveToBack(l.Front()) //将第一个元素(1)移动最右边 [2,3,4,5,6,7,8,9,1]
  10. l.MoveToFront(l.Back().Prev()) //将最后一项的前一个元素(9)移动最左边 [9,2,3,4,5,6,7,8,1]
  11. fmt.Println(l)
  12. // 将2到栈首元素的前面
  13. l.MoveBefore(l.Front().Next(), l.Front())
  14. // 将8到栈尾元素的后面
  15. l.MoveAfter(l.Back().Prev(), l.Back())
  16. fmt.Println(l)
  17. // 在栈尾元素前插入新元素
  18. l.InsertBefore(l.Back(), "a")
  19. // 在栈首元素后插入新元素
  20. l.InsertAfter(l.Front(), "b")
  21. // Output:
  22. // [9,2,3,4,5,6,7,8,1]
  23. // [2,9,3,4,5,6,7,1,8]
  24. // [2,"b",9,3,4,5,6,7,1,"a",8]
  25. }

Join元素项串连

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

Remove*元素项移除

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/glist"
  5. "github.com/gogf/gf/v2/frame/g"
  6. )
  7. func main() {
  8. l := glist.NewFrom(g.Slice{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
  9. fmt.Println(l)
  10. fmt.Println(l.Remove(l.Front()))
  11. fmt.Println(l)
  12. l.Removes([]*glist.Element{l.Front(), l.Front().Next()})
  13. fmt.Println(l)
  14. l.RemoveAll()
  15. fmt.Println(l)
  16. // Output:
  17. // [0,1,2,3,4,5,6,7,8,9]
  18. // 0
  19. // [1,2,3,4,5,6,7,8,9]
  20. // [3,4,5,6,7,8,9]
  21. // [] }

JSON序列化/反序列

glist容器实现了标准库json数据格式的序列化/反序列化接口。

  • Marshal

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

    ``` package main

  1. import (
  2. "encoding/json"
  3. "fmt"
  4. "github.com/gogf/gf/v2/container/glist"
  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 *glist.List
  12. }
  13. s := Student{}
  14. json.Unmarshal(b, &s)
  15. fmt.Println(s)
  16. // Output:
  17. // {1 john [100,99,98]}
  18. }
  19. ```