Standard library / slices

slices.Repeat()

Creates a slice containing count repetitions of the input elements. Panics for negative count or length overflow; result is non-nil. Expected stdout: '[1 2 1 2 1 2]\n'.

Formal Syntax & Structure

go syntaxgo
func Repeat[S ~[]E, E any](x S, count int) S

Parameters & Attributes

NameTypeDescription
xSInput value or collection to process.
countintNumber of repetitions.

Return Value

S

Creates a slice containing count repetitions of the input elements. Panics for negative count or length overflow; result is non-nil.

Working Example

Example Snippetgo
package main
import (  "fmt"  "slices")
func main() {  fmt.Println(slices.Repeat([]int{1, 2}, 3))}
Go Reference