doutan1875 2016-03-09 03:17
浏览 48
已采纳

如何填充嵌套的golang结构,其中包含结构数组

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条回答 默认 最新

  • douxun1934 2016-03-09 04:13
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记