How to populate a nested golang struct, which contains an array of structs

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

How to populate a nested golang struct, which contains an array of structs

问题

所以我一直在尝试填充这个我创建的庞大结构体,但一直没有成功。

type Initial_Load struct {
    Chapters []struct {
        Name    string `Chapter Name`
        PageNum int    `Number of Page"`
        Pages   []struct {
            Description string `Page Description`
            PageNumber  int    `Page Number`
            Source      string `Page Source`
        }
    }
    NumChapters int `Total number of chapters`
}

这是这个结构体所模拟的JSON数据:

{
    "Num_Chapters": 2,
    "Chapters": [
        {
            "Name": "Pilot",
            "Page_Num": 2,
            "Pages": [
                {
                    "Page_Number": 1,
                    "Source": "local.com",
                    "Description": "First Page"
                },
                {
                    "Page_Number": 2,
                    "Source": "local.com",
                    "Description": "Second Page"
                }
            ]
        },
        {
            "Name": "Chapter2",
            "Page_Num": 2,
            "Pages": [
                {
                    "Page_Number": 1,
                    "Source": "local.com",
                    "Description": "First Page"
                },
                {
                    "Page_Number": 2,
                    "Source": "local.com",
                    "Description": "Second Page"
                }
            ]
        },
        {
            "Name": "Chapter3",
            "Page_Num": 2,
            "Pages": [
                {
                    "Page_Number": 1,
                    "Source": "local.com",
                    "Description": "First Page"
                },
                {
                    "Page_Number": 2,
                    "Source": "local.com",
                    "Description": "Second Page"
                }
            ]
        }
    ]
}

关于填充嵌套结构体的问题有很多解答,但我还没有找到一个包含结构体数组的解答。我知道这可能非常简单,但我就是想不出来。谢谢。

英文:

So I've been trying to populate this monster of a struct I created, but with no success.

type Initial_Load struct {
	Chapters []struct {
		Name    string `Chapter Name`
		PageNum int    `Number of Page"`
		Pages   []struct {
			Description string `Page Description`
			PageNumber  int    `Page Number`
			Source      string `Page Source`
		}
	}
	NumChapters int `Total number of chapters`
}

Here's the JSON that this struct is modeling

{
   "Num_Chapters": 2,
   "Chapters": [
      {
         "Name": "Pilot",
         "Page_Num": 2,
         "Pages": [
            {
               "Page_Number": 1,
               "Source": "local.com",
               "Description": "First Page"
            },
            {
               "Page_Number": 2,
               "Source": "local.com",
               "Description": "Second Page"
            }
         ]
      },
      {
         "Name": "Chapter2",
         "Page_Num": 2,
         "Pages": [
            {
               "Page_Number": 1,
               "Source": "local.com",
               "Description": "First Page"
            },
            {
               "Page_Number": 2,
               "Source": "local.com",
               "Description": "Second Page"
            }
         ]
      },
      {
         "Name": "Chapter3",
         "Page_Num": 2,
         "Pages": [
            {
               "Page_Number": 1,
               "Source": "local.com",
               "Description": "First Page"
            },
            {
               "Page_Number": 2,
               "Source": "local.com",
               "Description": "Second Page"
            }
         ]
      }
   ]
}

There's of answered questions about populating nested structs, but I haven't found one which contains an array of structs. I know this is probably very simple, but I just can't figure it out. Thanks.

答案1

得分: 4

你可能需要将这些内部结构定义为类型。可以这样做:

type Page struct {
    Description string
    PageNumber  int
    Source      string
}

type Chapter struct {
    Name    string
    PageNum int
    Pages   []Page
}

type Initial_Load struct {
    Chapters    []Chapter
    NumChapters int
}

var x Initial_Load = Initial_Load{
    Chapters: []Chapter{
        {
            Name:    "abc",
            PageNum: 3,
            Pages: []Page{
                {
                    Description: "def",
                    PageNumber:  3,
                    Source:      "xyz",
                },
                {
                    Description: "qrs",
                    PageNumber:  5,
                    Source:      "xxx",
                },
            },
        },
    },
    NumChapters: 1,
}

我只放了一个章节,但你可以理解这个思路。

英文:

You may need to define those inner structs as types. This works:

type Page struct {
	Description string
	PageNumber  int
	Source      string
}

type Chapter struct {
	Name    string
	PageNum int
	Pages   []Page
}

type Initial_Load struct {
	Chapters    []Chapter
	NumChapters int
}

var x Initial_Load = Initial_Load{
	Chapters: []Chapter{
		{
			Name:    "abc",
			PageNum: 3,
			Pages: []Page{
				{
					Description: "def",
					PageNumber:  3,
					Source:      "xyz",
				},
				{
					Description: "qrs",
					PageNumber:  5,
					Source:      "xxx",
				},
			},
		},
	},
	NumChapters: 1,
}

I only put in 1 chapter, but you get the idea.

huangapple
  • 本文由 发表于 2016年3月9日 11:17:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/35882216.html
匿名

发表评论

匿名网友

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

确定