英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论