【发布时间】:2018-03-19 00:41:01
【问题描述】:
type A struct {
B []struct {
C string
D []struct {
E string
F []struct {
G string
}
}
}
}
假设我有一个结构 A 的实例,我想 将结构 D 附加到其中。我会尝试做类似的事情
var a A;
...
a.B.D = append(a.B.D, ???)
??? = A.B.D
-->类型A没有方法B
??? = D
--> 未定义:D
--- 编辑更完整的例子---
type A struct {
B []struct {
C string
D hugeNestedElement
}
}
var a A
// Goal is to create many B's
a = append(a, what_goes_here)
// or ...
a = append(a.B, what_goes_here)
【问题讨论】:
-
你能举一个更完整的例子吗?这是所有的代码吗?
-
将 D 附加到 A 是不正确的说法。您实际上是在选择一个 B 来附加一个 D 。因为 A.B 是一个结构体切片,所以可能有很多 Bs
标签: go data-structures struct