链表遍历

  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. // 12345678910
  57. // 12345678910
  58. // 10987654321
  59. // [1,2,3,4,5,"M",7,8,9,10]
  60. }
  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. 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. }

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

JSON序列化/反序列

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