【问题标题】:golang: using nested structsgolang:使用嵌套结构
【发布时间】:2014-09-05 09:51:49
【问题描述】:

我有两个结构:

type person struct {
    name string
    age  int
}

type class struct {
    students []person   
}

假设在主函数中,我创建并填充了两个人员变量,然后我想将它们添加到具有类类型的变量中。我该怎么做?

 s := person{name: "Sean", age: 50}
 t := person{name: "Nicola", age: 35} 

如何将 s 和 t 放入: lab:=class?

【问题讨论】:

标签: arrays struct go


【解决方案1】:

以下应该可以实现您想要的:

lab := class{[]person{s, t}}

测试它here

在您继续处理您的项目之前,强烈建议您阅读以下内容:

【讨论】:

    猜你喜欢
    • 2023-03-04
    • 2021-12-24
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 2014-03-25
    • 2018-10-15
    • 1970-01-01
    相关资源
    最近更新 更多