Golang slices.Repeat
last modified June 29, 2026
This tutorial explains how to use the slices.Repeat function in Go.
We'll cover slice operations with practical examples of creating repeated elements.
The slices.Repeat function creates a new slice by repeating the elements
of the given slice a specified number of times. It is part of Go's standard
slices package, available since Go 1.23.
This function is useful for initializing slices with repeated values or creating patterns. It returns a new slice with the element repeated count times.
Basic slices.Repeat Example
The simplest use of slices.Repeat creates a slice with repeated zeros.
We specify the element to repeat and the count of repetitions.
package main
import (
"fmt"
"slices"
)
func main() {
repeated := slices.Repeat([]int{0}, 5)
fmt.Println("Repeated zeros:", repeated)
}
We create a slice with five zeros. The first argument is a slice containing the value to repeat, and the second is the repetition count. The output shows [0 0 0 0 0].
Repeating Strings
slices.Repeat can create slices of repeated strings. This example
makes a slice with three "hello" strings.
package main
import (
"fmt"
"slices"
)
func main() {
greetings := slices.Repeat([]string{"hello"}, 3)
fmt.Println("Greetings:", greetings)
}
The function repeats the string "hello" three times. The result is a slice containing ["hello", "hello", "hello"].
Working with Structs
We can use slices.Repeat with custom struct types. This example
creates multiple instances of a Person struct.
package main
import (
"fmt"
"slices"
)
type Person struct {
Name string
Age int
}
func main() {
defaultPerson := Person{"Unknown", 0}
people := slices.Repeat([]Person{defaultPerson}, 2)
fmt.Println("People:", people)
}
We define a Person struct and create two copies of a default instance. Each element in the resulting slice is a separate copy of the struct.
Negative Count Handling
Passing a negative count to slices.Repeat causes a runtime panic.
This example demonstrates how to guard against invalid counts using
recover.
package main
import (
"fmt"
"slices"
)
func main() {
// Valid count
positive := slices.Repeat([]int{1}, 3)
fmt.Println("Positive count:", positive)
// Negative count causes a panic
defer func() {
if r := recover(); r != nil {
fmt.Println("Panic with negative count:", r)
}
}()
slices.Repeat([]int{1}, -1)
}
With a positive count we get the expected repeated elements. Passing a negative count panics with the message cannot be negative.
Zero Count Behavior
When count is zero, slices.Repeat returns an empty slice.
This example shows the difference between zero and positive counts.
package main
import (
"fmt"
"slices"
)
func main() {
empty := slices.Repeat([]string{"test"}, 0)
fmt.Println("Empty slice:", empty, "Length:", len(empty))
nonEmpty := slices.Repeat([]string{"test"}, 1)
fmt.Println("Non-empty slice:", nonEmpty, "Length:", len(nonEmpty))
}
A count of zero produces an empty slice, while count 1 creates a single-element slice. Both are valid but represent different concepts.
Performance Considerations
For large repetition counts, memory allocation becomes important. This example benchmarks different repetition sizes.
package main
import (
"fmt"
"slices"
"time"
)
func main() {
start := time.Now()
small := slices.Repeat([]int{1}, 100)
_ = small
fmt.Println("Small slice:", time.Since(start))
start = time.Now()
large := slices.Repeat([]int{1}, 1_000_000)
_ = large
fmt.Println("Large slice:", time.Since(start))
}
The execution time scales with the count parameter. Memory allocation is done once for the entire slice, making it efficient for large counts.
Practical Example: Initializing a Game Board
This practical example uses slices.Repeat to initialize a game board
with default values. We create a 2D slice representing the board.
package main
import (
"fmt"
"slices"
)
func main() {
const width, height = 5, 5
const emptyCell = "."
// Create board rows
board := make([][]string, height)
for i := range board {
board[i] = slices.Repeat([]string{emptyCell}, width)
}
// Print the board
for _, row := range board {
fmt.Println(row)
}
}
We create each row independently using slices.Repeat, which ensures
mutations to one row do not affect the others.
Source
Go slices.Repeat standard library documentation
This tutorial covered the slices.Repeat function in Go with practical
examples of creating repeated slice elements in various scenarios.
Author
List all Go tutorials.