Created
December 20, 2017 16:54
-
-
Save jleonardolemos/3ac8414585218e2de5f6e04b1c5552a9 to your computer and use it in GitHub Desktop.
A simple stack implementation in golang
This file contains 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
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