Array of a pointer in Go JSON marshalling

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

Array of a pointer in Go JSON marshalling

问题

我有以下代码,我想测试一下我是否正确地进行了JSON编组:

package main
import (
    "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: 语法错误: 意外的&,期望类型

代码中的第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"}}

如何修复这个问题?

英文:

I've the following code, where I want to just test whether I'm marshaling my JSON correctly or not:

package main
import (
    "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))

} 

I'm getting this error:

.\go_json_eg2.go:28:32: syntax error: unexpected &, expecting type.

Line no: 28 for my code is MyJSON := &TestSpec{Files: []&TestFileList{File: &TestFile{Download_Seconds: 600, Name: "filename1"}, File: &TestFile{Download_Seconds: 1200, Name: "filename2"}}}

Fairly new to Go marshaling. I figured I'm doing this wrong []&TestFileList{File: &TestFile{Download_Seconds: 600, Name: "filename1"}, File: &TestFile{Download_Seconds: 1200, Name: "filename2"}}.

How to fix this ?

答案1

得分: 2

&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: ...}}

您可以在这里了解更多关于复合字面量的信息。

英文:
&TestSpec{
    Files: []*TestFileList{
        {File: &TestFile{Download_Seconds: 600, Name: "filename1"}},
        {File: &TestFile{Download_Seconds: 1200, Name: "filename2"}},
    },
}

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

Notice that besides the error pointed out by Zombo in the comments, you also left out the curly braces that delimit the individual elements in the slice, i.e. you have {File: ..., File: ...}, but it should be {{File: ...}, {File: ...}}.

You can read more about Composite Literals here.

huangapple
  • 本文由 发表于 2022年3月20日 23:32:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/71548127.html
匿名

发表评论

匿名网友

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

确定