Standard library / slices

slices.Replace()

Replaces the half-open range [i:j] with supplied elements. Length can change; invalid indices panic and obsolete tail slots are cleared when shrinking. Expected stdout: '[1 9 4]\n'.

Formal Syntax & Structure

go syntaxgo
func Replace[S ~[]E, E any](s S, i, j int, v ...E) S

Parameters & Attributes

NameTypeDescription
sSInput slice; may share its backing array.
iintIndex or start position.
jintSecond index or exclusive end position.
v...EElement value or variadic values for this operation.

Return Value

S

Replaces the half-open range [i:j] with supplied elements. Length can change; invalid indices panic and obsolete tail slots are cleared when shrinking.

Working Example

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