github.com/gogf/gf@v1.16.9/.example/container/gqueue/gqueue3.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/gogf/gf/container/gqueue"
     8  	"github.com/gogf/gf/os/gtime"
     9  	"github.com/gogf/gf/os/gtimer"
    10  )
    11  
    12  func main() {
    13  	queue := gqueue.New()
    14  	// 数据生产者,每隔1秒往队列写数据
    15  	gtimer.SetInterval(time.Second, func() {
    16  		queue.Push(gtime.Now().String())
    17  	})
    18  
    19  	// 消费者,不停读取队列数据并输出到终端
    20  	for {
    21  		select {
    22  		case v := <-queue.C:
    23  			if v != nil {
    24  				fmt.Println(v)
    25  			} else {
    26  				return
    27  			}
    28  		}
    29  	}
    30  }