Skip to main content
This guide walks you through installing Go Container and using three of its data structures: 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 queue sub-package and create a slice-backed queue with a size hint.
Output:
The second Dequeue returns the zero value and ok: false because the queue is empty. Always check ok before using the returned element.
3

Use a Stack

Import the stack sub-package. The API mirrors the queue — Push and Pop instead of Enqueue and Dequeue.
Output:
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 a sizeHint argument. This is a hint — not a hard cap — that slice-backed implementations use to pre-allocate storage and avoid repeated resizing:
Pass 0 if you have no estimate. The implementation will still work correctly, just without the pre-allocation benefit.

Import paths

All implementations except WaitableQueue are not safe for concurrent use. Protect them with a sync.Mutex or equivalent when sharing across goroutines.

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.