Pop removes the next item in the stack and returns it.
()
| 122 | |
| 123 | // Pop removes the next item in the stack and returns it. |
| 124 | func (s *Stack) Pop() (*Item, error) { |
| 125 | s.Lock() |
| 126 | defer s.Unlock() |
| 127 | |
| 128 | // Check if stack is closed. |
| 129 | if !s.isOpen { |
| 130 | return nil, ErrDBClosed |
| 131 | } |
| 132 | |
| 133 | // Try to get the next item in the stack. |
| 134 | item, err := s.getItemByID(s.head) |
| 135 | if err != nil { |
| 136 | return nil, err |
| 137 | } |
| 138 | |
| 139 | // Remove this item from the stack. |
| 140 | if err := s.db.Delete(item.Key, nil); err != nil { |
| 141 | return nil, err |
| 142 | } |
| 143 | |
| 144 | // Decrement head position. |
| 145 | s.head-- |
| 146 | |
| 147 | return item, nil |
| 148 | } |
| 149 | |
| 150 | // Peek returns the next item in the stack without removing it. |
| 151 | func (s *Stack) Peek() (*Item, error) { |