Maps

Learn about maps in Go.

map iteration order is random (no really)

Technically map iteration order is “undefined”. Go maps use a hash table internally so map iteration would normally be done in whatever order the map elements are laid out in that table. This order can’t be relied on and changes as a hash table grow when new elements are added to the map.

In the early days of Go, this was a serious trap for programmers who didn’t read the instructions and relied on maps being iterated in a certain order. To help spot these issues early on rather than in production, Go developers made map iterations random-ish:

Get hands-on with 1200+ tech skills courses.