mirror of
https://gitee.com/johng/gf.git
synced 2024-12-05 05:37:55 +08:00
35 lines
577 B
Go
35 lines
577 B
Go
package main
|
||
|
||
import (
|
||
"fmt"
|
||
"time"
|
||
|
||
"github.com/gogf/gf/container/gqueue"
|
||
"github.com/gogf/gf/os/gtime"
|
||
"github.com/gogf/gf/os/gtimer"
|
||
)
|
||
|
||
func main() {
|
||
q := gqueue.New()
|
||
// 数据生产者,每隔1秒往队列写数据
|
||
gtimer.SetInterval(time.Second, func() {
|
||
v := gtime.Now().String()
|
||
q.Push(v)
|
||
fmt.Println("Push:", v)
|
||
})
|
||
|
||
// 3秒后关闭队列
|
||
gtimer.SetTimeout(3*time.Second, func() {
|
||
q.Close()
|
||
})
|
||
|
||
// 消费者,不停读取队列数据并输出到终端
|
||
for {
|
||
if v := q.Pop(); v != nil {
|
||
fmt.Println(" Pop:", v)
|
||
} else {
|
||
break
|
||
}
|
||
}
|
||
}
|