Go's standard library covers most everyday data structures and algorithms: built-in slices, maps and arrays, container/list, container/heap and container/ring, plus the slices, maps, sort and strings packages for searching, sorting and transforming data. Since Go 1.21 the generic slices and maps packages replace most hand-written helper functions, so you rarely need a third-party collections library.
The sections below show which structure to pick for which job, how to sort and search with the modern API, and how to build stacks, queues and priority queues from the pieces Go gives you.
Go has three built-in collection types, and they handle the large majority of programs.
| Type | Size | Typical use | Cost |
|---|---|---|---|
Array [N]T | Fixed at compile time | Small fixed sets, keys of maps | Copied on assignment |
Slice []T | Dynamic | Default ordered list | Append is amortised O(1) |
Map map[K]V | Dynamic | Lookup by key | Average O(1) lookup, unordered |
The capacity value can differ between Go versions because the growth strategy is an implementation detail. slices.Sorted(maps.Keys(m)) needs Go 1.23 or later, where maps.Keys returns an iterator.
Two rules save most bugs. A slice is a view onto an underlying array, so sub-slices share memory until an append forces a reallocation. A map is not safe for concurrent writes, so guard it with a sync.Mutex or use sync.Map for the narrow cases it was designed for.
The container directory of the standard library holds three structures.
container/list is a doubly linked list. Values are stored as any, so you need a type assertion when you read them back.container/ring is a circular list, useful for fixed-size rolling windows.container/heap does not provide a type. It provides functions that turn any type implementing heap.Interface into a binary heap.In practice a slice is faster than a linked list for most workloads because it is contiguous in memory and friendly to the CPU cache. Reach for container/list when you need O(1) removal from the middle while holding an element handle, for example in an LRU cache.
Go has no dedicated stack or queue type. A slice does both jobs.
For a queue, q = q[1:] dequeues in O(1) but keeps the old backing array alive. For long-running queues use a ring buffer, a list.List, or a buffered channel when the producers and consumers run in different goroutines.
Before Go 1.21 you sorted with sort.Slice and a closure. The generic slices package is now the recommended route: it is type safe, avoids reflection and is usually faster.
Use slices.Max only on a non-empty slice, because it panics on an empty one.
cmp.Or needs Go 1.22 or later. slices.BinarySearch requires a sorted slice and returns the position where the value is or would be inserted. slices.SortStableFunc keeps equal elements in their original order when that matters.
A priority queue is the standard tool for Dijkstra, schedulers and top-K problems. You implement heap.Interface (Len, Less, Swap, Push, Pop) and then call heap.Init, heap.Push and heap.Pop.
Push and Pop on the type are the low-level hooks. Always call the package-level heap.Push and heap.Pop, which restore the heap order after the hook runs.
make([]T, 0, n) or make(map[K]V, n) when you know the size.cmp instead of any plus assertions.strings.Builder, bytes.Buffer and slices.Grow to avoid repeated reallocation.go test -bench and testing.B, not intuition.The standard library has no built-in trees, graphs or sets. A set is normally a map[T]struct{}; for balanced trees or graphs, write a small generic type or use a well-maintained module.
No. Use map[T]struct{}, which stores keys with no per-value overhead, and test membership with the comma-ok form.
Prefer slices.SortFunc on Go 1.21 or later. It is generic, type safe and avoids the reflection used by sort.Slice.
Go randomises map iteration order on purpose. Collect the keys, sort them with slices.Sorted, and iterate over the sorted keys.
When you need constant-time insertion or removal in the middle of a long collection and you already hold the element pointer. In most other cases a slice is faster.
Go's standard library gives you slices, maps, arrays, a linked list, a ring and a heap, plus generic slices and maps helpers for sorting, searching and transforming data. Use slices and maps by default, build stacks and queues on slices, use container/heap for priority queues, and benchmark before optimising.