-
Notifications
You must be signed in to change notification settings - Fork 81
/
example_stack_test.go
51 lines (41 loc) · 1022 Bytes
/
example_stack_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/beeker1121/goque"
)
// ExampleStack demonstrates the implementation of a Goque stack.
func Example_stack() {
// Open/create a stack.
s, err := goque.OpenStack("data_dir")
if err != nil {
fmt.Println(err)
return
}
defer s.Close()
// Push an item onto the stack.
item, err := s.Push([]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 stack.
item, err = s.Update(item.ID, []byte("new item value"))
if err != nil {
fmt.Println(err)
return
}
fmt.Println(item.ToString()) // new item value
// Pop an item off the stack.
popItem, err := s.Pop()
if err != nil {
fmt.Println(err)
return
}
fmt.Println(popItem.ToString()) // new item value
// Delete the stack and its database.
s.Drop()
}