Standard library / slices

slices.Grow()

Ensures capacity for at least n additional elements without changing length. May allocate; negative n panics. Expected stdout: '1 true\n'.

Formal Syntax & Structure

go syntaxgo
func Grow[S ~[]E, E any](s S, n int) S

Parameters & Attributes

NameTypeDescription
sSInput slice; may share its backing array.
nintCount or bound for this operation.

Return Value

S

Ensures capacity for at least n additional elements without changing length. May allocate; negative n panics.

Working Example

Example Snippetgo
package main
import (  "fmt"  "slices")
func main() {  a := slices.Grow([]int{1}, 5)  fmt.Println(len(a), cap(a)-len(a) >= 5)}
Go Reference