{"record":{"id":"e83c81a63133e719","repo":"TheAlgorithms/Go","slug":"stack-list-is-empty","errorCode":null,"errorMessage":"stack list is empty","messagePattern":"stack list is empty","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"structure/stack/stacklinkedlistwithlist.go","lineNumber":33,"sourceCode":")\n\n// SList is our struct that point to stack with container/list.List library\ntype SList struct {\n\tStack *list.List\n}\n\n// Push add a value into our stack\nfunc (sl *SList) Push(val any) {\n\tsl.Stack.PushFront(val)\n}\n\n// Peak is return last value that insert into our stack\nfunc (sl *SList) Peek() (any, error) {\n\tif !sl.IsEmpty() {\n\t\telement := sl.Stack.Front()\n\t\treturn element.Value, nil\n\t}\n\treturn \"\", fmt.Errorf(\"stack list is empty\")\n}\n\n// Pop is return last value that insert into our stack\n// also it will remove it in our stack\nfunc (sl *SList) Pop() (any, error) {\n\tif !sl.IsEmpty() {\n\t\t// get last element that insert into stack\n\t\telement := sl.Stack.Front()\n\t\t// remove element in stack\n\t\tsl.Stack.Remove(element)\n\t\t// return element value\n\t\treturn element.Value, nil\n\t}\n\treturn \"\", fmt.Errorf(\"stack list is empty\")\n}\n\n// Length return length of our stack\nfunc (sl *SList) Length() int {","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/TheAlgorithms/Go/blob/5ba447ec5ff3d1213de65b92e726ee74c5d5cc19/structure/stack/stacklinkedlistwithlist.go#L15-L51","documentation":"SList.Peek returns the value at the top of the linked-list-backed stack without popping it. When the stack is empty, it returns this error (and an empty-string placeholder) because there is no top element. It is a misuse guard: callers must check IsEmpty() first.","triggerScenarios":"Calling Peek() on an empty SList — a newly created stack, one whose elements were all popped, or one never pushed to.","commonSituations":"Balanced-parentheses / expression-evaluation code that peeks before verifying the stack has operands, or peeking after a drain loop in undo-history implementations.","solutions":["Check sl.IsEmpty() before calling Peek().","Handle the returned error and treat it as 'stack empty' rather than using the placeholder value.","Ensure push operations actually execute before peek paths run (check control flow)."],"exampleFix":"// before\ntop, _ := sl.Peek()\ncompare(top)\n// after\nif sl.IsEmpty() {\n    return // nothing on the stack\n}\ntop, err := sl.Peek()\nif err != nil {\n    return err\n}\ncompare(top)","handlingStrategy":"validation","validationCode":"if sl.IsEmpty() {\n    return errors.New(\"cannot peek: stack is empty\")\n}\ntop, err := sl.Peek()","typeGuard":"func peekSafe(sl *stack.SList) (any, bool) {\n    if sl.IsEmpty() {\n        return nil, false\n    }\n    v, err := sl.Peek()\n    return v, err == nil\n}","tryCatchPattern":"top, err := sl.Peek()\nif err != nil {\n    if strings.Contains(err.Error(), \"empty\") {\n        return // stack empty, handle accordingly\n    }\n    return err\n}","preventionTips":["Always call IsEmpty() before Peek().","Never use the empty-string return when err != nil.","Verify pushes execute before peek paths in expression-evaluation code."],"tags":["go","stack","data-structure","empty-collection"],"backgroundTag":"empty-collection-access","analyzedSha":"5ba447ec5ff3d1213de65b92e726ee74c5d5cc19","analyzedAt":"2026-09-02T21:54:30.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}