如何在Go语言中重新创建这个JavaScript数组结构?

huangapple go评论130阅读模式
英文:

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

问题

我有一个JavaScript数组,我该如何在Go中重新创建它?我完全不知道如何创建这么多嵌套的切片或数组。

  1. var goArray = [][]interface{}{
  2. []interface{}{},
  3. []interface{}{},
  4. []interface{}{},
  5. []interface{}{},
  6. []interface{}{},
  7. []interface{}{},
  8. []interface{}{},
  9. []interface{}{},
  10. []interface{}{},
  11. []interface{}{},
  12. }

JSON输出:

  1. [[[],[],[],[],[]],[[],[],[],[],[]]]

谢谢!

英文:

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.

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

JSON output:

  1. [[[],[],[],[],[]],[[],[],[],[],[]]]

Thank you!

答案1

得分: 2

假设你要存储在当前的JS数组中的值如下所示:

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

相同的值可以用Golang切片来存储,如下所示:

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

上述切片的输出如下所示:

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

这与jsArray的结构相同。

如果你想使用数组而不是切片,可以这样定义:

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

希望这对你有所帮助。

英文:

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

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

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

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

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

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

Hope this will help you.

答案2

得分: 0

尝试这个:

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

你可以创建嵌套的切片,并从外到内描述它包含的每个切片的长度。

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

英文:

Try this:

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

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

huangapple
  • 本文由 发表于 2021年8月29日 08:05:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/68968934.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定