Go Prepend Slice
Saturday 28 January 2023

Prepending slice in Go

 1package main
 2
 3import (
 4    "fmt"
 5)
 6
 7func main() {
 8    var x []int
 9    for i := 2; i < 10; i += 2 {
10        x = append(x, i)
11    }
12    fmt.Println(x)
13
14    // Prepending 1 to X 
15    x = append([]int{1}, x...)
16
17    fmt.Println(x)
18}

Reference