-
Notifications
You must be signed in to change notification settings - Fork 1
/
ds_stack.go
44 lines (34 loc) · 890 Bytes
/
ds_stack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package main
import (
"errors"
)
// defining type stack which as items which is an array of values
type Stack struct {
items []interface{}
}
// s pointing to the Stack is similar to "this" in TS
func (s *Stack) Push(value interface{}) {
s.items = append(s.items, value)
}
// we can return two variables using parentheses
func (s *Stack) Pop() (interface{}, error) {
// if it is empty, return 0 and throw error
if s.IsEmpty() {
ErrStackEmpty := errors.New("Stack is empty")
return 0, ErrStackEmpty
}
// idiomatic was to pop - get last index, define value, slice the slice at that index
index := len(s.items) - 1
value := s.items[index]
s.items = s.items[:index]
return value, nil
}
func (s *Stack) IsEmpty() bool {
return len(s.items) == 0
}
func (s *Stack) Peek() interface{} {
return s.items[len(s.items)-1]
}
func (s *Stack) Size() int {
return len(s.items)
}