如何在Golang中初始化一个嵌套列表?

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

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)
}

huangapple
  • 本文由 发表于 2022年4月18日 22:36:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/71913222.html
匿名

发表评论

匿名网友

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

确定