-
Notifications
You must be signed in to change notification settings - Fork 21
/
pqueue2.go
75 lines (61 loc) · 1.27 KB
/
pqueue2.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
//
// Persistent queue for storing an implementatino of an interface
//
import (
"github.com/beeker1121/goque"
"log"
"time"
)
const (
dirName = "pqueue"
)
type AnObject interface {
Sleep()
}
type MyObject struct {
Name string
Age int64
}
func (mo *MyObject) Sleep() {
time.Sleep(time.Duration(mo.Age))
}
func main() {
add(&MyObject{"Jon", 30})
add(&MyObject{"Deb", 31})
add(&MyObject{"Fluffy", 8})
add(&MyObject{"Fido", 2})
log.Println("got Object:", get())
log.Println("got Object:", get())
log.Println("got Object:", get())
log.Println("got Object:", get())
}
// add adds an object to the given prefix
func add(obj AnObject) {
pq, err := goque.OpenQueue(dirName)
if err != nil {
log.Panicln("Error opening queue", err)
}
defer pq.Close()
item, err := pq.EnqueueObject(obj)
log.Println("item added: ", obj, item.ID)
}
// getAndLog gets an object from the queue and logs it
func get() AnObject {
pq, err := goque.OpenQueue(dirName)
if err != nil {
log.Panicln("Error opening queue", err)
}
defer pq.Close()
item, err := pq.Peek()
if err != nil {
log.Panicln("Error peeking in queue", err)
}
item, err = pq.Dequeue()
if err != nil {
log.Panicln("Error peeking in queue", err)
}
var obj MyObject
err = item.ToObject(&obj)
return &obj
}