Basic Usage

  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) // insert value from the back
  11. l.PushBack(2) // insert value from the back
  12. e := l.PushFront(0) // insert value from the front
  13. // Insert
  14. l.InsertBefore(e, -1) // insert value before 0
  15. l.InsertAfter(e, "a") // insert value after 0
  16. fmt.Println(l)
  17. // Pop After popping, remove from list
  18. fmt.Println(l.PopFront()) // pop from the front, return the popped value
  19. fmt.Println(l.PopBack()) // pop from the back
  20. fmt.Println(l)
  21. // All
  22. fmt.Println(l.FrontAll()) // return a copy in order
  23. fmt.Println(l.BackAll()) // return a copy in reverse order
  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. }

Linked List Traversal

In this example, we will traverse a concurrent-safe linked list using read lock and write lock, implemented by RLockFunc and LockFunc respectively. After execution, the output is:

  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* Element 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. // Positive numbers push from the right
  14. l.PushBacks(g.Slice{7, 8})
  15. fmt.Println(l)
  16. // Negative numbers push from the left
  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* Element 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* Element Movement and Insertion

  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()) // move the first element (1) to the most right [2,3,4,5,6,7,8,9,1]
  10. l.MoveToFront(l.Back().Prev()) // move the element before the last (9) to the most left [9,2,3,4,5,6,7,8,1]
  11. fmt.Println(l)
  12. // Move 2 before the first element of the stack
  13. l.MoveBefore(l.Front().Next(), l.Front())
  14. // Move 8 after the last element of the stack
  15. l.MoveAfter(l.Back().Prev(), l.Back())
  16. fmt.Println(l)
  17. // Insert new element before the last element of the stack
  18. l.InsertBefore(l.Back(), "a")
  19. // Insert new element after the first element of the stack
  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 Element Concatenation

  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* Element Removal

  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. // []
  22. }

JSON Serialization/Deserialization

The glist container implements the standard library json data format’s serialization/deserialization interface.

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