By Golang

  1. // Package stack creates a ItemStack data structure for the Item type
  2. package stack
  3. import (
  4. "sync"
  5. )
  6. // Item the type of the stack
  7. type Item interface{}
  8. // ItemStack the stack of Items
  9. type ItemStack struct {
  10. items []Item
  11. lock sync.RWMutex
  12. }
  13. // New creates a new ItemStack
  14. func (s *ItemStack) New() *ItemStack {
  15. s.items = []Item{}
  16. return s
  17. }
  18. // Push adds an Item to the top of the stack
  19. func (s *ItemStack) Push(t Item) {
  20. s.lock.Lock()
  21. s.items = append(s.items, t)
  22. s.lock.Unlock()
  23. }
  24. // Pop removes an Item from the top of the stack
  25. func (s *ItemStack) Pop() *Item {
  26. s.lock.Lock()
  27. item := s.items[len(s.items)-1]
  28. s.items = s.items[0 : len(s.items)-1]
  29. s.lock.Unlock()
  30. return &item
  31. }