Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

158
Vistas
How can I re-create this Javascript array structure in Go?

I have this Javascript array, how can I recreate it in Go? I'm completely lost on how to create so many nested slices or arrays.

var jsArray = [
    [
        [],
        [],
        [],
        [],
        [],
    ],
    [
        [],
        [],
        [],
        [],
        []
    ]
];

JSON output:

[[[],[],[],[],[]],[[],[],[],[],[]]]

Thank you!

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Let's assume that the values that you will store in your current JS array will look like this

var jsArray = [
    [
        [1, 2],
        [3, 4],
        [5, 6],
        [7, 8],
        [9, 10]
    ],
    [
        [11, 12],
        [13, 14],
        [15, 16],
        [17, 18],
        [19, 20]
    ]
];

The same values can be stored in the Golang slices like this

goSlice := [][][]int{
    {
        []int{1, 2},
        []int{3, 4},
        []int{5, 6},
        []int{7, 8},
        []int{9, 10},
    },
    {
        []int{11, 12},
        []int{13, 14},
        []int{15, 16},
        []int{17, 18},
        []int{19, 20},
    },
}

The output for the above slice looks like this

[[[1 2] [3 4] [5 6] [7 8] [9 10]] [[11 12] [13 14] [15 16] [17 18] [19 20]]]

which is identical to the jsArray structure.

Instead of slices if you want to use array than you can use the define it like this

goArray := [2][5][2]int{
    {
        [2]int{1, 2},
        [2]int{3, 4},
        [2]int{5, 6},
        [2]int{7, 8},
        [2]int{9, 10},
    },
    {
        [2]int{11, 12},
        [2]int{13, 14},
        [2]int{15, 16},
        [2]int{17, 18},
        [2]int{19, 20},
    },
}

Hope this will help you.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try this:

func main() {
    var jsArray [2][5][0]int
    fmt.Println(jsArray)
}

You can create your nested slice and describe the length of each slice it contains going from the outside in.

https://www.tutorialspoint.com/go/go_multi_dimensional_arrays.htm

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda