Basic Usage

Using Queue.Pop

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/gogf/gf/v2/os/gtimer"
  6. "github.com/gogf/gf/v2/container/gqueue"
  7. )
  8. func main() {
  9. q := gqueue.New()
  10. // Data producer, writes data to the queue every second
  11. gtimer.SetInterval(time.Second, func() {
  12. v := gtime.Now().String()
  13. q.Push(v)
  14. fmt.Println("Push:", v)
  15. })
  16. // Close the queue after 3 seconds
  17. gtimer.SetTimeout(3*time.Second, func() {
  18. q.Close()
  19. })
  20. // Consumer, continuously reads queue data and outputs to the terminal
  21. for {
  22. if v := q.Pop(); v != nil {
  23. fmt.Println(" Pop:", v)
  24. } else {
  25. break
  26. }
  27. }
  28. // The program exits immediately when the queue is closed at the third second, so only 2 seconds of data will be printed in the result. After execution, the output will be:
  29. // Output:
  30. // Push: 2021-09-07 14:03:00
  31. // Pop: 2021-09-07 14:03:00
  32. // Push: 2021-09-07 14:03:01
  33. // Pop: 2021-09-07 14:03:01
  34. }

Using Queue.C

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "time"
  6. _ "github.com/gogf/gf/contrib/drivers/mysql/v2"
  7. "github.com/gogf/gf/v2/container/gqueue"
  8. "github.com/gogf/gf/v2/os/gctx"
  9. "github.com/gogf/gf/v2/os/gtime"
  10. "github.com/gogf/gf/v2/os/gtimer"
  11. )
  12. func main() {
  13. queue := gqueue.New()
  14. gtimer.AddTimes(gctx.GetInitCtx(), time.Second, 3, func(ctx context.Context) {
  15. queue.Push(gtime.Now().String())
  16. })
  17. for {
  18. select {
  19. case queueItem := <-queue.C:
  20. fmt.Println(queueItem)
  21. case <-time.After(3 * time.Second):
  22. fmt.Println("timeout, exit loop")
  23. return
  24. }
  25. }
  26. }

Enqueue/Dequeue Elements

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/gogf/gf/v2/os/gtimer"
  6. "github.com/gogf/gf/v2/container/gqueue"
  7. )
  8. func main() {
  9. q := gqueue.New()
  10. for i := 0; i < 10; i++ {
  11. q.Push(i)
  12. }
  13. fmt.Println(q.Pop())
  14. fmt.Println(q.Pop())
  15. fmt.Println(q.Pop())
  16. // Output:
  17. // 0
  18. // 1
  19. // 2
  20. }

Queue Length

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/gogf/gf/v2/os/gtimer"
  6. "github.com/gogf/gf/v2/container/gqueue"
  7. )
  8. func main() {
  9. q := gqueue.New()
  10. q.Push(1)
  11. q.Push(2)
  12. fmt.Println(q.Len())
  13. // size is an alias for the len method
  14. fmt.Println(q.Size())
  15. // May Output:
  16. // 2
  17. // 2
  18. }

Queue Close

  1. package main
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/gogf/gf/v2/os/gtimer"
  6. "github.com/gogf/gf/v2/container/gqueue"
  7. )
  8. func main() {
  9. q := gqueue.New()
  10. for i := 0; i < 10; i++ {
  11. q.Push(i)
  12. }
  13. fmt.Println(q.Pop())
  14. q.Close()
  15. fmt.Println(q.Pop())
  16. fmt.Println(q.Len())
  17. // Output:
  18. // 0
  19. // <nil>
  20. // 0
  21. }

gqueue and glist

The underlying implementation of gqueue is based on the glist linked list, which provides dynamic size characteristics. Writing data when the queue is full or reading data when the queue is empty will result in blocking.

glist is a concurrent-safe linked list and can behave like a normal list when the concurrent-safe feature is turned off, without encountering blocking during data storage and retrieval.