基础使用

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

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. l.PushBacks(g.Slice{7, 8})
  14. fmt.Println(l)
  15. l.PushFronts(g.Slice{-1, -2})
  16. fmt.Println(l)
  17. l.PushFrontList(glist.NewFrom(g.Slice{"a", "b", "c"}))
  18. l.PushBackList(glist.NewFrom(g.Slice{"d", "e", "f"}))
  19. fmt.Println(l)
  20. // Output:
  21. // [1,2,3,4,5,6]
  22. // [0,1,2,3,4,5,6]
  23. // [0,1,2,3,4,5,6,7,8]
  24. // [-2,-1,0,1,2,3,4,5,6,7,8]
  25. // ["a","b","c",-2,-1,0,1,2,3,4,5,6,7,8,"d","e","f"]
  26. }

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())
  10. l.MoveToFront(l.Back().Prev())
  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.Remove(l.Front()))
  10. fmt.Println(l)
  11. l.Removes([]*glist.Element{l.Front(), l.Front().Next()})
  12. fmt.Println(l)
  13. l.RemoveAll()
  14. fmt.Println(l)
  15. // Output:
  16. // [0,1,2,3,4,5,6,7,8,9]
  17. // [1,2,3,4,5,6,7,8,9]
  18. // [3,4,5,6,7,8,9]
  19. // []
  20. }

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