24 lines
442 B
Go
24 lines
442 B
Go
|
package main
|
||
|
|
||
|
import "fmt"
|
||
|
|
||
|
// Stack is a stack of integers with no defined max depth
|
||
|
type Stack struct {
|
||
|
values []int
|
||
|
}
|
||
|
|
||
|
// Pop returns the top of the stack
|
||
|
func (s *Stack) Pop() (int, error) {
|
||
|
if len(s.values) == 0 {
|
||
|
return 0, fmt.Errorf("stack empty")
|
||
|
}
|
||
|
i := s.values[0]
|
||
|
s.values = s.values[1:]
|
||
|
return i, nil
|
||
|
}
|
||
|
|
||
|
// Push adds a value to the top of the stack
|
||
|
func (s *Stack) Push(i int) {
|
||
|
s.values = append([]int{i}, s.values...)
|
||
|
}
|