Generic LIFO stack with multiple backing implementations.
Stack is a generic last-in, first-out (LIFO) data structure. The most
recently pushed element is the first to be popped. The library provides four
implementations that share a common interface, each with different allocation
strategies.
Some implementations also satisfy container.Countable, which adds a Len()
method. Always check for it via a type assertion rather than relying on a
specific implementation.
if c, ok := s.(container.Countable); ok { fmt.Printf("elements in stack: %d\n", c.Len())}
Len() is not part of the core Stack interface because not all
implementations provide it. Always guard the call with a type assertion as
shown above.
package mainimport ( "fmt" "github.com/fgm/container" "github.com/fgm/container/stack")type Frame intfunc main() { const sizeHint = 100 s := stack.NewSliceStack[Frame](sizeHint) s.Push(Frame(1)) s.Push(Frame(2)) s.Push(Frame(3)) // Optionally check the length if the implementation supports it. if c, ok := s.(container.Countable); ok { fmt.Printf("elements in stack: %d\n", c.Len()) } // Drain the stack — LIFO order. for { e, ok := s.Pop() if !ok { break } fmt.Printf("popped: %v\n", e) } // Pop on an empty stack returns the zero value and ok=false. e, ok := s.Pop() fmt.Printf("empty pop -> value: %v, ok: %t\n", e, ok)}// Output:// elements in stack: 3// popped: 3// popped: 2// popped: 1// empty pop -> value: 0, ok: false
Stack is not concurrency-safe. Do not share a Stack across goroutines
without external synchronization.