英文:
loading static data into nested struct in Go
问题
我正在寻找实现类似于GoPlayground示例的东西:https://go.dev/play/p/B4JOVwgdwUk
然而,我的数据结构稍微复杂一些,其中有一个名为Tag的嵌套结构体。我无法正确地将数据加载到toFile
变量声明中的结构体中。
有人可以帮助我找出我做错了什么吗?我尝试了下面显示的完整代码,以及这个:
Tags: []Tag{
[{"appID:": "go", "category": "backend"},{"appID": "fiber", "category": "framework"}]
}
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
func main() {
type Stack struct {
Reputation int `json:"reputation"`
UserID int `json:"user_id"`
UserType string `json:"user_type"`
ProfileImage string `json:"profile_image"`
DisplayName string `json:"display_name"`
Link string `json:"link"`
Tags []Tag `json:"tags"`
}
type Tag struct {
AppID string `json: "appID:,omitempty"`
Category string `json: "category:,omitempty"`
}
toFile := Stack{
Reputation: 141,
UserID: 9820773,
UserType: "registered",
ProfileImage: "https://graph.facebook.com/10209865263541884/picture?type=large",
DisplayName: "Joe Smith",
Link: "https://stackoverflow.com/users/9820773/joe-smith",
Tags: []Tag{
{"appID:": "go", "category": "backend"},
{"appID": "fiber", "category": "framework"}
}, // 无法使其工作
}
// 写入文件!
tmpFile, err := ioutil.TempFile(os.TempDir(), "sample-")
if err != nil {
panic(err)
}
defer os.Remove(tmpFile.Name())
err = json.NewEncoder(tmpFile).Encode(toFile)
if err != nil {
panic(err)
}
err = tmpFile.Close()
if err != nil {
panic(err)
}
// 让我们读取它!
tmpFile2, err := os.Open(tmpFile.Name())
if err != nil {
panic(err)
}
var fromFile Person
err = json.NewDecoder(tmpFile2).Decode(&fromFile)
if err != nil {
panic(err)
}
err = tmpFile2.Close()
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", fromFile)
}
英文:
I am looking to implement something very similar to this GoPlayground example:
https://go.dev/play/p/B4JOVwgdwUk
However my data structure is a bit more complex, with the nested struct called Tag. I can't quite get the syntax correct for how to load the data into the struct in the toFile
variable declaration.
Can someone help me figure out what I am doing wrong? I have tried what is displayed in the full code below, as well as this:
Tags: []Tag{
[{"appID:": "go", "category": "backend"},{"appID": "fiber", "category": "framework"}]
}
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
func main() {
type Stack struct {
Reputation int `json:"reputation"`
UserID int `json:"user_id"`
UserType string `json:"user_type"`
ProfileImage string `json:"profile_image"`
DisplayName string `json:"display_name"`
Link string `json:"link"`
Tags []Tag `json:"tags"`
}
type Tag struct {
AppID string `json: "appID:,omitempty"`
Category string `json: "category:,omitempty"`
}
toFile := Stack{
Reputation: 141,
UserID: 9820773,
UserType: "registered",
ProfileImage: "https://graph.facebook.com/10209865263541884/picture?type=large",
DisplayName: "Joe Smith",
Link: "https://stackoverflow.com/users/9820773/joe-smith",
Tags: Tag{
[{"appID:": "go", "category": "backend"},{"appID": "fiber", "category": "framework"}]
} // cannot get this to work
}
// Write it out!
tmpFile, err := ioutil.TempFile(os.TempDir(), "sample-")
if err != nil {
panic(err)
}
defer os.Remove(tmpFile.Name())
err = json.NewEncoder(tmpFile).Encode(toFile)
if err != nil {
panic(err)
}
err = tmpFile.Close()
if err != nil {
panic(err)
}
// Let's read it in!
tmpFile2, err := os.Open(tmpFile.Name())
if err != nil {
panic(err)
}
var fromFile Person
err = json.NewDecoder(tmpFile2).Decode(&fromFile)
if err != nil {
panic(err)
}
err = tmpFile2.Close()
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", fromFile)
}
答案1
得分: 1
@Kousik的评论帮助我修复了代码,并成功在GoPlayground上运行了它:
https://go.dev/play/p/WmZ9m_55W3S
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
func main() {
type Tag struct {
AppID string `json:"appID,omitempty"`
Category string `json:"category,omitempty"`
}
type Stack struct {
Reputation int `json:"reputation"`
UserID int `json:"user_id"`
UserType string `json:"user_type"`
ProfileImage string `json:"profile_image"`
DisplayName string `json:"display_name"`
Link string `json:"link"`
Tags []Tag `json:"tags"`
}
toFile := Stack{
Reputation: 141,
UserID: 9820773,
UserType: "registered",
ProfileImage: "https://graph.facebook.com/10209865263541884/picture?type=large",
DisplayName: "Joe Smith",
Link: "https://stackoverflow.com/users/9820773/joe-smith",
Tags: []Tag{{AppID: "id1", Category: "cat1"}, {AppID: "id2", Category: "cat2"}},
}
// 写入文件!
tmpFile, err := ioutil.TempFile(os.TempDir(), "sample-")
if err != nil {
panic(err)
}
defer os.Remove(tmpFile.Name())
err = json.NewEncoder(tmpFile).Encode(toFile)
if err != nil {
panic(err)
}
err = tmpFile.Close()
if err != nil {
panic(err)
}
// 让我们读取它!
tmpFile2, err := os.Open(tmpFile.Name())
if err != nil {
panic(err)
}
var fromFile Stack
err = json.NewDecoder(tmpFile2).Decode(&fromFile)
if err != nil {
panic(err)
}
err = tmpFile2.Close()
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", fromFile)
}
英文:
The comment from @Kousik helped me fix up the code and run it successfully on the GoPlayground:
https://go.dev/play/p/WmZ9m_55W3S
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)
func main() {
type Tag struct {
AppID string `json: "appID:,omitempty"`
Category string `json: "category:,omitempty"`
}
type Stack struct {
Reputation int `json:"reputation"`
UserID int `json:"user_id"`
UserType string `json:"user_type"`
ProfileImage string `json:"profile_image"`
DisplayName string `json:"display_name"`
Link string `json:"link"`
Tags []Tag `json:"tags"`
}
toFile := Stack{
Reputation: 141,
UserID: 9820773,
UserType: "registered",
ProfileImage: "https://graph.facebook.com/10209865263541884/picture?type=large",
DisplayName: "Joe Smith",
Link: "https://stackoverflow.com/users/9820773/joe-smith",
Tags: []Tag{{AppID: "id1", Category: "cat1"}, {AppID: "id2", Category: "cat2"}},
}
// Write it out!
tmpFile, err := ioutil.TempFile(os.TempDir(), "sample-")
if err != nil {
panic(err)
}
defer os.Remove(tmpFile.Name())
err = json.NewEncoder(tmpFile).Encode(toFile)
if err != nil {
panic(err)
}
err = tmpFile.Close()
if err != nil {
panic(err)
}
// Let's read it in!
tmpFile2, err := os.Open(tmpFile.Name())
if err != nil {
panic(err)
}
var fromFile Stack
err = json.NewDecoder(tmpFile2).Decode(&fromFile)
if err != nil {
panic(err)
}
err = tmpFile2.Close()
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", fromFile)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论