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

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

Go JSON 編組中的指針數組

Go JSON 編組中的指針數組

Go
瀟湘沐 2022-11-23 19:14:13
我有以下代碼,我只想測試我是否正確編組了我的 JSON:package mainimport (    "encoding/json"    "fmt") type TestFile struct {        Download_Seconds        int                `json:"download_seconds"`                                                                                     Name                    string             `json:"name"`                                                            } type TestFileList struct {        File                    *TestFile          `json:"file"`                                                                                                                                                           }type TestSpec struct {        Files                   []*TestFileList    `json:"files"`                                                                                                 } func main() {         r := new(TestSpec)         b, _ := json.Marshal(r)         fmt.Println(string(b))         MyJSON := &TestSpec{Files: []&TestFileList{File: &TestFile{Download_Seconds: 600, Name: "filename1"}, File: &TestFile{Download_Seconds: 1200, Name: "filename2"}}}         b1, _ := json.Marshal(MyJSON)         fmt.Println(string(b1))} 我收到此錯誤:.\go_json_eg2.go:28:32: syntax error: unexpected &, expecting type.Line no: 28因為我的代碼是MyJSON := &TestSpec{Files: []&TestFileList{File: &TestFile{Download_Seconds: 600, Name: "filename1"}, File: &TestFile{Download_Seconds: 1200, Name: "filename2"}}}Go 封送處理相當新。我想我做錯了[]&TestFileList{File: &TestFile{Download_Seconds: 600, Name: "filename1"}, File: &TestFile{Download_Seconds: 1200, Name: "filename2"}}。如何解決這個問題?
查看完整描述

1 回答

?
慕運維8079593

TA貢獻1876條經驗 獲得超5個贊

&TestSpec{

    Files: []*TestFileList{

        {File: &TestFile{Download_Seconds: 600, Name: "filename1"}},

        {File: &TestFile{Download_Seconds: 1200, Name: "filename2"}},

    },

}

https://go.dev/play/p/I30Mm0CxrUT


請注意,除了 Zombo 在評論中指出的錯誤之外,您還遺漏了分隔切片中各個元素的花括號,即您有{File: ..., File: ...},但它應該是{{File: ...}, {File: ...}}。


您可以在此處閱讀有關復合文字 的更多信息。


查看完整回答
反對 回復 2022-11-23
  • 1 回答
  • 0 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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