在Golang中的嵌套结构 – 结构体A在结构体B中,结构体B在结构体A中。

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

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
	}

my error screenshot

my code screenshot

答案1

得分: 1

好的,我把我的结构声明放在主函数中,当我把它放在外面时,它开始工作了。

英文:

Ok, I put my structures' declarations in main function, when I put it outside it it started to work

huangapple
  • 本文由 发表于 2021年6月17日 17:35:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/68016706.html
匿名

发表评论

匿名网友

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

确定