Answers for "Golang Insert At Index (any slice)"

Go
0

Golang Insert At Index (any slice)

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 
// [1 6 3 4 5]
Posted by: Guest on July-28-2021
0

Golang Insert At Index (any slice)

a = append(a[:index+1], a[index:]...)
a[index] = value
Posted by: Guest on July-28-2021

Browse Popular Code Answers by Language