forked from beeker1121/goque
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_queue_test.go
51 lines (41 loc) · 1012 Bytes
/
example_queue_test.go
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
package goque_test
import (
"fmt"
"github.com/kubeshark/goque"
)
// ExampleQueue demonstrates the implementation of a Goque queue.
func Example_queue() {
// Open/create a queue.
q, err := goque.OpenQueue("data_dir")
if err != nil {
fmt.Println(err)
return
}
defer q.Close()
// Enqueue an item.
item, err := q.Enqueue([]byte("item value"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(item.ID) // 1
fmt.Println(item.Key) // [0 0 0 0 0 0 0 1]
fmt.Println(item.Value) // [105 116 101 109 32 118 97 108 117 101]
fmt.Println(item.ToString()) // item value
// Change the item value in the queue.
item, err = q.Update(item.ID, []byte("new item value"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(item.ToString()) // new item value
// Dequeue the next item.
deqItem, err := q.Dequeue()
if err != nil {
fmt.Println(err)
return
}
fmt.Println(deqItem.ToString()) // new item value
// Delete the queue and its database.
q.Drop()
}