Files
gf/.example/container/gqueue/gqueue3.go

31 lines
487 B
Go
Raw Normal View History

2019-01-23 21:30:02 +08:00
package main
import (
2019-04-03 00:03:46 +08:00
"fmt"
"time"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/container/gqueue"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/os/gtimer"
2019-01-23 21:30:02 +08:00
)
func main() {
2019-04-03 00:03:46 +08:00
queue := gqueue.New()
// 数据生产者每隔1秒往队列写数据
gtimer.SetInterval(time.Second, func() {
queue.Push(gtime.Now().String())
})
2019-01-23 21:30:02 +08:00
2019-04-03 00:03:46 +08:00
// 消费者,不停读取队列数据并输出到终端
for {
select {
2019-06-12 21:06:57 +08:00
case v := <-queue.C:
if v != nil {
fmt.Println(v)
} else {
return
}
2019-04-03 00:03:46 +08:00
}
}
2019-01-23 21:30:02 +08:00
}