链表遍历

  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. // [1,2,3,4,5,6,7,8,9]
  58. // 987654321
  59. // 123456789
  60. // 987654321
  61. // [1,2,3,4,5,M,7,8,9]
  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. 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. // [2,"b",9,3,4,5,6,7,1,"a",8]
  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. ```