Skip to content

Instantly share code, notes, and snippets.

@terror
Created June 25, 2021 19:56
Show Gist options
  • Save terror/5fcd8b7dae588e64b7e26a5a77f4f3bc to your computer and use it in GitHub Desktop.
Save terror/5fcd8b7dae588e64b7e26a5a77f4f3bc to your computer and use it in GitHub Desktop.
package main
import "fmt"
type Stack []string
func (s *Stack) IsEmpty() bool {
return len(*s) == 0
}
func (s *Stack) Push(str string) {
*s = append(*s, str)
}
func (s *Stack) Pop() (string, bool) {
if s.IsEmpty() {
return "", false
} else {
idx := len(*s) - 1
el := (*s)[idx]
*s = (*s)[:idx]
return el, true
}
}
func main() {
var st Stack
st.Push("Hello");
st.Push("World");
st.Push("!");
if len(st) > 0 {
for !st.IsEmpty() {
x, y := st.Pop()
if y == true {
fmt.Println(x);
}
}
}
// !
// World
// Hello
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment