Repository navigation
Expand file tree
/
Copy pathgqueue_z_example_test.go
More file actions
133 lines (104 loc) · 1.98 KB
/
Copy pathgqueue_z_example_test.go
File metadata and controls
133 lines (104 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gqueue_test
import (
"context"
"fmt"
"time"
"github.com/gogf/gf/v2/container/gqueue"
"github.com/gogf/gf/v2/os/gtimer"
)
func ExampleNew() {
n := 10
q := gqueue.New()
// Producer
for i := 0; i < n; i++ {
q.Push(i)
}
// Close the queue in three seconds.
gtimer.SetTimeout(context.Background(), time.Second*3, func(ctx context.Context) {
q.Close()
})
// The consumer constantly reads the queue data.
// If there is no data in the queue, it will block.
// The queue is read using the queue.C property exposed
// by the queue object and the selectIO multiplexing syntax
// example:
// for {
// select {
// case v := <-queue.C:
// if v != nil {
// fmt.Println(v)
// } else {
// return
// }
// }
// }
for {
if v := q.Pop(); v != nil {
fmt.Print(v)
} else {
break
}
}
// Output:
// 0123456789
}
func ExampleQueue_Push() {
q := gqueue.New()
for i := 0; i < 10; i++ {
q.Push(i)
}
fmt.Println(q.Pop())
fmt.Println(q.Pop())
fmt.Println(q.Pop())
// Output:
// 0
// 1
// 2
}
func ExampleQueue_Pop() {
q := gqueue.New()
for i := 0; i < 10; i++ {
q.Push(i)
}
fmt.Println(q.Pop())
q.Close()
fmt.Println(q.Pop())
// Output:
// 0
// <nil>
}
func ExampleQueue_Close() {
q := gqueue.New()
for i := 0; i < 10; i++ {
q.Push(i)
}
time.Sleep(time.Millisecond)
q.Close()
fmt.Println(q.Len())
fmt.Println(q.Pop())
// May Output:
// 0
// <nil>
}
func ExampleQueue_Len() {
q := gqueue.New()
q.Push(1)
q.Push(2)
fmt.Println(q.Len())
// May Output:
// 2
}
func ExampleQueue_Size() {
q := gqueue.New()
q.Push(1)
q.Push(2)
// Size is alias of Len.
fmt.Println(q.Size())
// May Output:
// 2
}