gf/.example/container/gqueue/gqueue.go

35 lines
577 B
Go
Raw Normal View History

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"
)
func main() {
2019-04-03 00:03:46 +08:00
q := gqueue.New()
// 数据生产者每隔1秒往队列写数据
gtimer.SetInterval(time.Second, func() {
v := gtime.Now().String()
q.Push(v)
fmt.Println("Push:", v)
})
2019-04-03 00:03:46 +08:00
// 3秒后关闭队列
gtimer.SetTimeout(3*time.Second, func() {
q.Close()
})
2019-04-03 00:03:46 +08:00
// 消费者,不停读取队列数据并输出到终端
for {
if v := q.Pop(); v != nil {
fmt.Println(" Pop:", v)
} else {
break
}
}
}