英文:
String Interpolation / Templating Variables in Go lang
问题
假设我有以下字节字符串,表示一些 JSON 数据包:
data := []byte(`{
"Id" : 1,
"Name": "John Doe",
"Occupation": "gardener"
}`)
我想知道,是否有一种动态创建这个 JSON 数据包的方法,例如:
var NAME = "John Doe"
var OCCUPATION = "gardener"
data := []byte(`{
"Id" : 1,
"Name": {{ NAME }},
"Occupation": {{ OCCUPATION }}
}`)
英文:
Let's say I have the following byte string which represents some JSON packet:
data := []byte(`{
"Id" : 1,
"Name": "John Doe",
"Occupation": "gardener"
}`)
I'm wondering, is there a way to create this JSON packet dynamically, e.g.,
var NAME = "John Doe"
var OCCUPATION = "gardener"
data := []byte(`{
"Id" : 1,
"Name": {{ NAME }},
"Occupation": {{ OCCUPATION }}
}`)
答案1
得分: 3
你可以创建一个结构体并进行编组:
type Data struct {
ID int `json:"Id"`
Name string `json:"Name"`
Occupation string `json:"Occupation"`
}
data := Data{ID: 1, Name: "name", Occupation: "Occupation"}
byteData, err := json.Marshal(data)
这样做也会在必要时处理数据中的转义字符。
另一种方法:
data := map[string]interface{}{
"Id": id,
"Name": name,
"Occupation": Occupation,
}
byteData, err := json.Marshal(data)
你也可以使用模板来实现,但是如果输入字符串包含需要转义的字符,有可能生成无效的 JSON 数据。
英文:
You can create a struct and marshal it:
type Data struct {
ID int `json:"Id"`
Name string `json:"Name"`
Occupation string `json:"Occupation"`
}
data:=Data{ ID:1, Name: "name", Occupation: "Occupation" }
byteData, err:=json.Marshal(data)
This will also take care of escaping characters in data when necessary.
Another way:
data:=map[string]interface{} {
"Id": id,
"Name": name,
"Occupation": Occupation,
}
byteData, err:=json.Marshal(data)
You can also use templates for this, but then there is a chance you may generate invalid JSON data if the input strings contain characters that need to be escaped.
答案2
得分: 0
Go by Example网站上有一个关于使用encoding/json
包的很好的页面:https://gobyexample.com/json
在你的特定情况下:
package main
import (
"encoding/json"
"log"
)
type Person struct {
ID int `json:"Id"`
Name string `json:"Name"`
Occupation string `json:"Occupation"`
}
func main() {
p := Person{ID: 1, Name: "John Doe", Occupation: "gardener"}
log.Printf("Person data: %+v\n", p)
d, err := json.Marshal(p)
if err != nil {
log.Fatalf("failed to marshal json: %v", err)
}
log.Printf("Encoded JSON: %s\n", d)
}
2022/09/05 16:02:54 Person data: {ID:1 Name:John Doe Occupation:gardener}
2022/09/05 16:02:54 Encoded JSON: {"Id":1,"Name":"John Doe","Occupation":"gardener"}
英文:
Go by Example has a great page on using the encoding/json
package: https://gobyexample.com/json
In your specific case:
package main
import (
"encoding/json"
"log"
)
type Person struct {
ID int `json:"Id"`
Name string `json:"Name"`
Occupation string `json:"Occupation"`
}
func main() {
p := Person{ID: 1, Name: "John Doe", Occupation: "gardener"}
log.Printf("Person data: %+v\n", p)
d, err := json.Marshal(p)
if err != nil {
log.Fatalf("failed to marshal json: %v", err)
}
log.Printf("Encoded JSON: %s\n", d)
}
2022/09/05 16:02:54 Person data: {ID:1 Name:John Doe Occupation:gardener}
2022/09/05 16:02:54 Encoded JSON: {"Id":1,"Name":"John Doe","Occupation":"gardener"}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论