在切片的索引处添加单个值

If I have

array1 := []int{1, 3, 4, 5}
array2 := []int{2, 4, 6, 8}

And I want to insert array2[2] i.e 6 at array1[1] i.e before 3 so that array1 becomes a slice of {1, 6, 3, 4, 5}. How can I do it?

Most the techniques I read online involve using the : operator but results in remaining elements being inserted as well. How can I append single values at an index in a slice?

Simple, efficient and logical way:

  1. Make sure array1 has enough capacity (length) to accomodate the new, insertable element. To do that, append a single element using the builting append() (doesn't matter what that is, it'll get overwritten).
  2. To insert an element, existing elements must be shifted (copied over to 1 index higher) to make room for that element, e.g. using the builtin copy() (elements you want to insert before).
  3. Set the element at the proper index, using a single assignment.

In code:

array1 := []int{1, 3, 4, 5}
array2 := []int{2, 4, 6, 8}

array1 = append(array1, 0)   // Step 1
copy(array1[2:], array1[1:]) // Step 2
array1[1] = array2[2]        // Step 3

fmt.Println(array1)

Output (try it on the Go Playground):

[1 6 3 4 5]

extending the answer from @Volker, i put the answer here https://play.golang.org/p/3Hla2y2ava too if you want to test it.

package main

import "fmt"

func main() {
    array1 := []int{1, 3, 4, 5}
    array2 := []int{2, 4, 6, 8}
    temp := append([]int{array2[2]}, array1[1:]...)
    array1 = append(array1[:1], temp...)
    fmt.Println(array1)
}

I don't know is it optimal or not, but this piece of code works for me:

func sliceins(arr []int, pos int, elem int) []int { //insert element before pos in slice. if pos >= len(arr) insert into tail
    if pos < 0 {
        pos = 0
    } else if pos >= len(arr) {
        pos = len(arr)
    }
    out := make([]int, len(arr)+1)
    copy(out[:pos], arr[:pos])
    out[pos] = elem
    copy(out[pos+1:], arr[pos:])
    return out
}

In Your case just call

sliceins(array1, 1, array2[2])

I found the question setup pretty tricky to follow.

Rephrased, they want to insert an element. Here we have an array where it's missing the element 3 and we want to insert it.

package main

import (
    "fmt"
)

func main() {
    a := []int{1, 2, 4, 5, 6}
    b := 3

    // Make space in the array for a new element. You can assign it any value.
    a = append(a, 0)   
    fmt.Println(a)

    // Copy over elements sourced from index 2, into elements starting at index 3.
    copy(a[3:], a[2:])  
    fmt.Println(a)

    a[2] = b         
    fmt.Println(a)
}

Based on icza's post i wrote a function to shift the slice / array which I want to share with you:

package main

import "fmt"

func main() {
    s := []string{"a", "c", "d"}
    shiftArray(&s, 1, "b")
    fmt.Println(s)

}

func shiftArray(array *[]string, position int, value string) {
    //  extend array by one
    *array = append(*array, "")

    // shift values
    copy((*array)[position+1:], (*array)[position:])

    // insert value
    (*array)[position] = value
}