英文:
Nested structures in Golang - Struct A in struct B and struct B in struct A
问题
我必须创建两个相互包含的结构体。
由于RoutingTable结构体在声明之前不能用作类型,所以会出现编译错误。
是否可以同时声明它们呢?
type Node struct
{
name string
engaged bool
visited_packages[]int
rt []RoutingTable
}
type RoutingTable struct
{
next_hop Node
cost int
}
英文:
I have to create two structures that contain each other.
There is a compiler error because the RoutingTable structure cannot be used as a type until it is declared.
is it possible to declare them "at the same time"?
type Node struct
{
name string
engaged bool
visited_packages[]int
rt []RoutingTable
}
type RoutingTable struct
{
next_hop Node
cost int
}
答案1
得分: 1
好的,我把我的结构声明放在主函数中,当我把它放在外面时,它开始工作了。
英文:
Ok, I put my structures' declarations in main function, when I put it outside it it started to work
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论