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) SParameters & Attributes
| Name | Type | Description |
|---|---|---|
| s | S | Input slice; may share its backing array. |
| i | int | Index or start position. |
| j | int | Second index or exclusive end position. |
| v | ...E | Element 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))}