【发布时间】:2023-03-26 21:53:01
【问题描述】:
是否可以在 Go 中创建数组,其中数组的每个元素都是切片或结构的数组。
类似于 PHP 的东西
$a = [1=>"test", 2=>""]
// in this example 2 is integer will be for GoLang?
$a[2] = [ object, object, object ]
我可以在 Go 中做类似的事情吗?我知道不正确的语法。
var a [int][]StructureName
b := make([]StructureName, 0)
b := append ( b, StructureName{a, b, c, d})
b := append ( b, StructureName{e, f, g, h})
a[0] = append (a[0][0], b)
`/*
[
1 => [
‘username1’, <-- string element
‘events’=>[ <-- array of structures
object1, <-- structure
object2, <-- structure
object3 <-- structure
]
],
2 => [ <-- next record
‘username2’,
‘events’=>[
object1,
object2,
object3
]
]
]
*/
`
【问题讨论】:
-
你能说得清楚一点吗?你想创建数组数组吗?
-
我需要使用第一个元素和用户名创建数组,第二个元素和结构数组(包含此用户名的事件的记录)啊哈,我需要另外 1 个数组,所有这些数组都将包含在内所以,是的,它将是数组数组
标签: arrays dictionary go slice