Skip to content

slice-preallocation

Detect slices that can use range-source capacity.

Updated View as Markdown

Default severity: note

Conservatively reports an empty slice followed by exactly one direct append per iteration of a range with a useful len. Preallocating that capacity avoids repeated growth while preserving zero length.

Bad

var result []Item
for _, item := range source {
	result = append(result, convert(item))
}

Good

result := make([]Item, 0, len(source))
for _, item := range source {
	result = append(result, convert(item))
}
Navigation

Type to search…

↑↓ navigate↵ selectEsc close