← All recipes
Moderne licensed

Preallocate slice

Recipe IDorg.openrewrite.golang.codequality.PreallocateSliceArtifactgithub.com/moderneinc/recipes-go

Add a capacity to a slice made empty and then filled by appending over a range, so out := make([]int, 0) before for _, x := range xs becomes make([]int, 0, len(xs)). The capacity is a hint, so only the allocation changes.

Single recipeProprietary

Usage

This recipe has no required configuration options. You’ll need the Moderne CLI configured before running the command below.

mod run . --recipe org.openrewrite.golang.codequality.PreallocateSlice

If the recipe isn’t available locally, install it with:

mod config recipes go install github.com/moderneinc/recipes-go