Go gives you a small set of built-in structures — arrays, slices, maps, structs — and lets you build everything else, like trees, heaps, and graphs, on top of them.
Data structure reference
| Structure | Description | Real-world example |
|---|---|---|
Array | Fixed-length, fixed-type sequence. | RGB pixel buffer of a fixed image size |
Slice | Growable view over a backing array. | Loading rows from a database query |
Map | Hash table of key-value pairs. | Caching HTTP responses by URL |
Struct | Named fields grouped into one record. | A user profile with name, email, age |
Linked list (singly) | Nodes chained by one forward pointer. | A music player's next-track chain |
Linked list (doubly) | Nodes chained forward and backward. | Browser back/forward navigation history |
Stack | Last in, first out access. | Undo history in a text editor |
Queue | First in, first out access. | A print spooler processing jobs in order |
Binary search tree | Ordered tree, O(log n) lookup when balanced. | Autocomplete suggestions sorted alphabetically |
Heap | Tree keeping the min or max at the root. | A task scheduler running the highest-priority job next |
Graph | Nodes connected by arbitrary edges. | Road network for a GPS route planner |
Set | Unique values, usually a map[T]struct. | Tracking unique visitor IDs for a day |
Trie | Tree indexed by prefix, one branch per character. | Spell checker or search-bar suggestions |
Ring | Circular list, last node points back to first. | Round-robin load balancing across servers |
An array has a fixed length that's part of its type — [5]int and [10]int are different types. Rarely used directly; slices wrap arrays and are far more common.
var a [5]int // fixed size, part of the typeA slice is a small header (pointer, length, capacity) pointing at a backing array. Appending past capacity allocates a new, larger array and copies the data over.
A struct groups named fields into one type. It's Go's building block for records — no classes, no inheritance, just composition.
type Config struct {
Host string
Port int
}A map is a hash table: unordered key-value pairs with O(1) average lookup. Iteration order is intentionally randomized by the runtime.
m := map[string]int{
"a": 1,
"b": 2,
}A singly linked list chains nodes with one pointer each. Cheap to insert at the head, but O(n) to reach an arbitrary node — no random access like a slice.
type Node struct {
Value int
Next *Node
}A stack (LIFO) and queue (FIFO) are usage patterns, not distinct Go types — both are typically built from a slice or linked list, differing only in which end you push and pop from.
A binary tree links each node to at most two children. Search trees keep values ordered so lookup, insert, and delete run in O(log n) when balanced.
type TreeNode struct {
Value int
Left, Right *TreeNode
}A heap keeps the smallest (or largest) element accessible in O(1), with O(log n) insert and removal. Go's container/heap turns any type implementing its interface into a heap.
h := &IntHeap{5, 2, 8}
heap.Init(h)
heap.Push(h, 1) // container/heapA graph models nodes and arbitrary connections between them, commonly as an adjacency list — a map from node to its neighbors. Go has no built-in graph type.
type Graph struct {
Edges map[string][]string
}