Skip to content

Archive

Allocation

2 articles
Go 13 Sep 2026 4 min read

Repeat Slice Patterns with slices.Repeat in Go

slices.Repeat builds a new slice by concatenating a source slice with itself a specified number of times. The operation is small, but its exact contract matters when code depends on allocation, nil state, or reference-bearing elements. The function belongs to the standard slices package and has this signature: func Repeat[S ~[]E, E any](x S, count int) S The returned slice has both length and capacity equal to len(x) * count. The result is never nil.

Go 13 Sep 2026 4 min read

Grow Slice Capacity with slices.Grow in Go

slices.Grow reserves room for future appends without changing a slice’s length. The distinction between length and capacity is central to its contract: existing elements remain the logical contents, while the returned slice can accept at least a requested number of additional elements without another allocation. The generic signature is: func Grow[S ~[]E, E any](s S, n int) S The return value matters because increasing capacity can require a new backing array.