亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何填充包含結構數組的嵌套 golang 結構

如何填充包含結構數組的嵌套 golang 結構

Go
慕田峪9158850 2021-12-20 15:54:19
所以我一直試圖填充我創建的結構的這個怪物,但沒有成功。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"            }         ]      }   ]}有關于填充嵌套結構的已回答問題,但我還沒有找到包含結構數組的問題。我知道這可能很簡單,但我就是想不通。謝謝。
查看完整描述

1 回答

?
慕神8447489

TA貢獻1780條經驗 獲得超1個贊

您可能需要將這些內部結構定義為類型。這有效:


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,

}

我只放了 1 章,但你明白了。


查看完整回答
反對 回復 2021-12-20
  • 1 回答
  • 0 關注
  • 153 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號