Return error if stack is empty
This commit is contained in:
@@ -1,8 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import "errors"
|
||||||
|
|
||||||
// Helper functions for slices, to make them behave more like stacks
|
// Helper functions for slices, to make them behave more like stacks
|
||||||
func peek[T any](s []T) T {
|
func peek[T any](s []T) (T, error) {
|
||||||
return s[len(s)-1]
|
if len(s) < 1 {
|
||||||
|
return *new(T), errors.New("Stack empty")
|
||||||
|
}
|
||||||
|
return s[len(s)-1], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func pop[T any](sp *[]T) T {
|
func pop[T any](sp *[]T) T {
|
||||||
|
Reference in New Issue
Block a user