-1

Trying to add a string to an initialised array of array of interfaces.

matrix := [][]interface{}{{"cat", "cat", "cat"}}
a := [][]interface{}{{"tiger"}}
matrix = append(matrix, a...)

Output:

[[cat cat cat] [tiger]]

But I wanted tiger in together with [[cat cat cat tiger]] like this.

aakash singh
  • 267
  • 5
  • 19

1 Answers1

0

You are adding an array of interface (a...) to matrix an array of array of interface:
your array of array (matrix) has now two arrays.

You would need to add to the first element of that array (matrix[0]), in order to keep one array inside your array of array.

Example: https://goplay.tools/snippet/QcPTYIh1wJR

package main

import (
    "fmt"
)

func main() {
    matrix := [][]interface{}{{"cat", "cat", "cat"}}
    a := [][]interface{}{{"tiger"}}
    matrix = append(matrix, a...)
    fmt.Println("matrix :", matrix)

    matrix2 := [][]interface{}{{"cat", "cat", "cat"}}
    b := append(matrix2[0], a[0]...)
    matrix2[0] = b
    fmt.Println("matrix2: ", matrix2)
}

Output:

matrix : [[cat cat cat] [tiger]]
matrix2:  [[cat cat cat tiger]]
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250