Skip to content

Instantly share code, notes, and snippets.

@sausheong
Created May 16, 2022 06:14
Show Gist options
  • Select an option

  • Save sausheong/d8f43f1c2828551cb360d1d190894703 to your computer and use it in GitHub Desktop.

Select an option

Save sausheong/d8f43f1c2828551cb360d1d190894703 to your computer and use it in GitHub Desktop.
generics
type StringStack struct {
items []string
}
func (s *StringStack) Push(item string) {
s.items = append(s.items, item)
}
func (s *StringStack) Pop() (item string) {
if !s.IsEmpty() {
item = s.items[len(s.items)-1]
s.items = s.items[:len(s.items)-1]
}
return
}
func (s *StringStack) Peek() (item string) {
if !s.IsEmpty() {
item = s.items[len(s.items)-1]
}
return
}
func (s *StringStack) IsEmpty() bool {
return len(s.items) == 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment