Skip to content

Instantly share code, notes, and snippets.

@jleonardolemos
Created December 20, 2017 16:54
Show Gist options
  • Save jleonardolemos/3ac8414585218e2de5f6e04b1c5552a9 to your computer and use it in GitHub Desktop.
Save jleonardolemos/3ac8414585218e2de5f6e04b1c5552a9 to your computer and use it in GitHub Desktop.
A simple stack implementation in golang
package collections
import "errors"
type Stack struct {
valores []interface{}
}
func (stack *Stack) Push (valor interface{}) {
stack.valores = append(stack.valores, valor)
}
func (stack Stack) Lenght () int {
return len(stack.valores)
}
func (stack Stack) IsEmpty () bool {
return stack.Lenght() == 0
}
func (stack *Stack) Pull () (interface{}, error) {
if stack.IsEmpty() {
return nil, errors.New("Pilha Vazia")
}
valor := stack.valores[stack.Lenght() - 1]
stack.valores = stack.valores[:stack.Lenght() - 1]
return valor, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment