Go JSON 编组中的指针数组

发布于 2025-01-15 15:53:21 字数 1746 浏览 1 评论 0原文

我有以下代码,我只想测试我是否正确编组 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"},文件:&TestFile{Download_Seconds:1200,名称:“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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寻找一个思念的角度 2025-01-22 15:53:21
&TestSpec{
    Files: []*TestFileList{
        {File: &TestFile{Download_Seconds: 600, Name: "filename1"}},
        {File: &TestFile{Download_Seconds: 1200, Name: "filename2"}},
    },
}

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

注意,除了指出的错误之外by Zombo 在注释中,您还省略了分隔切片中各个元素的花括号,即您有 {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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文