seniorGolang

Optimizing slice allocations using capacity.

Updated Apr 28, 2026

Short answer

Pre-allocating slice capacity prevents expensive memory reallocations and copies during append operations.

Deep explanation

When a slice reaches its capacity, Go allocates a new array (usually 2x the size for small slices) and copies elements. If you loop 10,000 times appending to a slice, this reallocation happens roughly 14 times, causing massive CPU and GC overhead. make([]T, length, capacity) eliminates this.

Unlock with a Pro subscription to view this section.

View pricing

Real-world example

No real-world example available yet.

Unlock with a Pro subscription to view this section.

Upgrade to Pro

Common mistakes

No common mistakes listed yet.

Unlock with a Pro subscription to view this section.

Upgrade to Pro

Follow-up questions

No follow-up questions available yet.

Unlock with a Pro subscription to view this section.

Upgrade to Pro

More Golang interview questions

View all →