英文:
How to initalize a nested list in Golang?
问题
我有一个嵌套的结构体,像这样:
type Project struct {
FolderStructure []FolderItem
Description string
}
type FolderItem struct {
SubFolderStructure []SubFolderItem
Description string
}
type SubFolderItem struct {
SubSubFolderStructure []SubSubFolderItem
Description string
}
type SubSubFolderItem struct {
Content string
Description string
}
我想知道如何初始化它,否则会抛出invalid memory address or nil pointer dereference
错误。提前谢谢!
英文:
I have a nested struct like this:
type Project struct {
FolderStructure []FolderItem
Description string
}
type FolderItem struct {
SubFolderStructure []SubFolderItem
Description string
}
type SubFolderItem struct {
SubSubFolderStructure []SubSubFolderItem
Description string
}
type SubSubFolderItem struct {
Content string
Description string
}
I wonder how to initalize it, otherwise invalid memory address or nil pointer dereference
will be thrown out.
Thanks in advance!
答案1
得分: 0
初始化的最简单方法是创建多个实例作为变量,然后重复使用它们来为嵌套结构赋值。
以下是一个可工作的示例:https://go.dev/play/p/6p3VFljyqom,下面也是同样的示例,这只是一种实现方式,我认为这是最简单的方式。
package main
import "fmt"
type Project struct {
FolderStructure []FolderItem
Description string
}
type FolderItem struct {
SubFolderStructure []SubFolderItem
Description string
}
type SubFolderItem struct {
SubSubFolderStructure []SubSubFolderItem
Description string
}
type SubSubFolderItem struct {
Content string
Description string
}
func main() {
ssfi1 := SubSubFolderItem{
"content1",
"description1 - SubSubFolderItem",
}
ssfi2 := SubSubFolderItem{
"content2",
"description2 - SubSubFolderItem",
}
sfi := SubFolderItem{
SubSubFolderStructure: []SubSubFolderItem{ssfi1, ssfi2},
Description: "description 1 - SubFolderItem",
}
fi := FolderItem{
SubFolderStructure: []SubFolderItem{sfi, sfi},
Description: "description 1 - FolderItem",
}
p := Project{
FolderStructure: []FolderItem{fi, fi},
Description: "description 1 - Project",
}
fmt.Println(ssfi1)
fmt.Println(ssfi2)
fmt.Println(sfi)
fmt.Println(fi)
fmt.Println(p)
}
英文:
The easiest way to initialize is to create multiple instances as variables & then reuse them to assign the values to the nested structures.
Here is the working example: https://go.dev/play/p/6p3VFljyqom and same below, this is just one way of doing it and feel it's the easiest.
package main
import "fmt"
type Project struct {
FolderStructure []FolderItem
Description string
}
type FolderItem struct {
SubFolderStructure []SubFolderItem
Description string
}
type SubFolderItem struct {
SubSubFolderStructure []SubSubFolderItem
Description string
}
type SubSubFolderItem struct {
Content string
Description string
}
func main() {
ssfi1 := SubSubFolderItem{
"content1",
"description1 - SubSubFolderItem",
}
ssfi2 := SubSubFolderItem{
"content2",
"description2 - SubSubFolderItem",
}
sfi := SubFolderItem{
SubSubFolderStructure: []SubSubFolderItem{ssfi1, ssfi2},
Description: "description 1 - SubFolderItem",
}
fi := FolderItem{
SubFolderStructure: []SubFolderItem{sfi, sfi},
Description: "description 1 - FolderItem",
}
p := Project{
FolderStructure: []FolderItem{fi, fi},
Description: "description 1 - Project",
}
fmt.Println(ssfi1)
fmt.Println(ssfi2)
fmt.Println(sfi)
fmt.Println(fi)
fmt.Println(p)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论