From 4f9e220638ea62443db25d252c4e12ede2c7f9c5 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Sun, 1 Oct 2023 20:02:22 -0400 Subject: error handling and tidying --- stack.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'stack.go') diff --git a/stack.go b/stack.go index 182d418..1a52e94 100644 --- a/stack.go +++ b/stack.go @@ -6,11 +6,17 @@ func (s *Stack) push(v float64) { *s = append(*s, v) } -func (s *Stack) pop() *float64 { +func (s *Stack) pop() (float64, error) { if len(*s) > 0 { v := (*s)[len(*s)-1] *s = (*s)[:len(*s)-1] - return &v + return v, nil } - return nil + return 0, EmptyStackErr{} +} + +type EmptyStackErr struct{} + +func (e EmptyStackErr) Error() string { + return "empty stack" } -- cgit v1.2.3