Skip to content
Reliability and safety

determinism

Checks map iteration reaching ordered output without explicit sorting.

AvailabilityOpt-in
Fix offeredNo

What it detects

Reports map iteration whose order flows directly into ordered output, such as a returned slice or string, encoded data, or written text, without an explicit sorting step.

Checks

Check What it detects
map-output-order*
Reports map iteration that reaches ordered output without explicit sorting.

* Opt-in; requires explicit selection.

Why this is flagged

Go deliberately does not guarantee map iteration order. Letting that order reach output can produce flaky tests, noisy generated files, and unstable hashes even when the underlying data has not changed.

Further reading: The Go specification: Range clauses.

How to fix it

Copy the map keys or output values into a slice, sort that slice, and produce the output from the sorted order. Choose an explicit comparison when ordinary string or numeric order is not the intended result.

Examples

Flagged code

func names(users map[string]User) []string {
var result []string
// gohawk: map iteration reaches ordered output without sorting
for name := range users {
result = append(result, name)
}
return result
}

Accepted code

func sortedNames(users map[string]User) []string {
var result []string
for name := range users {
result = append(result, name)
}
slices.Sort(result)
return result
}