arrays - 戈朗 : using nested structs

标签 arrays struct go

我有两个结构:

type person struct {
    name string
    age  int
}

type class struct {
    students []person   
}

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

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

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

最佳答案

下面应该实现你想要的:

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

测试一下 here .

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

关于arrays - 戈朗 : using nested structs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25683122/

相关文章:

javascript - 我自己对 Array.reduce() 和 Array.from(string) 的处理出现意外返回值

c - 如何在 C 中使用外部 union 数组?

javascript - 在数组中向前和向后选择 X 数量,如果需要则循环到开头和结尾

c - 解引用指向不完整类型的指针 - 使用指向函数的指针将值分配给结构

c++ - 从文件读取并将值放入结构数组时遇到问题

arrays - 错误 : The part-name to the right of a part-ref with nonzero rank has the ALLOCATABLE attribute

go - 我正在编写一个具有 irc 样式命令的聊天应用程序。构建程序以处理这些命令的最佳方式是什么?

go - BufferedWriter 在同时使用时不起作用

javascript - 在 Javascript 中引用 Go 数组

c - 为什么内部 Lua 字符串会这样存储?