The complete guide to Go Data Structures

By

An index of classic data structures implemented in Go: the binary search tree, the graph, and the set, each implemented and explained in its own post.

~~~

This is the index of the classic data structures implemented in Go on this blog: the binary search tree, the graph, and the set. Each one is described and implemented in its own post, linked below.

This week, in various articles on this blog, I posted an analysis and implementation of these structures. This post acts as the entry point.

Why implement data structures in Go?

Go ships with two workhorse data structures built into the language: slices and maps. They cover most of what you need day to day.

But the classic structures from computer science are not in the standard library. When you need a tree, a graph, or a set, you build it yourself. Writing them by hand is also one of the best ways I know to get comfortable with Go: you practice structs, methods, and pointers on problems you already understand conceptually.

Data structures covered, in alphabetical order:

Binary Search Tree

A binary search tree keeps its values ordered. Each node has up to two children: smaller values go to the left, bigger values to the right.

This layout makes lookups, insertions, and deletions fast, because every comparison lets you discard half of the remaining tree. Reach for it when you need ordered data with quick searches.

One thing to be careful with: if you insert already-sorted values, the tree degenerates into a long chain and you lose the speed advantage.

Graph

A graph is a collection of nodes connected by edges. It’s the natural model for anything network-shaped: roads between cities, links between web pages, friendships between people.

The post covers how to represent the nodes and their connections in Go, and how to add nodes and edges to the structure.

Set

A set is a collection of values with no duplicates and no defined order. You use it when the only question you care about is “is this item in here or not?”

Go has no built-in set type. The idiomatic base is a map, since map keys are unique by definition, and the post builds a full set on top of that idea, with operations like union, intersection, and difference.

Start with the set if you’re new to this: it’s the smallest of the three and a gentle introduction to how the other implementations are organized.

Tagged: Go · All topics
~~~

Related posts about go: