ExampleObject demonstrates enqueuing a struct object.
()
| 8 | |
| 9 | // ExampleObject demonstrates enqueuing a struct object. |
| 10 | func Example_object() { |
| 11 | // Open/create a queue. |
| 12 | q, err := goque.OpenQueue("data_dir") |
| 13 | if err != nil { |
| 14 | fmt.Println(err) |
| 15 | return |
| 16 | } |
| 17 | defer q.Close() |
| 18 | |
| 19 | // Define our struct. |
| 20 | type object struct { |
| 21 | X int |
| 22 | Y int |
| 23 | } |
| 24 | |
| 25 | // Enqueue an object. |
| 26 | item, err := q.EnqueueObject(object{X: 1, Y: 2}) |
| 27 | if err != nil { |
| 28 | fmt.Println(err) |
| 29 | return |
| 30 | } |
| 31 | |
| 32 | fmt.Println(item.ID) // 1 |
| 33 | fmt.Println(item.Key) // [0 0 0 0 0 0 0 1] |
| 34 | |
| 35 | // Dequeue an item. |
| 36 | deqItem, err := q.Dequeue() |
| 37 | if err != nil { |
| 38 | fmt.Println(err) |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | // Create variable to hold our object in. |
| 43 | var obj object |
| 44 | |
| 45 | // Decode item into our struct type. |
| 46 | if err := deqItem.ToObject(&obj); err != nil { |
| 47 | fmt.Println(err) |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | fmt.Printf("%+v\n", obj) // {X:1 Y:2} |
| 52 | |
| 53 | // Delete the queue and its database. |
| 54 | q.Drop() |
| 55 | } |