The append()
function expects the first argument passed to it to be the value that will be returned and everything starting from the second argument will be appended to it.
So if you pass a two-dimensional array inside append()
as a first argument, you will have a two-dimensional array returned from it.
The expression append(arr, twoDiamArr...)
is the opposite, because you are trying to put elements of a two-dimensional array inside another one-dimensional. As a result you have one dimensional array with all elements from two-dimensional
Instead, you can use a two-dimensional slice literal with the value you want to have as the first element. Everything spread in the second argument will be appended after it.
package main
import "fmt"
func main() {
var twoDimensional = [][]int{{1}, {2}}
var oneDimensional = []int{3}
twoDimensional = append([][]int{oneDimensional}, twoDimensional...)
fmt.Println(twoDimensional)
// prints [[3] [1] [2]]
}