Skip to main content
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.

Installation

Interface

Constructors

All four constructors accept a sizeHint parameter and return container.Stack[E].
int
required
A non-binding hint for the initial capacity. Some implementations use it to pre-allocate storage; others ignore it. Pass 0 if you have no estimate.

Choosing an implementation

Optional Countable interface

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.
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.

Example

Stack is not concurrency-safe. Do not share a Stack across goroutines without external synchronization.