Skip to content

Instantly share code, notes, and snippets.

@kissofjudase23
Created January 1, 2020 08:44
Show Gist options
  • Save kissofjudase23/bae0f58f0d7efe8f6055ab531aceb9a7 to your computer and use it in GitHub Desktop.
Save kissofjudase23/bae0f58f0d7efe8f6055ab531aceb9a7 to your computer and use it in GitHub Desktop.
go library
type Item interface{}
// ItemStack the stack of Items
type ItemStack struct {
items []Item
lock *sync.RWMutex
}
// New creates a new ItemStack
func New(len int, cap int) *ItemStack {
s := ItemStack{items: make([]Item, len, cap),
lock: &sync.RWMutex{}}
return &s
}
// Push adds an Item to the top of the stack
func (s *ItemStack) Push(t Item) {
s.lock.Lock()
defer s.lock.Unlock()
s.items = append(s.items, t)
}
// Pop removes an Item from the top of the stack
func (s *ItemStack) Pop() *Item {
s.lock.Lock()
defer s.lock.Unlock()
item := s.items[len(s.items)-1]
s.items = s.items[0 : len(s.items)-1]
return &item
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment