Queue, Stack, and Set.
Prerequisites
- Go 1.18 or later (generics support is required).
- A Go module initialized with
go mod init.
Steps
1
Install the module
Add
github.com/fgm/container to your module:2
Use a Queue
Import the Output:
queue sub-package and create a slice-backed queue with a size hint.3
Use a Stack
Import the Output:
stack sub-package. The API mirrors the queue — Push and Pop instead of Enqueue and Dequeue.4
Use a Set
Import the
set sub-package. Set eliminates duplicates and supports standard set operations.5
Use a WaitableQueue for concurrent code
WaitableQueue is the only concurrency-safe type in the module. It uses a WaitChan to signal consumers when items are available, avoiding the capacity constraints of Go channels.Call
Close() on the queue when the producer is done. This unblocks any consumers that are waiting on WaitChan, allowing them to drain remaining items and exit cleanly.The size hint pattern
Every constructor accepts asizeHint argument. This is a hint — not a hard cap — that slice-backed implementations use to pre-allocate storage and avoid repeated resizing:
0 if you have no estimate. The implementation will still work correctly, just without the pre-allocation benefit.
Import paths
Next steps
OrderedMap
Explore stable and recency ordering modes.
WaitableQueue
Learn about watermarks and flow control for concurrent queues.
Set operations
Use Union, Intersection, and Difference on your sets.
Choosing an implementation
Pick the right backing store for your performance profile.