Skip to content

Instantly share code, notes, and snippets.

@lkrych
Created June 13, 2018 15:39
Show Gist options
  • Save lkrych/117d990e3227cb293b29941783f1a0d4 to your computer and use it in GitHub Desktop.
Save lkrych/117d990e3227cb293b29941783f1a0d4 to your computer and use it in GitHub Desktop.
Stack implementation in Go
type stack struct { //use a struct to define the stack
data []int
}
func (s *stack) pop() int {
popped := s.data[len(s.data)-1]
s.data = s.data[:len(s.data)-1] //resize the internal slice
return popped
}
func (s *stack) push(item int) {
s.data = append(s.data, item)
}
func (s *stack) peek() int {
return s.data[len(s.data)-1]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment