Go has a built-in append function which add elements in the slice:

func append(slice []Type, elems ...Type) []Type

But how if we want to the "prepend" effect? Maybe we should use copy function. E.g.:

package main

import "fmt"

func main()  {
    var s []int = []int{1, 2}

    s1 := make([]int, len(s) + 1)
    s1[0] = 0
    copy(s1[1:], s)
    s = s1


The result is like this:

[1 2]
[0 1 2]

But the above code looks ugly and cumbersome, so an elegant implementation maybe here:

s = append([]int{0}, s...)

BTW, I also have tried to write a "general-purpose" prepend:

func Prepend(v interface{}, slice []interface{}) []interface{}{
    return append([]interface{}{v}, slice...)

But since []T can't convert to an []interface{} directly (please refer, it is just a toy, not useful.

Go – append/prepend item into slice.

results matching ""

    No results matching ""