Created
January 1, 2020 08:44
-
-
Save kissofjudase23/bae0f58f0d7efe8f6055ab531aceb9a7 to your computer and use it in GitHub Desktop.
go library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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